Home › Learn › SQL › Joins

LEFT JOIN

Lesson 18 of 26. Keep rows that have no match.

An inner join drops customers who never ordered. LEFT JOIN keeps every row of the left table and fills the right side with NULLs where nothing matched. That makes "customers with no orders" a LEFT JOIN plus WHERE orders.id IS NULL: the NULL is the sign that no order was found.

Counting with a left join needs care: COUNT(*) counts the NULL row too, while COUNT(orders.id) counts real orders, giving zero for customers without any.

SELECT c.name, COUNT(o.id) AS orders
FROM customers c LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id;

Your task

Show the name of every customer who has never placed an order.

Hint 1

LEFT JOIN orders ON orders.customer_id = customers.id WHERE orders.id IS NULL

Show the solution
SELECT customers.name
FROM customers LEFT JOIN orders ON orders.customer_id = customers.id
WHERE orders.id IS NULL;

    Runs SQLite compiled to WebAssembly in this tab; the database is rebuilt before every run.