Skip to content
Laviyx Learn

Try

SQL course menu

SQL JOIN Interview Questions and Answers

SQL JOIN interview questions with worked answers: types of joins, counting join results with duplicates and NULLs, the LEFT JOIN WHERE trap, anti-joins, self joins, finding orphan rows, and fan-out.

Intermediate8 min readUpdated
On this page

JOINs come up in nearly every SQL interview, because they show whether you really understand how tables relate. Interviewers like to ask you to predict results before writing queries, so practise reasoning about rows, not just syntax.

The examples use the course's sample tables:

customers: Asha (1), Ravi (2), Meera (3), John (4)

orders

order_idcustomer_idamount
1011250
1021120
1032560
104590

Concepts#

1. Name the types of JOIN and what each returns.#

Show solution
  • INNER JOIN: only rows that match in both tables.
  • LEFT JOIN: all rows from the left table, plus matches (or NULLs) from the right.
  • RIGHT JOIN: all rows from the right table, plus matches from the left.
  • FULL OUTER JOIN: all rows from both, matched where possible.
  • CROSS JOIN: every combination of rows (Cartesian product).
  • SELF JOIN: a table joined to itself using two aliases.

See Introduction to JOINs.

2. Predict the row counts of INNER and LEFT JOIN between customers and orders.#

Show hint

Count matching pairs first; then add the unmatched rows each join keeps.

Show solution
  • customers INNER JOIN orders: 3 rows (Asha ×2, Ravi ×1).
  • customers LEFT JOIN orders: 5 rows (the 3 matches + Meera and John with NULLs).
SQLRun
SELECT COUNT(*) AS inner_rows
FROM customers c JOIN orders o ON o.customer_id = c.customer_id;
Output
inner_rows
----------
3
SQLRun
SELECT COUNT(*) AS left_rows
FROM customers c LEFT JOIN orders o ON o.customer_id = c.customer_id;
Output
left_rows
---------
5

3. The duplicates-and-NULLs puzzle#

Table a has ids 1, 1, 2, NULL. Table b has ids 1, 1, 3, NULL. How many rows does each join on a.id = b.id return?

Show hint

Each 1 in a matches each 1 in b. NULL never equals anything, including another NULL.

Show solution
JoinRowsWhy
INNER42 ones × 2 ones = 4 pairs; 2, 3 and the NULLs have no match
LEFT64 pairs + unmatched 2 and NULL from a
FULL OUTER86 + unmatched 3 and NULL from b

Two common wrong answers: "2" (forgetting that duplicates multiply) and counting NULL = NULL as a match. This question tests both ideas at once.

4. What is the difference between putting a condition in ON and in WHERE for a LEFT JOIN?#

Show solution

A condition in ON decides which right-side rows match; unmatched left rows are still kept with NULLs. A condition in WHERE filters after the join, so a condition on the right table removes the NULL rows and effectively turns the LEFT JOIN into an INNER JOIN.

SQLRun
-- Condition in ON: every customer kept
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o
  ON o.customer_id = c.customer_id AND o.amount > 200
ORDER BY c.customer_id;
Output
name  | order_id
------+---------
Asha  | 101
Ravi  | 103
Meera | NULL
John  | NULL
SQLRun
-- Condition in WHERE: Meera and John disappear
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.amount > 200
ORDER BY c.customer_id;
Output
name | order_id
-----+---------
Asha | 101
Ravi | 103

See LEFT JOIN.

5. Can a JOIN return more rows than either table?#

Show solution

Yes. When one row matches several rows on the other side, it appears once per match. In the puzzle above, a 4-row table joined to a 4-row table returned up to 8 rows, and a CROSS JOIN of the same tables returns 16. This "fan-out" is also why SUM after a one-to-many join can double-count. See Multiple JOINs.

Queries#

6. Find customers who have never placed an order (three ways).#

Show solution
SQLRun
-- 1. LEFT JOIN anti-join
SELECT c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;

-- 2. NOT EXISTS (safest)
SELECT c.name
FROM customers c
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id);

-- 3. NOT IN (only safe if orders.customer_id can never be NULL)
SELECT name
FROM customers
WHERE customer_id NOT IN (SELECT customer_id FROM orders);

All three return Meera and John. Mention the NOT IN + NULL trap.

7. Find orders whose customer does not exist.#

Show solution
SQLRun
SELECT o.order_id, o.customer_id
FROM orders o
LEFT JOIN customers c ON c.customer_id = o.customer_id
WHERE c.customer_id IS NULL;
Output
order_id | customer_id
---------+------------
104      | 5

Follow up by explaining that a foreign key constraint would have prevented this orphan row.

8. List each employee with their manager's name, including employees without a manager.#

Show solution

A self join with LEFT JOIN:

SQLRun
SELECT e.name AS employee,
       COALESCE(m.name, '(no manager)') AS manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.id
ORDER BY e.id;
Output
employee | manager
---------+-------------
Asha     | Priya
Ravi     | Meera
Meera    | (no manager)
John     | Priya
Priya    | (no manager)

See SELF JOIN.

9. Which customer placed the most orders?#

Show hint

Count orders per customer, then keep the customer(s) whose count equals the maximum count, so ties are handled.

Show solution
SQLRun
SELECT c.name
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id
GROUP BY c.customer_id, c.name
HAVING COUNT(*) = (
  SELECT MAX(cnt)
  FROM (SELECT COUNT(*) AS cnt FROM orders GROUP BY customer_id) t
);
Output
name
----
Asha

ORDER BY COUNT(*) DESC LIMIT 1 is shorter, but it silently drops ties. Mention both.

Tips#

  • Draw the two tables and connect matching rows before answering "how many rows" questions.
  • Say out loud what one row of your result represents.
  • When an interviewer says "all customers", reach for a LEFT JOIN from customers.

Next: SQL Query Challenges.

Finished this lesson?