Bird
Raised Fist0
SQLquery~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of joining more than two tables in SQL?
easy
A. To combine related data from multiple tables into one result set
B. To delete data from multiple tables at once
C. To create new tables automatically
D. To backup tables in the database

Solution

  1. Step 1: Understand the concept of JOIN

    JOIN is used to combine rows from two or more tables based on related columns.
  2. Step 2: Apply to multiple tables

    Joining more than two tables extends this idea to combine data from several tables into one result.
  3. Final Answer:

    To combine related data from multiple tables into one result set -> Option A
  4. Quick Check:

    Joining multiple tables = combine data [OK]
Hint: Joining means combining data from tables step-by-step [OK]
Common Mistakes:
  • Thinking JOIN deletes or creates tables
  • Confusing JOIN with backup or delete operations
  • Assuming JOIN works without conditions
2. Which of the following is the correct syntax to join three tables A, B, and C on columns A.id = B.a_id and B.id = C.b_id?
easy
A. SELECT * FROM A, B, C WHERE A.id = B.a_id, B.id = C.b_id;
B. SELECT * FROM A JOIN B ON A.id = B.a_id, C ON B.id = C.b_id;
C. SELECT * FROM A JOIN B JOIN C ON A.id = B.a_id AND B.id = C.b_id;
D. SELECT * FROM A JOIN B ON A.id = B.a_id JOIN C ON B.id = C.b_id;

Solution

  1. Step 1: Check JOIN syntax for multiple tables

    Each JOIN must have its own ON condition to specify how tables connect.
  2. Step 2: Validate SELECT * FROM A JOIN B ON A.id = B.a_id JOIN C ON B.id = C.b_id;

    SELECT * FROM A JOIN B ON A.id = B.a_id JOIN C ON B.id = C.b_id; correctly joins A to B with ON, then B to C with ON separately.
  3. Final Answer:

    SELECT * FROM A JOIN B ON A.id = B.a_id JOIN C ON B.id = C.b_id; -> Option D
  4. Quick Check:

    Each JOIN needs its own ON condition [OK]
Hint: Use separate ON for each JOIN clause [OK]
Common Mistakes:
  • Combining multiple ON conditions in one JOIN
  • Using commas with JOIN incorrectly
  • Missing ON clause for a JOIN
3. Given tables:
Students(id, name),
Enrollments(student_id, course_id),
Courses(id, title)
What will the query below return?
SELECT Students.name, Courses.title FROM Students JOIN Enrollments ON Students.id = Enrollments.student_id JOIN Courses ON Enrollments.course_id = Courses.id;
medium
A. List of all students and all courses regardless of enrollment
B. List of student names with the titles of courses they are enrolled in
C. List of courses with no students enrolled
D. Syntax error due to missing WHERE clause

Solution

  1. Step 1: Analyze JOINs in the query

    Students join Enrollments on student ID, then Enrollments join Courses on course ID, linking students to their courses.
  2. Step 2: Understand SELECT output

    The query selects student names and course titles for matching enrollments, showing which student is in which course.
  3. Final Answer:

    List of student names with the titles of courses they are enrolled in -> Option B
  4. Quick Check:

    JOINs link students to their courses [OK]
Hint: JOIN chains link related data stepwise [OK]
Common Mistakes:
  • Thinking JOIN returns all combinations without conditions
  • Expecting courses without students to appear
  • Assuming WHERE is required for JOIN
4. Identify the error in the following SQL query joining three tables:
SELECT * FROM A JOIN B ON A.id = B.a_id JOIN C ON A.id = C.a_id;
medium
A. Missing WHERE clause for filtering
B. No error, query is correct
C. JOIN condition for table C should use B's columns, not A's
D. JOIN keyword is missing before table C

Solution

  1. Step 1: Review JOIN conditions

    First JOIN connects A and B on A.id = B.a_id, which is correct.
  2. Step 2: Check second JOIN condition

    Second JOIN connects C using A.id = C.a_id, but logically C should join via B, not A, to maintain correct relationships.
  3. Final Answer:

    JOIN condition for table C should use B's columns, not A's -> Option C
  4. Quick Check:

    JOIN conditions must link correct tables [OK]
Hint: Check JOIN ON uses correct table columns [OK]
Common Mistakes:
  • Using wrong table columns in JOIN condition
  • Assuming WHERE is needed for JOIN
  • Ignoring logical table relationships
5. You have tables:
Orders(order_id, customer_id),
Customers(customer_id, name),
Payments(payment_id, order_id, amount).
Write a query to find each customer's name and the total amount they paid across all orders. Which query is correct?
hard
A. SELECT Customers.name, SUM(Payments.amount) FROM Customers JOIN Orders ON Customers.customer_id = Orders.customer_id JOIN Payments ON Orders.order_id = Payments.order_id GROUP BY Customers.name;
B. SELECT Customers.name, Payments.amount FROM Customers JOIN Orders ON Customers.customer_id = Orders.customer_id JOIN Payments ON Orders.order_id = Payments.order_id;
C. SELECT Customers.name, SUM(Payments.amount) FROM Customers, Orders, Payments WHERE Customers.customer_id = Orders.customer_id AND Orders.order_id = Payments.order_id;
D. SELECT Customers.name, SUM(Payments.amount) FROM Customers JOIN Payments ON Customers.customer_id = Payments.order_id GROUP BY Customers.name;

Solution

  1. Step 1: Understand the relationships

    Customers link to Orders by customer_id; Orders link to Payments by order_id.
  2. Step 2: Check aggregation and grouping

    We need total payment per customer, so SUM and GROUP BY Customers.name are required.
  3. Step 3: Validate options

    SELECT Customers.name, SUM(Payments.amount) FROM Customers JOIN Orders ON Customers.customer_id = Orders.customer_id JOIN Payments ON Orders.order_id = Payments.order_id GROUP BY Customers.name; correctly joins tables and groups by customer name with SUM of payments. SELECT Customers.name, Payments.amount FROM Customers JOIN Orders ON Customers.customer_id = Orders.customer_id JOIN Payments ON Orders.order_id = Payments.order_id; lacks aggregation. SELECT Customers.name, SUM(Payments.amount) FROM Customers, Orders, Payments WHERE Customers.customer_id = Orders.customer_id AND Orders.order_id = Payments.order_id; misses GROUP BY. SELECT Customers.name, SUM(Payments.amount) FROM Customers JOIN Payments ON Customers.customer_id = Payments.order_id GROUP BY Customers.name; joins wrong columns.
  4. Final Answer:

    SELECT Customers.name, SUM(Payments.amount) FROM Customers JOIN Orders ON Customers.customer_id = Orders.customer_id JOIN Payments ON Orders.order_id = Payments.order_id GROUP BY Customers.name; -> Option A
  5. Quick Check:

    JOIN + SUM + GROUP BY = correct total per customer [OK]
Hint: Use JOINs with GROUP BY and SUM for totals [OK]
Common Mistakes:
  • Missing GROUP BY when using SUM
  • Joining tables on wrong columns
  • Selecting aggregated and non-aggregated columns without GROUP BY