Compare two tables (EXCEPT)
An SQL exercise from Querydrill. Solve it with a SQL editor in your browser, checked on the rows it returns and again on changed data, and it is checked the moment you submit.
Try it yourself
customers_import is a fresh export of the customer list. Return the rows of customers_import that are new or changed: every row that is not in customers with exactly the same id, name, country and signup_date. Two empty (NULL) values count as the same. customers_import has one row per id.
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 |
customers_import (8 rows)
| id | name | country | signup_date |
|---|---|---|---|
| 1 | Anna | NL | 2024-01-05 |
| 2 | Bruno | PT | 2024-02-11 |
| 3 | Chloe | US | 2024-03-02 |
| 4 | Dev | IN | 2024-03-19 |
| 5 | Elif | NULL | 2024-05-27 |
| 6 | Farah | US | 2024-06-14 |
| 8 | Hana | JP | 2024-09-09 |
| 9 | Ivan | PL | 2025-01-15 |
Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.
What this exercise teaches
EXCEPT compares whole rows and treats two NULLs as equal, which is exactly what a table comparison needs. A join with <> on each column misses a value that changed to or from NULL, because NULL <> 'TR' is unknown.
A hint if you get stuck
EXCEPT returns the rows of the first query that the second query does not return.
Background you need
Common mistakes on this exercise
- Finds only new ids, not rows whose values changed.
- A country that changed to NULL is missed, because a comparison with NULL is unknown.
Each of these returns a result that looks plausible. The checker catches them because it runs your query on changed data as well.
Keep going on the practice page
Your first 8 exercises on Querydrill are free, with no account. The practice page has the full editor, timed mode and all the exercises in order. A free account shows 3 reference solutions a day; Pro unlocks every exercise, every solution, timed mode and the certificate.
Open this exerciseMore exercises in Joins
- Orders with customer names
- Customers who never ordered
- Revenue per country
- Everyone with a department
- Employee and manager
- Revenue per product
- Revenue per category
- Products never sold
- Headcount per department
- Orders per customer
- Most recent order per customer
- Products per order
- Customers from the same country
- Customers who never had a refund
- Employees who manage nobody
- Categories per customer
- Payroll per department
- Direct reports per manager
- Stack two lists with UNION ALL
- Keep both sides with a full join
- Products on each order as one text (string_agg)
- Customers whose every order is paid (bool_and)
- Revenue inflated by a join (fan-out)
- Not equal, and the rows with NULL
- Overlapping bookings