SQL LEFT JOIN practice problems
A LEFT JOIN keeps every row of the first table, matched or not. It is the tool for questions such as customers with no orders, departments with no employees, and totals that should show 0 where nothing happened.
The usual traps are covered here: filtering the right-hand table in WHERE instead of ON, counting with COUNT(*) where COUNT(column) is needed, and forgetting COALESCE for the empty side.
Try one here: Customers who never ordered
Try it yourself
Return the names of customers who have no orders at all.
customers (8 rows)
| id | name | country | signup_date |
|---|---|---|---|
| 1 | Anna | NL | 2024-01-05 |
| 2 | Bruno | BR | 2024-02-11 |
| 3 | Chloe | US | 2024-03-02 |
| 4 | Dev | IN | 2024-03-19 |
| 5 | Elif | TR | 2024-05-27 |
| 6 | Farah | US | 2024-06-14 |
| 7 | Gus | NL | 2024-08-01 |
| 8 | Hana | JP | 2024-09-09 |
orders (first 12 of 14 rows)
| id | customer_id | order_date | amount | status |
|---|---|---|---|---|
| 101 | 1 | 2025-01-03 | 120 | paid |
| 102 | 1 | 2025-01-20 | 80.5 | paid |
| 103 | 2 | 2025-01-22 | 200 | paid |
| 104 | 3 | 2025-02-02 | 45 | refunded |
| 105 | 3 | 2025-02-15 | 310 | paid |
| 106 | 4 | 2025-02-18 | 150 | pending |
| 107 | 5 | 2025-03-01 | 60 | paid |
| 108 | 6 | 2025-03-09 | 95 | paid |
| 109 | 6 | 2025-03-30 | 130 | paid |
| 110 | 6 | 2025-04-11 | 75 | paid |
| 111 | 7 | 2025-04-12 | 210 | paid |
| 112 | 1 | 2025-04-25 | 40 | paid |
Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.
Joins
- Customers who never orderedReturn the names of customers who have no orders at all.
- Everyone with a departmentReturn every employee's name and department name. Employees without a department must still appear, with NULL as the department name.
- Employee and managerReturn each employee's name and their manager's name. Employees without a manager show NULL.
- Products never soldReturn the names of products that appear on no order line.
- Headcount per departmentReturn every department name and its number of employees. Departments with no employees must show 0.
- Orders per customerReturn every customer name with the number of orders they placed, including customers with 0 orders.
- Most recent order per customerReturn every customer's name and the date of their most recent order, whatever the status. Customers who never ordered must still appear, with NULL as the date.
- Payroll per departmentReturn every department name with the total salary of its employees. Departments with no employees must show 0, not NULL.
- Revenue inflated by a join (fan-out)For each customer with at least one paid order (status paid), return the name, the total amount of their paid orders and the total quantity of items on those orders. An order without item lines counts with quantity 0. Customer names are unique.
- Not equal, and the rows with NULLReturn the names of all employees who do not work in the Engineering department. Employees with no department count too.
Advanced
- Year-over-year growthFor every month of 2025 that is in the table, return the month, its revenue, the revenue of the same month one year earlier, and the growth as a percentage rounded to 1 decimal. When there is no figure for the same month a year earlier, the last two columns are NULL. When that figure is 0, the growth is NULL.
Interview classics
- A month with no sales still shows up (generate_series)Return every month from January to June 2025 as text like 2025-01, with its paid revenue. A month without paid orders must still appear, with 0.
- Signup to purchase conversionA user converted when they have a purchase that is later than their first signup. Return one row: the number of users who signed up, the number of those who converted, and the conversion rate as a percentage rounded to 1 decimal.
- Day-1 retentionA user is retained on day 1 when they have an event on the calendar day right after their first active day. Every user counts in the total, also a user whose first day is the last day in the data. Return one row: the number of users, the number retained on day 1, and the retention rate as a percentage rounded to 1 decimal.
- Monthly churn rateA user is active on a day when one of their subscriptions started on or before that day and has no end date or an end date after that day. Return one row for March 2025: the number of users active on 2025-03-01, how many of them were no longer active on 2025-04-01, and that share as a percentage rounded to 1 decimal.
Work mode
- Signed up, never paidFrom Marta in Marketing: "Which people signed up but never made a paid payment? Treat emails as the same person whatever the capitals or spaces, and count a person as paid if any of their accounts has a paid payment. One row per email, in lowercase." Return email.
Case studies
- Refund review, step 4: refunded value per categoryIs it a product problem? For every product category, return the category and the value of its order lines (quantity times price) on refunded orders. A category with nothing refunded must still appear, with 0.
- Payroll review, step 1: headcount and payroll per departmentFinance is preparing next year's budget and asks for the people numbers. Start with the base table: for every department, return the department name, the number of employees and the total salary. A department with no employees must still appear, with 0 and 0.
Read first if you are stuck
Other practice sets
Your first 8 exercises are free, with no account. Every answer is checked on the rows you see and again on changed data.