Bird
Raised Fist0
SQLquery~5 mins

Multiple LEFT JOINs in one query in SQL

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
Introduction
We use multiple LEFT JOINs to combine data from several tables, even if some tables don't have matching records. This helps us see all main data with related details.
You want to list all customers and their orders, even if some customers have no orders.
You need to show all employees with their department and project info, even if some employees are not assigned to projects.
You want to display all products with their categories and suppliers, even if some products lack category or supplier info.
Syntax
SQL
SELECT columns
FROM table1
LEFT JOIN table2 ON table1.key = table2.key
LEFT JOIN table3 ON table1.key = table3.key;
LEFT JOIN keeps all rows from the first (left) table, adding matching rows from joined tables or NULL if no match.
You can add as many LEFT JOINs as needed to include related data from multiple tables.
Examples
Shows all customers and their orders, including customers without orders.
SQL
SELECT customers.name, orders.id
FROM customers
LEFT JOIN orders ON customers.id = orders.customer_id;
Lists all employees with their department and project names, even if some info is missing.
SQL
SELECT employees.name, departments.name, projects.name
FROM employees
LEFT JOIN departments ON employees.department_id = departments.id
LEFT JOIN projects ON employees.project_id = projects.id;
Sample Program
This query lists all authors with their books and any reviews. Authors without books or books without reviews still appear with NULLs.
SQL
CREATE TABLE authors (id INT, name VARCHAR(20));
CREATE TABLE books (id INT, title VARCHAR(30), author_id INT);
CREATE TABLE reviews (id INT, book_id INT, rating INT);

INSERT INTO authors VALUES (1, 'Alice'), (2, 'Bob');
INSERT INTO books VALUES (1, 'Book A', 1), (2, 'Book B', 1), (3, 'Book C', 2);
INSERT INTO reviews VALUES (1, 1, 5), (2, 1, 4), (3, 3, 3);

SELECT authors.name AS author, books.title AS book, reviews.rating
FROM authors
LEFT JOIN books ON authors.id = books.author_id
LEFT JOIN reviews ON books.id = reviews.book_id
ORDER BY authors.name, books.title;
OutputSuccess
Important Notes
If a LEFT JOIN finds no matching row, it fills columns from that table with NULL.
Order of LEFT JOINs matters: each join uses the result of previous joins as its left table.
Use ORDER BY to organize results clearly when joining multiple tables.
Summary
Multiple LEFT JOINs let you combine data from many tables while keeping all rows from the main table.
They help show complete info even if some related data is missing.
Remember NULLs appear when no matching data exists in joined tables.

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