๐๐ป๐๐ฒ๐ฟ๐๐ถ๐ฒ๐๐ฒ๐ฟ:
You have 2 minutes to solve this SQL query.
Find customers who have never placed an order.
Tables:
customers(customer_id, customer_name)
orders(order_id, customer_id, order_date)
๐ ๐ฒ: Challenge accepted! ๐ช
SELECT
c.customer_id,
c.customer_name
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id
WHERE o.customer_id IS NULL;
๐ก Explanation:
This query uses a LEFT JOIN to return all customers, whether or not they have placed an order.
โข LEFT JOIN keeps every customer in the result.
โข Customers without matching records in the orders table will have NULL values.
โข The WHERE o.customer_id IS NULL condition filters only customers who have never placed an order.
This question tests your understanding of:
โ
LEFT JOIN
โ
Finding missing records
โ
NULL handling
๐ฏ Expected Output Example
Customer ID Customer Name
105 Alice
112 David
118 Sarah
๐ Questions about finding unmatched records are very common in interviews. Practice using:
- LEFT JOIN ... IS NULL
- NOT EXISTS
- NOT IN (carefully, because NULL values can affect results)
Among these, NOT EXISTS is often preferred for correctness and performance in many databases.
โค๏ธ React with โค๏ธ for more SQL interview challenges!
Post #2898
4.4K
- โค 11
- ๐ฅ 1