DAY 6: SQL - JOIN Operations

Guru Charan Pathalla

DAY 6: SQL - JOIN Operations

Learning Objectives: - Master INNER JOIN - Learn LEFT JOIN and RIGHT JOIN - Understand JOIN relationships - Combine data from multiple tables (Mirror Excel VLOOKUP)

Topics Covered (60 min):

1. INNER JOIN (Mirror Excel VLOOKUP) - Returns only matching records from both tables - Syntax: SELECT * FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID - Example: Orders with matching customers only - Most common JOIN type

2. LEFT JOIN - Returns ALL from left table + matching from right - NULL for non-matching right table rows - Syntax: SELECT * FROM Customers LEFT JOIN Orders ON Customers.ID = Orders.CustomerID - Use case: Find customers with NO orders (WHERE Orders.ID IS NULL)

3. RIGHT JOIN - Returns ALL from right table + matching from left - Less common (can be rewritten as LEFT JOIN) - Usually avoid for consistency

4. Multi-Table JOINs - Chain JOINs: Orders JOIN Customers ON ... JOIN Products ON ... - Use aliases for readability: FROM Orders O JOIN Customers C ON O.CustomerID = C.ID - Get comprehensive data from 3+ tables

Video Resources:

  1. SQL GROUP BY Explained for Beginners (8:41)
    https://www.youtube.com/watch?v=MXK83I9bTrU

  2. SQL Aggregate Functions & GROUP BY (12:58)
    https://www.youtube.com/watch?v=5IuU8mxC3iE

  3. SQL GROUP BY: Visual Guide & Examples (14:29)
    https://www.youtube.com/watch?v=Fud1Rfsl9dE

AI Learning Prompts (Concept Understanding Only):

Understanding JOINs (Mirror VLOOKUP):

Yesterday I learned VLOOKUP in Excel. Help me understand SQL JOINs:

1. How are SQL JOINs similar to Excel VLOOKUP?
2. What's the fundamental concept of joining tables?
3. Explain INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN
4. When should I use each type of JOIN?
5. Show me Venn diagrams or visual representations for each JOIN
6. Give examples using Authors and Books tables (not sales data)

Explain the parallel between VLOOKUP and JOINs clearly.

Understanding INNER JOIN:

Explain SQL INNER JOIN in detail:

1. What does INNER JOIN return?
2. What's the syntax: SELECT ... FROM Table1 INNER JOIN Table2 ON condition?
3. What is the ON clause and how does it work?
4. Show me examples using Students and Enrollments tables
5. What happens to rows that don't have matches?
6. Can I join on multiple columns?

Use school or library data for examples (not orders).

Understanding LEFT JOIN:

Explain LEFT JOIN and its use cases:

1. How does LEFT JOIN differ from INNER JOIN?
2. What does "all records from left table" mean?
3. What are NULL values in the result?
4. When would I use LEFT JOIN vs INNER JOIN?
5. Show me examples: find customers who haven't made purchases using Customers LEFT JOIN Orders
6. How do I find records that DON'T have matches (WHERE column IS NULL)?

Use customer or membership data for examples.

Understanding Multi-Table JOINs:

Explain joining more than 2 tables:

1. How do I chain multiple JOINs?
2. Does the order of JOINs matter?
3. What are table aliases and why use them (AS)?
4. Show me how to JOIN 3 tables: Authors, Books, Publishers
5. How do I keep track of which columns come from which table?

Use publishing or university data for examples (not sales).

Guided Practice (60 min):

📝 How to approach Guided Practice:

  • Follow step-by-step: Work through each exercise in order
  • Use provided resources: Reference the video tutorials and AI prompts if you get stuck
  • Verify your work: Check your results match expected outcomes
  • Take notes: Document key learnings and shortcuts you discover
  • Ask for help: Use AI assistants to clarify concepts you don't understand

Using Superstore Sales with separate tables - SQL Practice

📝 Setup Instructions:

  1. Open SQLite Browser with your database
  2. You should have 3 tables: Orders, Customers, Products
  3. Orders table: OrderID, CustomerID, ProductID, Quantity, OrderDate
  4. Customers table: CustomerID, CustomerName, Region, Email
  5. Products table: ProductID, ProductName, Category, Price

