Debugging SQL Queries Step by Step
When a query surprises you, shrink it. Inspect the data, check one piece at a time, and the bug surfaces.
Not every bug throws an error. Sometimes a query runs perfectly and returns nothing, which is its own kind of clue. The fix is to debug in small steps: strip the query back, look at the actual data, and add conditions one at a time.
The query below expects shipped orders but comes back empty. Before changing
it blindly, inspect what's really in the
status
column. String comparisons are case-sensitive, so a capital letter is all it
takes to match nothing.
-- when a result is empty, inspect the real values:
select distinct status from orders;
-- ...then fix the filter to match Schema · Garden Shop Table · orders 6 columns · 24 rows
One row per order. Unshipped orders have a null shipped_date.
The starter returns zero rows. Figure out why, then fix it so it lists every
shipped order: order_id and status, sorted by order_id.
Run your query to inspect the result.
Expected answer
- Columns: order_id, status.
- Rows: 17 shipped orders.
- String comparisons are case-sensitive, so 'Shipped' matched nothing.
Related
Indentation, casing, and aliasing conventions.
One-to-many joins can inflate totals.
Paste an error message and find the fix, from GROUP BY to constraint failures.
Debug filters that match nothing.
Group or aggregate every selected column.
Right-table WHERE filters remove unmatched rows.