Bird
Raised Fist0
SQLquery~20 mins

Joining more than two tables in SQL - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Master of Joining Multiple Tables
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of joining three tables with INNER JOIN
Given the tables Employees, Departments, and Locations, what is the output of the following SQL query?
SELECT e.name, d.department_name, l.city
FROM Employees e
INNER JOIN Departments d ON e.department_id = d.id
INNER JOIN Locations l ON d.location_id = l.id
ORDER BY e.name;
SQL
CREATE TABLE Employees (id INT, name VARCHAR(50), department_id INT);
CREATE TABLE Departments (id INT, department_name VARCHAR(50), location_id INT);
CREATE TABLE Locations (id INT, city VARCHAR(50));

INSERT INTO Employees VALUES (1, 'Alice', 10), (2, 'Bob', 20), (3, 'Charlie', 10);
INSERT INTO Departments VALUES (10, 'Sales', 100), (20, 'Engineering', 200);
INSERT INTO Locations VALUES (100, 'New York'), (200, 'San Francisco');
A[{'name': 'Alice', 'department_name': 'Sales', 'city': 'New York'}, {'name': 'Bob', 'department_name': 'Engineering', 'city': 'San Francisco'}, {'name': 'Charlie', 'department_name': 'Sales', 'city': 'New York'}]
B[{'name': 'Alice', 'department_name': 'Sales', 'city': 'San Francisco'}, {'name': 'Bob', 'department_name': 'Engineering', 'city': 'New York'}, {'name': 'Charlie', 'department_name': 'Sales', 'city': 'San Francisco'}]
C[{'name': 'Alice', 'department_name': 'Sales', 'city': 'New York'}, {'name': 'Charlie', 'department_name': 'Sales', 'city': 'New York'}]
D[{'name': 'Bob', 'department_name': 'Engineering', 'city': 'San Francisco'}]
Attempts:
2 left
💡 Hint
Think about how INNER JOIN works: it only includes rows where the join condition matches in all tables.
📝 Syntax
intermediate
1:30remaining
Identify the syntax error in joining three tables
Which option contains a syntax error when joining three tables Orders, Customers, and Products to get order details?
SQL
SELECT o.order_id, c.customer_name, p.product_name
FROM Orders o
JOIN Customers c ON o.customer_id = c.id
JOIN Products p ON o.product_id = p.id;
ASELECT o.order_id, c.customer_name, p.product_name FROM Orders o INNER JOIN Customers c ON o.customer_id = c.id JOIN Products p ON o.product_id = p.id;
BSELECT o.order_id, c.customer_name, p.product_name FROM Orders o JOIN Customers c ON o.customer_id = c.id JOIN Products p o.product_id = p.id;
CSELECT o.order_id, c.customer_name, p.product_name FROM Orders o JOIN Customers c ON o.customer_id = c.id INNER JOIN Products p ON o.product_id = p.id;
DSELECT o.order_id, c.customer_name, p.product_name FROM Orders o JOIN Customers c ON o.customer_id = c.id JOIN Products p ON o.product_id = p.id;
Attempts:
2 left
💡 Hint
Check the JOIN syntax carefully, especially the ON clause.
optimization
advanced
2:30remaining
Optimizing a query joining four tables
You have four tables: Sales, Customers, Products, and Stores. Which query is the most efficient to get sales details including customer name, product name, and store location?
ASELECT s.sale_id, c.name, p.name, st.location FROM Sales s LEFT JOIN Customers c ON s.customer_id = c.id LEFT JOIN Products p ON s.product_id = p.id LEFT JOIN Stores st ON s.store_id = st.id;
BSELECT s.sale_id, c.name, p.name, st.location FROM Sales s, Customers c, Products p, Stores st WHERE s.customer_id = c.id AND s.product_id = p.id AND s.store_id = st.id;
CSELECT s.sale_id, c.name, p.name, st.location FROM Sales s JOIN Customers c ON s.customer_id = c.id JOIN Products p ON s.product_id = p.id JOIN Stores st ON s.store_id = st.id;
DSELECT s.sale_id, c.name, p.name, st.location FROM Sales s CROSS JOIN Customers c CROSS JOIN Products p CROSS JOIN Stores st WHERE s.customer_id = c.id AND s.product_id = p.id AND s.store_id = st.id;
Attempts:
2 left
💡 Hint
Consider the clarity and performance of JOIN syntax versus older comma joins and CROSS JOINs.
🧠 Conceptual
advanced
1:30remaining
Understanding join types with three tables
You join three tables: A, B, and C. If you want to keep all rows from A even if there are no matching rows in B or C, which join type should you use?
AUse INNER JOIN between A and B, then INNER JOIN between B and C.
BUse FULL OUTER JOIN between A and B, then FULL OUTER JOIN between B and C.
CUse RIGHT JOIN from A to B, then RIGHT JOIN from B to C.
DUse LEFT JOIN from A to B, then LEFT JOIN from B to C.
Attempts:
2 left
💡 Hint
Think about which join keeps all rows from the left table regardless of matches.
🔧 Debug
expert
2:30remaining
Debugging incorrect results from joining three tables
You run this query joining three tables but get fewer rows than expected:
SELECT e.name, d.department_name, p.project_name
FROM Employees e
JOIN Departments d ON e.department_id = d.id
JOIN Projects p ON e.id = p.employee_id
WHERE d.location = 'New York';

What is the most likely reason for missing rows?
ASome employees in New York departments have no projects, so INNER JOIN excludes them.
BThe WHERE clause filters out employees not in New York, causing no rows to show.
CThe JOIN condition between Employees and Departments is incorrect, causing no matches.
DThe Projects table has no employee_id column, causing a syntax error.
Attempts:
2 left
💡 Hint
Consider how INNER JOIN behaves when there is no matching row in the joined table.

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