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:
-
SQL GROUP BY Explained for Beginners (8:41)
https://www.youtube.com/watch?v=MXK83I9bTrU -
SQL Aggregate Functions & GROUP BY (12:58)
https://www.youtube.com/watch?v=5IuU8mxC3iE -
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:
- Open SQLite Browser with your database
- You should have 3 tables: Orders, Customers, Products
- Orders table: OrderID, CustomerID, ProductID, Quantity, OrderDate
- Customers table: CustomerID, CustomerName, Region, Email
- 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:
- 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; - Click Execute (▶️)
- 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:
- 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; - Notice we calculated TotalPrice directly in the query!
- 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:
- 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; - If an OrderID has a CustomerID not in the Customers table, it still shows (with NULL for customer fields)
- Try finding orders with missing customers:
This finds "orphaned" orders - orders without matching customers!SELECT Orders.OrderID, Orders.CustomerID, Customers.CustomerName FROM Orders LEFT JOIN Customers ON Orders.CustomerID = Customers.CustomerID WHERE Customers.CustomerName IS NULL;
🎯 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:
- 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; - This single query combines data from 3 tables!
- 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:
- 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; - This finds your top 10 customers by total sales!
- 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
- Total sales by Region:
🎯 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
- List all orders with customer and product details (3-table JOIN)
- Find products that were never ordered (LEFT JOIN Products and Orders)
- Show orders with customer region information
- Calculate total revenue per product category (JOIN + SUM + GROUP BY)
- List customers with their order count (JOIN + COUNT + GROUP BY)
- Find high-value customers (total orders > $10,000)
- Show which products were sold in each region
- Display orders with customer state and product category
- Calculate average sales per customer
- Find top 5 products by total revenue
Daily Assignment (60 min):
- Create comprehensive sales report: Orders + Customers + Products
- This mirrors your Excel Day 5 VLOOKUP assignment!
- Include: Customer names, regions, product details, totals
- Save as: Day06_SQL_Practice.sql
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)?