Bird
Raised Fist0
SQLquery~10 mins

Multiple LEFT JOINs in one query 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 - Multiple LEFT JOINs in one query
Start with main table
LEFT JOIN first table
Combine rows, keep all from main
LEFT JOIN second table
Combine rows, keep all from main
Result with all rows from main and matching rows from joined tables
END
Start with a main table, then add rows from other tables using LEFT JOINs one by one, keeping all rows from the main table even if no match is found.
Execution Sample
SQL
SELECT A.id, B.name, C.status
FROM A
LEFT JOIN B ON A.id = B.a_id
LEFT JOIN C ON A.id = C.a_id;
This query selects data from table A and adds matching data from tables B and C using LEFT JOINs.
Execution Table
StepActionTables InvolvedRows ResultingExplanation
1Start with table AA3 rowsInitial rows from A: ids 1, 2, 3
2LEFT JOIN B on A.id = B.a_idA, B3 rowsRows from A kept; B data added where matches exist (id=1,2) else NULL
3LEFT JOIN C on A.id = C.a_idA, B, C3 rowsRows from previous step kept; C data added where matches exist (id=2,3) else NULL
4Final resultA, B, C3 rowsAll rows from A with matching B and C data or NULLs where no match
💡 All rows from A are included; LEFT JOINs add matching rows from B and C or NULL if no match
Variable Tracker
VariableStartAfter Step 2After Step 3Final
Rows3 rows from A3 rows with B data or NULL3 rows with B and C data or NULL3 rows with combined data
Key Moments - 2 Insights
Why do we still have 3 rows after the LEFT JOINs even if some joined tables have no matching rows?
Because LEFT JOIN keeps all rows from the main table (A) regardless of matches in joined tables, as shown in execution_table rows 2 and 3.
What happens to columns from joined tables when there is no matching row?
Those columns show NULL values, as seen in execution_table row 2 for id=3 in table B and row 3 for id=1 in table C.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, how many rows are in the result after the first LEFT JOIN?
A2 rows
B3 rows
C5 rows
D0 rows
💡 Hint
Check execution_table row 2 under 'Rows Resulting'
At which step do columns from table C get added to the result?
AStep 1
BStep 2
CStep 3
DFinal step only
💡 Hint
Look at execution_table row 3 'Action' column
If table B had no matching rows for any A.id, what would happen to the number of rows after the first LEFT JOIN?
ANumber of rows would stay the same
BNumber of rows would increase
CNumber of rows would decrease
DQuery would fail
💡 Hint
LEFT JOIN keeps all rows from the main table as shown in execution_table row 2
Concept Snapshot
Multiple LEFT JOINs combine one main table with several others.
Syntax: SELECT ... FROM main LEFT JOIN table1 ON ... LEFT JOIN table2 ON ...
All rows from main table stay in result.
Joined tables add matching data or NULL if no match.
Useful to gather related info without losing main rows.
Full Transcript
This visual execution shows how multiple LEFT JOINs work in SQL. We start with a main table A with 3 rows. Then we LEFT JOIN table B on matching ids, keeping all rows from A and adding B's data where it matches, else NULL. Next, we LEFT JOIN table C similarly. The final result has all rows from A with matching data from B and C or NULLs if no match. Key points: LEFT JOIN keeps all main rows; unmatched joined columns become NULL. This helps combine data from multiple tables without losing main table rows.

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