Bird
Raised Fist0
SQLquery~10 mins

Multiple LEFT JOINs in one query in SQL - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to select all columns from table1 and left join table2 on id.

SQL
SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.[1];
Drag options to blanks, or click blank then click option'
Aname
Bvalue
Cdate
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using a column that does not exist in table2.
Joining on columns with different meanings.
2fill in blank
medium

Complete the code to add a second LEFT JOIN to table3 on user_id.

SQL
SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id LEFT JOIN table3 ON table1.[1] = table3.user_id;
Drag options to blanks, or click blank then click option'
Aid
Bname
Cuser_id
Ddate
Attempts:
3 left
💡 Hint
Common Mistakes
Joining on columns with different data types.
Using a column that does not exist in table1.
3fill in blank
hard

Fix the error in the join condition to correctly join table4 on order_id.

SQL
SELECT * FROM table1 LEFT JOIN table4 ON table1.order_id = table4.[1];
Drag options to blanks, or click blank then click option'
Aorder_id
Borderid
Cid
Duser_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using a similar but incorrect column name like 'orderid'.
Joining on unrelated columns.
4fill in blank
hard

Fill both blanks to join table2 and table3 correctly on their respective keys.

SQL
SELECT * FROM table1 LEFT JOIN table2 ON table1.[1] = table2.[2] LEFT JOIN table3 ON table1.user_id = table3.user_id;
Drag options to blanks, or click blank then click option'
Aid
Buser_id
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up user_id and id columns.
Using columns that don't exist in the tables.
5fill in blank
hard

Fill all three blanks to join table1, table2, and table3 with correct keys and aliases.

SQL
SELECT t1.*, t2.info, t3.details FROM table1 AS t1 LEFT JOIN table2 AS t2 ON t1.[1] = t2.[2] LEFT JOIN table3 AS t3 ON t1.[3] = t3.user_id;
Drag options to blanks, or click blank then click option'
Aid
Cuser_id
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'name' instead of 'id' or 'user_id' for joins.
Mixing up aliases and table names.

Practice

(1/5)
1. What is the main purpose of using multiple LEFT JOIN clauses in a single SQL query?
easy
A. To delete rows from multiple tables at once
B. To combine rows from several tables while keeping all rows from the first table
C. To return only rows that have matching values in all joined tables
D. To update multiple tables simultaneously

Solution

  1. Step 1: Understand LEFT JOIN behavior

    A LEFT JOIN returns all rows from the left (first) table and matching rows from the right table. If no match, NULLs appear.
  2. Step 2: Apply to multiple LEFT JOINs

    Using multiple LEFT JOINs keeps all rows from the main table and adds data from each joined table if available.
  3. Final Answer:

    To combine rows from several tables while keeping all rows from the first table -> Option B
  4. Quick Check:

    Multiple LEFT JOINs keep all main table rows [OK]
Hint: LEFT JOIN keeps all main table rows even if others miss data [OK]
Common Mistakes:
  • Confusing LEFT JOIN with INNER JOIN which filters rows
  • Thinking LEFT JOIN deletes or updates data
  • Assuming NULLs mean errors instead of missing matches
2. Which of the following SQL queries correctly uses multiple LEFT JOINs to combine tables orders, customers, and payments?
easy
A. SELECT o.id, c.name, p.amount FROM orders o LEFT JOIN customers c ON o.customer_id = c.id LEFT JOIN payments p ON o.id = p.order_id;
B. SELECT o.id, c.name, p.amount FROM orders o JOIN customers c ON o.customer_id = c.id JOIN payments p ON o.id = p.order_id;
C. SELECT o.id, c.name, p.amount FROM orders o LEFT JOIN customers c ON c.id = o.customer_id RIGHT JOIN payments p ON p.order_id = o.id;
D. SELECT o.id, c.name, p.amount FROM orders o INNER JOIN customers c ON o.customer_id = c.id LEFT JOIN payments p ON o.id = p.order_id;

Solution

  1. Step 1: Check JOIN types

    SELECT o.id, c.name, p.amount FROM orders o LEFT JOIN customers c ON o.customer_id = c.id LEFT JOIN payments p ON o.id = p.order_id; uses LEFT JOIN twice, correctly keeping all orders and adding customer and payment info if available.
  2. Step 2: Identify errors in other options

    SELECT o.id, c.name, p.amount FROM orders o JOIN customers c ON o.customer_id = c.id JOIN payments p ON o.id = p.order_id; uses INNER JOINs, which exclude orders without customers or payments. SELECT o.id, c.name, p.amount FROM orders o LEFT JOIN customers c ON c.id = o.customer_id RIGHT JOIN payments p ON p.order_id = o.id; mixes LEFT and RIGHT JOIN incorrectly. SELECT o.id, c.name, p.amount FROM orders o INNER JOIN customers c ON o.customer_id = c.id LEFT JOIN payments p ON o.id = p.order_id; uses INNER JOIN first, losing unmatched orders.
  3. Final Answer:

    SELECT o.id, c.name, p.amount FROM orders o LEFT JOIN customers c ON o.customer_id = c.id LEFT JOIN payments p ON o.id = p.order_id; -> Option A
  4. Quick Check:

    Multiple LEFT JOINs keep all orders [OK]
Hint: Use LEFT JOIN for all tables to keep main table rows [OK]
Common Mistakes:
  • Using INNER JOIN instead of LEFT JOIN loses unmatched rows
  • Mixing JOIN types without clear logic
  • Incorrect ON conditions causing wrong matches
