0
0
SQLquery~10 mins

Joining more than two tables in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Joining more than two tables
Start with Table1
Join Table2 on matching key
Result: Combined Table1+Table2
Join Table3 on matching key
Final Result: Combined Table1+Table2+Table3
Start by joining the first two tables on a common column, then join the third table to the result using another matching column.
Execution Sample
SQL
SELECT A.id, A.name, B.order_id, C.product_name
FROM Customers A
JOIN Orders B ON A.id = B.customer_id
JOIN Products C ON B.product_id = C.id;
This query joins three tables: Customers, Orders, and Products to get customer info, their orders, and product names.
Execution Table
StepActionTables InvolvedMatching ConditionResult Rows
1Start with Customers tableCustomersN/A3 rows (Alice, Bob, Carol)
2Join Orders on Customers.id = Orders.customer_idCustomers + OrdersCustomers.id = Orders.customer_id4 rows (matching orders)
3Join Products on Orders.product_id = Products.idCustomers + Orders + ProductsOrders.product_id = Products.id4 rows (final combined)
4Return final result with selected columnsAll three tablesN/A4 rows with customer, order, product info
💡 All matching rows combined; no more tables to join.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
Result Rows3 (Customers)4 (Customers + Orders)4 (Customers + Orders + Products)4 (Final combined rows)
Key Moments - 3 Insights
Why does the number of rows increase after the first join?
Because one customer can have multiple orders, the join duplicates customer rows for each matching order (see execution_table step 2).
Why do we join Products last?
We join Products last to add product details to each order. This step uses the product_id from Orders to match Products (execution_table step 3).
What happens if a customer has no orders?
In this INNER JOIN example, customers without orders are excluded because the join requires matching rows (not shown in this example).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, how many rows are there after joining Customers and Orders?
A3 rows
B7 rows
C4 rows
D1 row
💡 Hint
Check the 'Result Rows' column in execution_table at Step 2.
At which step does the Products table get joined?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Action' column in execution_table for when Products is joined.
If a customer has no orders, what happens to that customer in this query?
AThey appear with NULL order and product info
BThey are excluded from the result
CThey appear multiple times
DThe query returns an error
💡 Hint
Recall the join type is INNER JOIN, which only includes matching rows (see key_moments).
Concept Snapshot
Joining more than two tables:
Use multiple JOIN clauses.
Each JOIN connects two tables on matching columns.
Result grows as tables combine.
INNER JOIN excludes non-matching rows.
Order of joins matters for clarity.
Full Transcript
This visual execution shows how to join more than two tables in SQL. We start with the Customers table, then join Orders using the customer ID, increasing rows when customers have multiple orders. Next, we join Products using the product ID from Orders to add product names. The final result combines all three tables' data. The execution table tracks each step, showing how rows increase and how tables combine. Key moments clarify why rows increase after joins and what happens to customers without orders. The quiz tests understanding of row counts and join order. Remember, INNER JOIN only includes rows with matches in all tables.