Bird
Raised Fist0
SQLquery~5 mins

Multiple LEFT JOINs in one query in SQL - Cheat Sheet & Quick Revision

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
Recall & Review
beginner
What does a LEFT JOIN do in SQL?
A LEFT JOIN returns all rows from the left table and the matched rows from the right table. If there is no match, the result is NULL on the right side.
Click to reveal answer
intermediate
How do multiple LEFT JOINs work in one SQL query?
Multiple LEFT JOINs add more tables to the query, joining each one to the previous result. Each join keeps all rows from the left side, adding matching rows or NULLs from the right side.
Click to reveal answer
beginner
Write a simple SQL query using two 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 a.id = c.a_id;
Click to reveal answer
beginner
Why might you get NULL values in columns from tables joined with LEFT JOIN?
Because LEFT JOIN keeps all rows from the left table, if there is no matching row in the right table, the columns from the right table will show NULL.
Click to reveal answer
intermediate
What is the difference between INNER JOIN and multiple LEFT JOINs?
INNER JOIN returns only rows with matches in both tables. Multiple LEFT JOINs return all rows from the left table and matching rows or NULLs from each joined table.
Click to reveal answer
What will a LEFT JOIN return if there is no matching row in the right table?
AAll rows from the left table with NULLs for the right table columns
BOnly matching rows from both tables
CAll rows from the right table
DNo rows
In a query with multiple LEFT JOINs, what happens if the second joined table has no matching rows?
AAn error occurs
BThe entire query returns no rows
COnly rows with matches in the second table appear
DRows from the left table still appear with NULLs for the second joined table
Which SQL keyword is used to join tables and keep all rows from the left table?
AINNER JOIN
BRIGHT JOIN
CLEFT JOIN
DFULL JOIN
If you want to join three tables and keep all rows from the first table, which join type should you use?
ALEFT JOIN for all
BINNER JOIN for all
CRIGHT JOIN for all
DFULL JOIN for all
What does the ON clause specify in a LEFT JOIN?
AThe columns to select
BThe condition to match rows between tables
CThe order of rows
DThe filter for the left table only
Explain how multiple LEFT JOINs work in a SQL query and why they might be useful.
Think about combining data from several tables while keeping all main records.
You got /4 concepts.
    Describe the difference between INNER JOIN and multiple LEFT JOINs in terms of returned rows.
    Focus on how unmatched rows are handled.
    You got /4 concepts.

      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