Start here
A query that runs is not always a query that is right. Data can be wrong, and SQL does not warn you.
In the pipeline chapter you saw that a check is a query that returns the bad rows. Zero rows means the check passes. This chapter shows the SQL patterns for many kinds of checks.
This chapter uses the shop tables from chapters 1 to 3. You know customers, orders and order_items. Here is customers again:
| customer_id | name | country |
|---|---|---|
| 1 | Asha | IN |
| 2 | Ben | GB |
| 3 | Carla | US |
| 4 | Dev | NULL |
| 5 | Omar | IN |
The shop also has a load. A load is a batch of new rows copied into the database.
This load is the table raw_orders. It has 10 rows:
| order_id | customer_id | status | amount | order_date |
|---|---|---|---|---|
| 101 | 1 | paid | 40.00 | 2026-01-03 |
| 102 | 1 | paid | 25.00 | 2026-01-10 |
| 103 | 2 | refunded | 60.00 | 2026-01-11 |
| 104 | 3 | paid | 15.50 | 2026-01-15 |
| 105 | 3 | pending | NULL | 2026-01-20 |
| 106 | 4 | paid | 80.00 | 2026-01-21 |
| 106 | 4 | paid | 80.00 | 2026-01-21 |
| 107 | 5 | paid | -20.00 | 2026-01-21 |
| 108 | 9 | paid | 30.00 | 2026-01-21 |
| 109 | 5 | Paid | 45.00 | 2026-01-24 |
Four rows are wrong:
- Order 106 appears twice. This is a duplicate.
- Order 107 has an amount of -20.00. An amount cannot be below zero.
- Order 108 belongs to customer 9. No customer 9 exists.
- Order 109 has the status “Paid ” with a capital P and a space at the end. The other paid orders use “paid”.
You will find each problem with a query. Here is the first check. The rule is: no order has a negative amount.
| order_id | customer_id | status | amount | order_date |
|---|---|---|---|---|
| 101 | 1 | paid | 40.00 | 2026-01-03 |
| 102 | 1 | paid | 25.00 | 2026-01-10 |
| 103 | 2 | refunded | 60.00 | 2026-01-11 |
| 104 | 3 | paid | 15.50 | 2026-01-15 |
| 105 | 3 | pending | NULL | 2026-01-20 |
| 106 | 4 | paid | 80.00 | 2026-01-21 |
| 106 | 4 | paid | 80.00 | 2026-01-21 |
| 107 | 5 | paid | -20.00 | 2026-01-21 |
| 108 | 9 | paid | 30.00 | 2026-01-21 |
| 109 | 5 | Paid | 45.00 | 2026-01-24 |
SELECT order_id, customer_id, amount
FROM raw_orders
WHERE amount < 0;| order_id | customer_id | amount |
|---|---|---|
| 107 | 5 | -20.00 |
One bad row, so the check fails.
The check found order 107. If no row had a negative amount, the result would be empty. Empty means pass.
