0
0
SQLquery~20 mins

Multiple LEFT JOINs in one query in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multiple LEFT JOIN Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of multiple LEFT JOINs with missing matches

Consider these tables:

Employees: (id, name)

Departments: (id, dept_name)

Projects: (id, project_name, dept_id)

Query:

SELECT e.name, d.dept_name, p.project_name
FROM Employees e
LEFT JOIN Departments d ON e.id = d.id
LEFT JOIN Projects p ON d.id = p.dept_id
ORDER BY e.id;

What is the output of this query?

A[{"name": "Alice", "dept_name": "HR", "project_name": "Recruitment"}, {"name": "Bob", "dept_name": null, "project_name": null}, {"name": "Charlie", "dept_name": "IT", "project_name": "Infrastructure"}]
B[{"name": "Alice", "dept_name": null, "project_name": null}, {"name": "Bob", "dept_name": null, "project_name": null}, {"name": "Charlie", "dept_name": null, "project_name": null}]
C[{"name": "Alice", "dept_name": "HR", "project_name": null}, {"name": "Bob", "dept_name": null, "project_name": null}, {"name": "Charlie", "dept_name": "IT", "project_name": null}]
D[{"name": "Alice", "dept_name": "HR", "project_name": "Recruitment"}, {"name": "Bob", "dept_name": "Finance", "project_name": "Budgeting"}, {"name": "Charlie", "dept_name": "IT", "project_name": "Infrastructure"}]
Attempts:
2 left
💡 Hint

Remember that LEFT JOIN keeps all rows from the left table even if there is no match on the right.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in multiple LEFT JOINs

Which option contains a syntax error in this SQL query with multiple LEFT JOINs?

SELECT a.id, b.value, 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;
A
LEFT JOIN TableB b ON a.id = b.a_id
LEFT JOIN TableC c ON a.id = c.a_id;
B
LEFT JOIN TableB b ON a.id = b.a_id
LEFT JOIN TableC c ON a.id = c.a_id AND b.status = 'active';
C
LEFT JOIN TableB b ON a.id = b.a_id
LEFT JOIN TableC c ON a.id = c.a_id
D
LEFT JOIN TableB b ON a.id = b.a_id
LEFT JOIN TableC c a.id = c.a_id;
Attempts:
2 left
💡 Hint

Look carefully at the ON clauses for each JOIN.

optimization
advanced
2:00remaining
Optimizing multiple LEFT JOINs for performance

You have a query joining three large tables with LEFT JOINs. Which option is the best way to optimize the query?

AAdd indexes on the columns used in the ON conditions for all LEFT JOINs.
BReplace all LEFT JOINs with INNER JOINs to reduce rows.
CUse SELECT * to avoid specifying columns and speed up the query.
DRemove all JOIN conditions to speed up the query.
Attempts:
2 left
💡 Hint

Think about how databases find matching rows efficiently.

🔧 Debug
advanced
2:00remaining
Why does this multiple LEFT JOIN query return duplicate rows?

Given these tables:

Authors: (id, name)

Books: (id, author_id, title)

Reviews: (id, book_id, rating)

Query:

SELECT a.name, b.title, r.rating
FROM Authors a
LEFT JOIN Books b ON a.id = b.author_id
LEFT JOIN Reviews r ON b.id = r.book_id;

Why might this query return duplicate rows for the same author and book?

ABecause the ON conditions are incorrect and cause cross joins.
BBecause LEFT JOINs always duplicate rows regardless of data.
CBecause each book can have multiple reviews, the join duplicates rows for each review.
DBecause the SELECT statement is missing DISTINCT keyword.
Attempts:
2 left
💡 Hint

Think about how many reviews a single book can have.

🧠 Conceptual
expert
2:00remaining
Understanding NULLs in multiple LEFT JOINs

In a query with multiple LEFT JOINs, what does a NULL value in a column from a right table indicate?

AThe right table column value is actually NULL in the database.
BThere was no matching row in the right table for the left table row.
CThe LEFT JOIN failed due to a syntax error.
DThe left table row was filtered out by a WHERE clause.
Attempts:
2 left
💡 Hint

Think about what LEFT JOIN does when no match is found.