ποΈ SQL Β· Topic 12 of 20
OUTER JOINs β SQL Questions with Answers
30 practice questions on OUTER JOINs, each with the correct answer and a clear explanation of why it is right. Read through them as revision notes, or take this topic as an interactive quiz to test yourself.
1 easy Β· 14 medium Β· 15 hard Β· Part of SQL: Databases from basics to window functions - 20 topics.
Medium questions (14)
Q2. medium
In 'orders LEFT JOIN customers', which table keeps all its rows?
- A orders - the LEFT (first) table
- B customers - the right table
- C Both tables
- D Neither
Answer: A β orders - the LEFT (first) table
LEFT JOIN keeps everything from the table on the left of the keyword. Every order appears, even those whose customer_id has no matching customer.
LEFT JOINDirection
Q3. medium
What does RIGHT JOIN return?
- A All rows from the RIGHT table, plus matching left-table columns (NULL where no match)
- B Only matches
- C All left rows
- D Nothing
Answer: A β All rows from the RIGHT table, plus matching left-table columns (NULL where no match)
RIGHT JOIN is the mirror of LEFT JOIN. In practice most people rewrite RIGHT joins as LEFT joins by swapping table order, because left-to-right reads more naturally.
RIGHT JOIN
Q5. medium
What does FULL OUTER JOIN return?
- A ALL rows from BOTH tables - matched where possible, NULLs filled in where not
- B Only matching rows
- C Only unmatched rows
- D The left table only
Answer: A β ALL rows from BOTH tables - matched where possible, NULLs filled in where not
FULL OUTER JOIN is the union of LEFT and RIGHT: every unmatched row from either side is kept. (MySQL lacks it natively; emulate with LEFT JOIN UNION RIGHT JOIN.)
FULL OUTER JOIN
Q10. medium
What does this count?
SELECT c.name, COUNT(o.id) FROM customers c LEFT JOIN orders o ON c.id = o.customer_id GROUP BY c.name;
- A Every customer's order count, showing 0 for customers with no orders
- B Only customers with orders
- C Total orders
- D An error
Answer: A β Every customer's order count, showing 0 for customers with no orders
COUNT(o.id) counts non-NULL order ids, so unmatched customers correctly show 0. Note COUNT(*) would show 1 for them (the single NULL-padded row), which is a subtle trap.
LEFT JOINCOUNT
Q12. medium
What does a LEFT JOIN return when the left table is empty?
- A Zero rows - there are no left rows to preserve
- B All right rows
- C One NULL row
- D An error
Answer: A β Zero rows - there are no left rows to preserve
LEFT JOIN preserves LEFT rows; with none, the result is empty regardless of the right table's contents.
LEFT JOINEmpty tables
Q14. medium
Does the ORDER of tables matter for LEFT JOIN?
- A Yes - LEFT JOIN is NOT symmetric; A LEFT JOIN B differs from B LEFT JOIN A
- B No, it is symmetric
- C Only for three tables
- D Only with WHERE
Answer: A β Yes - LEFT JOIN is NOT symmetric; A LEFT JOIN B differs from B LEFT JOIN A
The left table's rows are always preserved, so swapping the tables changes which side keeps its unmatched rows. This is a key difference from INNER JOIN.
LEFT JOINOrder
Q16. medium
How do you show all products and their total sales, INCLUDING never-sold products?
SELECT p.title, COALESCE(SUM(i.quantity), 0) AS sold
FROM products p
LEFT JOIN order_items i ON p.id = i.product_id
GROUP BY p.title;
- A LEFT JOIN from products to order_items, then SUM with COALESCE for zero
- B INNER JOIN products to order_items
- C RIGHT JOIN order_items
- D CROSS JOIN
Answer: A β LEFT JOIN from products to order_items, then SUM with COALESCE for zero
Starting from products and LEFT JOINing sales keeps every product. Wrap the sum: COALESCE(SUM(i.quantity), 0) so unsold products show 0 rather than NULL.
LEFT JOINCOALESCE
Q18. medium
What is a common real-world use of LEFT JOIN?
- A Listing all items and their optional related data - e.g. all users with their last login (or none)
- B Deleting orphan rows
- C Speeding up queries
- D Sorting tables
Answer: A β Listing all items and their optional related data - e.g. all users with their last login (or none)
Whenever the related data is optional - a profile photo, a last order, a manager - LEFT JOIN ensures the main entity is not dropped just because the optional part is missing.
LEFT JOINOptional data
Q20. medium
What does the term 'outer' mean in outer join?
- A Rows OUTSIDE the matched set (unmatched rows) are preserved, unlike inner join
- B It joins external databases
- C It refers to the outermost query
- D Nothing specific
Answer: A β Rows OUTSIDE the matched set (unmatched rows) are preserved, unlike inner join
INNER keeps only the intersection; OUTER additionally keeps the 'outer' unmatched rows from one side (LEFT/RIGHT) or both (FULL).
Outer joinsTerminology
Q22. medium
What appears in the right-table columns for an unmatched LEFT JOIN row?
- A NULL
- B 0
- C Empty string
- D The default value
Answer: A β NULL
Every right column is NULL when there is no match. This is why you test for NULL to find unmatched rows, and use COALESCE to supply display defaults.
NULLLEFT JOIN
Q24. medium
Can you LEFT JOIN more than two tables?
- A Yes - chain them, keeping in mind each LEFT preserves rows accumulated so far
- B No, only two
- C Only with FULL join
- D Only in subqueries
Answer: A β Yes - chain them, keeping in mind each LEFT preserves rows accumulated so far
Multiple LEFT JOINs are common (a user, their optional profile, their optional settings). Be careful that a later INNER JOIN does not filter out preserved rows.
Join chainsLEFT JOIN
Q26. medium
Which is clearer to read: LEFT JOIN or RIGHT JOIN?
- A LEFT JOIN - reading left to right, the 'main' table comes first
- B RIGHT JOIN always
- C They are equally clear
- D FULL JOIN
Answer: A β LEFT JOIN - reading left to right, the 'main' table comes first
Convention favours LEFT JOIN so the preserved (main) table leads the FROM clause. Consistent direction makes complex queries easier to follow.
StyleReadability
Q28. medium
What does COALESCE(o.status, 'no orders') do in a LEFT JOIN?
- A Displays 'no orders' for unmatched customers instead of NULL
- B Filters out NULLs
- C Counts orders
- D Sorts the result
Answer: A β Displays 'no orders' for unmatched customers instead of NULL
COALESCE gives friendly labels to the NULLs that outer joins produce - much nicer in a report than blank cells.
COALESCELEFT JOIN
Q30. medium
When should you choose LEFT JOIN over INNER JOIN?
- A When you must keep ALL rows of the main table even if the related data is missing
- B Always
- C Never - INNER is always better
- D Only for two tables
Answer: A β When you must keep ALL rows of the main table even if the related data is missing
If missing related data should hide the main row, use INNER. If the main row must always appear (all customers, all products), use LEFT. Choosing correctly is the heart of join design.
LEFT JOININNER JOINDesign
Hard questions (15)
Q4. hard
How can any RIGHT JOIN be rewritten?
- A As a LEFT JOIN with the two tables swapped
- B As an INNER JOIN
- C As a CROSS JOIN
- D It cannot be rewritten
Answer: A β As a LEFT JOIN with the two tables swapped
A RIGHT JOIN B equals B LEFT JOIN A. Because LEFT joins are more intuitive, many teams standardise on them and avoid RIGHT joins entirely.
LEFT JOINRIGHT JOIN
Q6. hard
How do you find customers who have placed NO orders (an anti-join)?
SELECT c.name
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id
WHERE o.id IS NULL;
- A LEFT JOIN orders and keep rows WHERE o.id IS NULL
- B INNER JOIN and count zero
- C RIGHT JOIN customers
- D It is impossible
Answer: A β LEFT JOIN orders and keep rows WHERE o.id IS NULL
A LEFT JOIN gives NULLs for unmatched right rows; filtering for those NULLs finds the 'orphans'. This is the anti-join pattern - a very common interview question.
- Filter on the right table's PRIMARY KEY being NULL for reliability.
Anti-joinLEFT JOINNULL
Q7. hard
In an anti-join, which column should you test for IS NULL?
- A The right table's PRIMARY KEY (or a NOT NULL column) - a nullable column could be NULL even when matched
- B Any column works
- C The left table's key
- D The join column of the left table
Answer: A β The right table's PRIMARY KEY (or a NOT NULL column) - a nullable column could be NULL even when matched
If you test a nullable right column, a matched row that happens to store NULL there would be falsely flagged as unmatched. The primary key is never NULL in a real match.
Anti-joinNULL
Q8. hard
Why does putting a right-table filter in WHERE 'break' a LEFT JOIN?
LEFT JOIN orders o ON ... WHERE o.status = 'paid'
LEFT JOIN orders o ON c.id = o.customer_id AND o.status = 'paid'
- A The WHERE removes the NULL rows, silently turning the LEFT JOIN into an INNER JOIN
- B It causes a syntax error
- C It has no effect
- D It duplicates rows
Answer: A β The WHERE removes the NULL rows, silently turning the LEFT JOIN into an INNER JOIN
Unmatched left rows have o.status = NULL, and NULL = 'paid' is not TRUE, so WHERE discards them. To keep them, move the condition into the ON clause instead.
- A subtle, very common bug.
LEFT JOINON vs WHERE
Q9. hard
What is the difference between putting a condition in ON vs WHERE for a LEFT JOIN?
- A ON filters which rows MATCH (unmatched left rows are still kept); WHERE filters the final result (can drop unmatched rows)
- B No difference
- C ON is only for inner joins
- D WHERE runs first
Answer: A β ON filters which rows MATCH (unmatched left rows are still kept); WHERE filters the final result (can drop unmatched rows)
For INNER JOIN the placement is equivalent, but for OUTER joins it changes the result fundamentally. Conditions on the outer (right) table usually belong in ON.
ON vs WHEREOuter joins
Q11. hard
Why use COUNT(o.id) instead of COUNT(*) in a LEFT JOIN aggregate?
- A COUNT(*) counts the NULL-padded row as 1; COUNT(o.id) counts only real matches, giving 0 for no orders
- B They are identical
- C COUNT(*) is faster and correct
- D COUNT(o.id) is invalid
Answer: A β COUNT(*) counts the NULL-padded row as 1; COUNT(o.id) counts only real matches, giving 0 for no orders
A customer with no orders still produces one result row (all order columns NULL). COUNT(*) counts that row; COUNT of a specific order column ignores it. Getting this wrong overstates counts by one.
COUNTNULLGotchas
Q13. hard
How do you find rows that exist in EITHER table but not both (symmetric difference)?
- A FULL OUTER JOIN with WHERE a.id IS NULL OR b.id IS NULL
- B INNER JOIN
- C CROSS JOIN
- D LEFT JOIN only
Answer: A β FULL OUTER JOIN with WHERE a.id IS NULL OR b.id IS NULL
FULL join keeps unmatched rows from both sides; filtering for NULL on either key leaves only the non-overlapping rows. Useful for reconciling two data sources.
FULL OUTER JOINReconciliation
Q15. hard
In a chain of LEFT JOINs, why can a later INNER JOIN undo earlier ones?
- A An INNER JOIN after LEFT JOINs discards rows that had NULLs, effectively filtering out the preserved rows
- B It never happens
- C INNER always comes first
- D Chained joins are invalid
Answer: A β An INNER JOIN after LEFT JOINs discards rows that had NULLs, effectively filtering out the preserved rows
Once you switch to INNER later in the chain, any row lacking a match there is dropped - including the NULL-padded rows the LEFT JOIN kept. Keep the chain LEFT if you must preserve rows throughout.
Join chainsOuter joins
Q17. hard
Why might SUM over a LEFT JOIN return NULL for unmatched rows?
- A There are no values to sum, and SUM of nothing is NULL - wrap with COALESCE(SUM(x), 0)
- B SUM always errors on joins
- C LEFT JOIN removes the row
- D It returns 0 automatically
Answer: A β There are no values to sum, and SUM of nothing is NULL - wrap with COALESCE(SUM(x), 0)
The unmatched right columns are all NULL, so SUM has nothing to add and yields NULL. COALESCE converts it to a numeric 0 for clean reports.
SUMNULLCOALESCE
Q19. hard
Does MySQL support FULL OUTER JOIN?
SELECT * FROM a LEFT JOIN b ON a.id=b.a_id
UNION
SELECT * FROM a RIGHT JOIN b ON a.id=b.a_id;
- A Not directly - you emulate it with LEFT JOIN UNION RIGHT JOIN
- B Yes, fully
- C Only in views
- D Only with indexes
Answer: A β Not directly - you emulate it with LEFT JOIN UNION RIGHT JOIN
PostgreSQL, SQL Server and Oracle support FULL OUTER JOIN. In MySQL, combine a LEFT JOIN and a RIGHT JOIN with UNION to get the same result.
FULL OUTER JOINMySQL
Q21. hard
How would you list departments and their employee count, showing empty departments too?
- A SELECT d.name, COUNT(e.id) FROM departments d LEFT JOIN employees e ON d.id = e.dept_id GROUP BY d.name;
- B INNER JOIN departments and employees
- C RIGHT JOIN employees
- D Count employees only
Answer: A β SELECT d.name, COUNT(e.id) FROM departments d LEFT JOIN employees e ON d.id = e.dept_id GROUP BY d.name;
LEFT JOIN from departments preserves every department; COUNT(e.id) yields 0 for empty ones. INNER JOIN would silently hide departments with no staff.
LEFT JOINCOUNT
Q23. hard
Which join finds products that have NEVER been ordered?
- A products LEFT JOIN order_items ... WHERE order_items.id IS NULL
- B products INNER JOIN order_items
- C products RIGHT JOIN order_items
- D products CROSS JOIN order_items
Answer: A β products LEFT JOIN order_items ... WHERE order_items.id IS NULL
The anti-join again: keep all products, then filter to those with no matching order line. Equivalent to WHERE NOT EXISTS (SELECT 1 FROM order_items ...).
Anti-joinNOT EXISTS
Q25. hard
What is the row count of A LEFT JOIN B when every A row matches exactly one B row, plus 5 A rows match none?
- A The number of A rows (matched rows once each, plus the 5 unmatched once each)
- B A rows minus 5
- C A times B
- D Only the matched rows
Answer: A β The number of A rows (matched rows once each, plus the 5 unmatched once each)
One-to-one matches produce one row each, and unmatched A rows also produce one (NULL-padded) row each - so the total equals the A row count. Fan-out only occurs with one-to-many matches.
LEFT JOINRow counts
Q27. hard
How does a LEFT JOIN interact with a one-to-many relationship?
- A Matched left rows still fan out to multiple rows; unmatched left rows appear once with NULLs
- B It prevents fan-out
- C It always returns one row per left row
- D It errors
Answer: A β Matched left rows still fan out to multiple rows; unmatched left rows appear once with NULLs
LEFT JOIN does not stop duplication - a customer with 3 orders still yields 3 rows. It only guarantees that customers with 0 orders are not lost.
Fan-outLEFT JOIN
Q29. hard
Why can a LEFT JOIN sometimes be SLOWER than an INNER JOIN?
- A It must preserve unmatched rows, giving the optimiser fewer ways to prune and reorder
- B It is always slower
- C It scans twice
- D It never differs
Answer: A β It must preserve unmatched rows, giving the optimiser fewer ways to prune and reorder
INNER joins let the optimiser drop non-matching rows early and choose join order freely. Outer joins constrain this, so use INNER when you genuinely only need matches.
PerformanceOptimiser