SQL JOIN Interview Questions with Worked Answers
Choose the rows you need to preserve, identify the matching keys and check how many matches each input row can have.
Start the worked examples ↓SQL JOIN interview question: Where should a right-table filter go?
Question: Return every customer and their count of orders worth at least $50, including customers with zero qualifying orders.
Put the amount condition in the ON clause of the LEFT JOIN and count o.order_id. A WHERE o.amount_usd >= 50 condition would remove customers whose unmatched order columns are NULL.
SELECT c.customer_id, COUNT(o.order_id) AS qualifying_orders
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id AND o.amount_usd >= 50
GROUP BY c.customer_id
ORDER BY c.customer_id;Using the sample below, customers 1 and 2 each have one qualifying order. Customer 3 stays in the result with zero.
Practise more SQL interview questions, including aggregation and window functions.
A customer report that retains zero-order customers
The two customers named Alex are different people. Group by their IDs as well as their names. Each order ID is unique and non-null in this sample.
WITH customers(customer_id, name) AS (
VALUES (1, 'Alex'), (2, 'Alex'), (3, 'Sam')
), orders(order_id, customer_id, amount_usd) AS (
VALUES (101, 1, 50.0), (102, 1, 30.0), (103, 2, 80.0)
)
SELECT c.customer_id, c.name, COUNT(o.order_id) AS order_count,
COALESCE(SUM(o.amount_usd), 0) AS revenue_usd
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
GROUP BY c.customer_id, c.name
ORDER BY c.customer_id;Expected: customer 1 has two orders and $80; customer 2 has one order and $80; customer 3 has no orders and $0. The total is $160.
What is the difference between INNER JOIN and LEFT JOIN?
- INNER JOIN keeps matching pairs. A customer with two matching orders produces two joined rows.
- LEFT JOIN also retains unmatched left rows, with NULL right-side values. RIGHT JOIN reverses which side is preserved.
- FULL OUTER JOIN preserves unmatched rows from both sides; check support in your database.
- CROSS JOIN produces every pair. A SELF JOIN uses two aliases for the same table, such as employees and their managers.
Why does COUNT(*) give the wrong order count after a LEFT JOIN?
COUNT(o.order_id) counts matched orders here. COUNT(*) would count the preserved unmatched customer row too. A right-table condition in WHERE can remove unmatched rows after a left join. Put a condition in ON when the intention is to restrict matches while keeping all customers.
For a runnable comparison, use ON versus WHERE: keep zero-activity campaigns. It also tests a campaign with only rejected events, which a simple OR event_id IS NULL workaround still loses.
Before a many-to-one lookup join, confirm that the lookup key is unique. If both sides have repeated keys, the result can multiply rows. Do not apply DISTINCT as a substitute for understanding those matches.
Find customers without an order
WITH customers(customer_id, name) AS (
VALUES (1, 'Alex'), (2, 'Alex'), (3, 'Sam')
), orders(order_id, customer_id, amount_usd) AS (
VALUES (101, 1, 50.0), (102, 1, 30.0), (103, 2, 80.0)
)
SELECT c.customer_id, c.name
FROM customers c
WHERE NOT EXISTS (
SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id
)
ORDER BY c.customer_id;Expect only Sam. Try adding a second order for customer 2 and predict the new count and total before running the report.
Why can a SQL join inflate revenue?
A join returns every matching pair. If one $50 order matches two rows in a customer lookup, summing the joined amount produces $100. That does not mean a second order occurred. Check key uniqueness and the intended output grain before aggregating.
Interview follow-up: Would SUM(DISTINCT amount_usd) fix this? No. In the example above, three real orders have values $50, $30 and $80. If another real $50 order arrives, DISTINCT would count the $50 value only once and understate revenue. Deduplicate erroneous records by a justified key or correct the join; do not deduplicate monetary values.
For the complete calculation, run the SUM(DISTINCT) duplicate-join exercise. Three legitimate orders total $250; joining their items produces $350, while summing distinct amounts produces $150. Summarizing items by order before joining restores the intended order-level revenue. If you only need to test whether a match exists, the exercise also shows an EXISTS alternative.
How do you explain your join choice in an interview?
“The report needs every customer, including those without orders, so I start from customers and use a LEFT JOIN. I group by customer ID to keep the two Alex records separate, count the non-null order ID, and reconcile the $160 total against the order table.”
For the related missing-match question, try customers with no orders: NOT EXISTS and the NULL trap. For a two-system investigation, use the order/payment reconciliation exercise.
Continue learning
Reference: PostgreSQL tutorial. Check behaviour for your installed version.
These are educational exercises with fictional data, not a verified employer question bank. Amounts in examples are USD unless otherwise stated.
Practise your explanation
Use the self-practice guide to review your approach. Mock-interview booking and payment are currently inactive. All planned preparation sessions and feedback are in English.
View preparation options