💡 JOINs = SQL's VLOOKUP: Remember Day 5 (Excel VLOOKUP)? SQL JOINs do the same thing - combine related data from multiple tables! But JOINs are more powerful and flexible.

Exercise 1: Your First INNER JOIN - Add Customer Names

Step-by-Step Instructions:

  1. Type this query to join Orders with Customers:
    SELECT
        Orders.OrderID,
        Orders.CustomerID,
        Customers.CustomerName,
        Customers.Region,
        Orders.Quantity
    FROM Orders
    INNER JOIN Customers
        ON Orders.CustomerID = Customers.CustomerID
    LIMIT 20;
  2. Click Execute (▶️)
  3. You'll see order data WITH customer details!

🔍 Understanding the Syntax:

  • FROM Orders = Start with Orders table (left table)
  • INNER JOIN Customers = Connect to Customers table (right table)
  • ON Orders.CustomerID = Customers.CustomerID = Matching condition (like VLOOKUP lookup value)
  • Orders.OrderID = Specify which table each column comes from

💡 Tips:

  • INNER JOIN returns only rows where match exists in BOTH tables
  • Always specify table name for columns: Orders.CustomerID
  • The ON clause is like the VLOOKUP match - it connects related records

✅ Expected Result: Orders with customer names and regions added

Exercise 2: JOIN to Add Product Information

Step-by-Step Instructions:

  1. Now join Orders with Products to get product details:
    SELECT
        Orders.OrderID,
        Orders.ProductID,
        Products.ProductName,
        Products.Category,
        Products.Price,
        Orders.Quantity,
        (Products.Price * Orders.Quantity) AS TotalPrice
    FROM Orders
    INNER JOIN Products
        ON Orders.ProductID = Products.ProductID
    ORDER BY TotalPrice DESC
    LIMIT 20;
  2. Notice we calculated TotalPrice directly in the query!
  3. This is the SQL equivalent of Day 5's VLOOKUP + multiplication

💡 JOIN vs VLOOKUP Comparison:

  • Excel Day 5: =VLOOKUP(C2, Products!A:D, 4, FALSE)
  • SQL Day 6: INNER JOIN Products ON Orders.ProductID = Products.ProductID
  • Same concept, different syntax!

✅ Expected Result: Orders with product names, categories, prices, and calculated totals

Exercise 3: LEFT JOIN - Include ALL Orders (Even Without Matches)

Step-by-Step Instructions:

  1. LEFT JOIN keeps ALL rows from the left table, even if no match exists:
    SELECT
        Orders.OrderID,
        Orders.CustomerID,
        Customers.CustomerName,
        Customers.Region
    FROM Orders
    LEFT JOIN Customers
        ON Orders.CustomerID = Customers.CustomerID;
  2. If an OrderID has a CustomerID not in the Customers table, it still shows (with NULL for customer fields)
  3. Try finding orders with missing customers:
    SELECT
        Orders.OrderID,
        Orders.CustomerID,
        Customers.CustomerName
    FROM Orders
    LEFT JOIN Customers
        ON Orders.CustomerID = Customers.CustomerID
    WHERE Customers.CustomerName IS NULL;
    This finds "orphaned" orders - orders without matching customers!

🎯 JOIN Types Comparison:

  • INNER JOIN: Only rows with matches in BOTH tables (most common)
  • LEFT JOIN: ALL rows from left table + matching rows from right (right can be NULL)
  • RIGHT JOIN: ALL rows from right table + matching rows from left (left can be NULL)

✅ Expected Result: All orders, including those without matching customers (if any)

Exercise 4: Three-Table JOIN - Complete Order View

Step-by-Step Instructions:

  1. Join all three tables for a comprehensive view:
    SELECT
        Orders.OrderID,
        Customers.CustomerName,
        Customers.Region,
        Products.ProductName,
        Products.Category,
        Products.Price,
        Orders.Quantity,
        (Products.Price * Orders.Quantity) AS TotalPrice
    FROM Orders
    INNER JOIN Customers
        ON Orders.CustomerID = Customers.CustomerID
    INNER JOIN Products
        ON Orders.ProductID = Products.ProductID
    ORDER BY TotalPrice DESC
    LIMIT 30;
  2. This single query combines data from 3 tables!
  3. You can add WHERE clauses too:
    WHERE Customers.Region = 'West' AND Products.Category = 'Technology'