3. Given tables:
users(id, name),
posts(id, user_id, title),
comments(id, post_id, content)
What will this query return?
SELECT u.name, p.title, c.content FROM users u LEFT JOIN posts p ON u.id = p.user_id LEFT JOIN comments c ON p.id = c.post_id ORDER BY u.id, p.id, c.id;
medium
A. All posts with their users and comments; excludes users without posts
B. Only users who have posts and comments
C. All users with their posts and comments; NULLs if no posts or comments
D. Only comments with matching posts and users

Solution

  1. Step 1: Analyze first LEFT JOIN

    The first LEFT JOIN keeps all users, adding posts if they exist; users without posts get NULLs for post columns.
  2. Step 2: Analyze second LEFT JOIN

    The second LEFT JOIN adds comments for each post; if no comments, comment columns are NULL. Users without posts have NULL posts, so comments also NULL.
  3. Final Answer:

    All users with their posts and comments; NULLs if no posts or comments -> Option C
  4. Quick Check:

    LEFT JOINs keep all users, add posts and comments if present [OK]
Hint: LEFT JOIN keeps all left table rows, fills NULLs if no matches [OK]
Common Mistakes:
  • Assuming INNER JOIN behavior filters rows
  • Thinking NULLs mean errors instead of missing data
  • Ignoring order of JOINs affecting results
4. Identify the error in this SQL query using multiple LEFT JOINs:
SELECT a.id, b.name, c.status FROM tableA a LEFT JOIN tableB b ON a.id = b.a_id LEFT JOIN tableC c ON b.id = c.b_id WHERE c.status = 'active';
medium
A. The SELECT clause must include all columns from joined tables
B. The JOIN conditions are missing ON clauses
C. Using LEFT JOIN twice is not allowed
D. The WHERE clause filters out rows where c.status is NULL, negating LEFT JOIN effect

Solution

  1. Step 1: Understand WHERE with LEFT JOIN

    The WHERE clause filters rows after JOINs. Filtering on c.status excludes rows where c is NULL, removing unmatched rows.
  2. Step 2: Effect on LEFT JOIN

    This makes the LEFT JOIN behave like INNER JOIN, losing rows from tableA without matching tableC rows.
  3. Final Answer:

    The WHERE clause filters out rows where c.status is NULL, negating LEFT JOIN effect -> Option D
  4. Quick Check:

    Filtering on joined table in WHERE breaks LEFT JOIN [OK]
Hint: Use WHERE on main table only; filter joined tables in ON [OK]
Common Mistakes:
  • Filtering joined table columns in WHERE instead of ON
  • Assuming multiple LEFT JOINs are invalid
  • Missing ON conditions causing cross joins
5. You have tables:
employees(id, name, department_id),
departments(id, dept_name),
projects(id, dept_id, project_name),
tasks(id, project_id, task_name).
Write a query to list all employees with their department, projects, and tasks. Include employees even if they have no department, projects, or tasks. Which query achieves this?
hard
A. SELECT e.name, d.dept_name, p.project_name, t.task_name FROM employees e LEFT JOIN departments d ON e.department_id = d.id LEFT JOIN projects p ON d.id = p.dept_id LEFT JOIN tasks t ON p.id = t.project_id;
B. SELECT e.name, d.dept_name, p.project_name, t.task_name FROM employees e LEFT JOIN departments d ON e.id = d.id LEFT JOIN projects p ON d.id = p.dept_id LEFT JOIN tasks t ON p.id = t.project_id;
C. SELECT e.name, d.dept_name, p.project_name, t.task_name FROM employees e INNER JOIN departments d ON e.id = d.id INNER JOIN projects p ON d.id = p.dept_id INNER JOIN tasks t ON p.id = t.project_id;
D. SELECT e.name, d.dept_name, p.project_name, t.task_name FROM employees e LEFT JOIN departments d ON e.id = d.id LEFT JOIN projects p ON e.id = p.dept_id LEFT JOIN tasks t ON p.id = t.project_id;

Solution

  1. Step 1: Identify correct JOIN keys

    Employees link to departments by e.department_id = d.id, not e.id = d.id. Projects link to departments by dept_id, tasks to projects by project_id.
  2. Step 2: Use LEFT JOINs to keep all employees

    LEFT JOINs keep all employees even if no department, projects, or tasks exist.
  3. Step 3: Check each option

    SELECT e.name, d.dept_name, p.project_name, t.task_name FROM employees e LEFT JOIN departments d ON e.department_id = d.id LEFT JOIN projects p ON d.id = p.dept_id LEFT JOIN tasks t ON p.id = t.project_id; uses correct keys and LEFT JOINs. Other queries use wrong keys (joining on e.id = d.id or e.id = p.dept_id) or INNER JOINs, excluding employees without matches.
  4. Final Answer:

    SELECT e.name, d.dept_name, p.project_name, t.task_name FROM employees e LEFT JOIN departments d ON e.department_id = d.id LEFT JOIN projects p ON d.id = p.dept_id LEFT JOIN tasks t ON p.id = t.project_id; -> Option A
  5. Quick Check:

    Correct keys + LEFT JOINs keep all employees [OK]
Hint: Match keys correctly and use LEFT JOINs to keep all main rows [OK]
Common Mistakes:
  • Joining on wrong columns causing missing data
  • Using INNER JOINs losing employees without projects
  • Confusing employee ID with department ID