Lesson 5 of 10 · 30 min

Debugging queries under pressure

By the end of this lesson you can debug a broken or subtly wrong query methodically: read the error, check row counts layer by layer, compare against a control total, and fix the five silent bugs interviewers plant most often.

There are two kinds of broken query. Loud ones stop with an error message: a missing comma, a column not in GROUP BY, a type mismatch. Silent ones run and return a number that looks reasonable and is wrong: a join that duplicated rows, a filter that removed the rows you wanted to count, a NULL that made a condition unknown. Interviewers love silent bugs, because finding one shows you check results instead of trusting them.

This lesson uses Sahra Logistics (freight): shipments from warehouses to customers via carriers, the scan events of each shipment, purchase orders and an inventory ledger. Take two minutes to look at the schema first. Note that shipments.status is the status today, that a delivered shipment can still be late, and that inventory_movements is a ledger whose running sum is the stock on hand.

Included with a Formula Quest membership

This lesson, its 6 graded exercises and every other Members lesson in SQL Quest come with a Formula Quest membership that includes SQL (Single Quest with SQL selected, Quest Trio including SQL, or Quest Unlimited), or with SQL access from your organization.

Free to everyone: all of SQL Foundations and Retrieving and Filtering Data, the first lesson of every track, the playground, the dataset library and the SQL reference. See free lessons