💡 Multiple JOINs:

  • Stack JOINs one after another
  • Each JOIN connects to the previous result
  • You can join as many tables as needed
  • This is database normalization in action!

✅ Expected Result: Complete order information with customer and product details in one query

Exercise 5: JOIN + GROUP BY - Aggregate Across Tables

Step-by-Step Instructions:

  1. Calculate total sales per customer using JOIN + GROUP BY:
    SELECT
        Customers.CustomerName,
        Customers.Region,
        COUNT(Orders.OrderID) AS TotalOrders,
        SUM(Products.Price * Orders.Quantity) AS TotalSales
    FROM Orders
    INNER JOIN Customers
        ON Orders.CustomerID = Customers.CustomerID
    INNER JOIN Products
        ON Orders.ProductID = Products.ProductID
    GROUP BY Customers.CustomerName, Customers.Region
    ORDER BY TotalSales DESC
    LIMIT 10;
  2. This finds your top 10 customers by total sales!
  3. Try these variations:
    • Total sales by Region: GROUP BY Customers.Region
    • Total sales by Category: GROUP BY Products.Category
    • Top products: GROUP BY Products.ProductName

🎯 Combining Concepts:

This exercise combines:

  • Day 2: SELECT, aggregate functions (SUM, COUNT)
  • Day 4: WHERE clause (can add filters)
  • Day 6: JOINs (connecting tables)
  • Plus: GROUP BY (we'll cover more in Day 8)

✅ Expected Result: Customer ranking by total sales value

🎯 Practice Goal Checkpoint:

By completing these 5 exercises, you should now be able to:

  • Use INNER JOIN to combine data from multiple tables
  • Understand the ON clause (matching condition)
  • Use LEFT JOIN to include all rows from one table
  • Join three or more tables in a single query
  • Combine JOINs with WHERE, GROUP BY, and aggregates
  • See the parallel between SQL JOINs and Excel VLOOKUPs

Key Insight: JOINs are how databases connect related data. Instead of storing everything in one giant table (like Excel), we normalize data into separate tables and JOIN them when needed. This is more efficient and reduces data duplication!

Excel vs SQL Data Combination:

  • Excel (Day 5): VLOOKUP pulls one value at a time
  • SQL (Day 6): JOIN combines entire tables at once
  • Same goal, but SQL is more powerful for large datasets!

Take a 5-minute break, then move on to Independent Practice!

Independent Practice (60 min):

💡 Hints for Independent Practice:

  • Start simple: Begin with basic variations before attempting complex modifications
  • Break it down: Divide complex problems into smaller, manageable steps
  • Test incrementally: Verify each step works before moving to the next
  • Use documentation: Refer to official docs or tutorials when needed
  • Experiment: Try different approaches to find what works best
  1. List all orders with customer and product details (3-table JOIN)
  2. Find products that were never ordered (LEFT JOIN Products and Orders)
  3. Show orders with customer region information
  4. Calculate total revenue per product category (JOIN + SUM + GROUP BY)
  5. List customers with their order count (JOIN + COUNT + GROUP BY)
  6. Find high-value customers (total orders > $10,000)
  7. Show which products were sold in each region
  8. Display orders with customer state and product category
  9. Calculate average sales per customer
  10. Find top 5 products by total revenue

Daily Assignment (60 min):

Expected Outcomes:

✓ JOIN tables to combine related data
✓ Understand different JOIN types and when to use each
✓ Combine data from 3+ tables
✓ Submit: Day06_SQL_Practice.sql


📝 Day 6 Quick Quiz - Test Your Knowledge!

Q1. What does INNER JOIN return?

Q2. What's the difference between LEFT JOIN and RIGHT JOIN?

Q3. What keyword specifies the join condition?

Q4. What does a CROSS JOIN produce?

Q5. If Table A has 5 rows and Table B has 3 rows, how many rows does LEFT JOIN produce (all rows match)?

Have feedback or questions?