Skip to content
mentoor

Topic 4 of 5 · 4 min read

Sanity-checking query results

Goal: Check a query result against a known total and find the join or NULL problem behind a mismatch.

Never accept a query result until it passes at least one check against something you already know. A fast, confident number from an assistant is only a lead. Three cheap checks catch most of the errors that reading the SQL missed.

Check yourself · 1 of 3

An assistant reports October revenue of 412,000 EUR. The finance dashboard says 398,500 EUR. What is the right next step?

Choose one answer

Why it matters

A wrong query rarely fails loudly. It returns a table that looks like an answer. Decisions on budgets, targets and reports then rest on it. The check is usually minutes of work, and it is the step that separates using AI from trusting it.

How to do it

  1. Compare with a known total. Run the same logic with no breakdown: total paid revenue for October. Does it match the finance dashboard or last month's report? If the breakdown by country adds up to a different number than the plain total, the breakdown loses or repeats rows.
  2. Check row counts at each step. How many orders are in October before the join? How many after? If the count rises after a join, rows are being repeated (fan-out). If it falls, an inner join is dropping rows.
  3. Look for NULL. Aggregates such as SUM and AVG skip NULL values, and COUNT(column) does too, so averages may cover fewer rows than you think. A comparison like status <> 'refunded' also drops rows where status is NULL. Count the NULLs in the columns you filter on.
  4. Check for duplicates. Does COUNT(*) equal COUNT(DISTINCT id) on the key you expect to be unique? If not, you have duplicate rows.
  5. Look at ten raw rows. Pick one customer and follow them by hand through the tables. Edge cases show up fast.
  6. Judge the size. Does a result 40 percent higher than last month fit what you know about the business?

Before and after

Before: "The assistant says October revenue was 412,000 EUR. Sent to the team."

After: "The assistant says 412,000 EUR. The dashboard says 398,500 EUR. The gap is 13,500 EUR. I counted orders: 1,820 before the join with order lines, 2,103 after. The join repeats orders with several lines. I count distinct orders and the total now matches."

What goes wrong

  • Checking against a number that uses a different definition. The dashboard may exclude tax. Align definitions first, or the "mismatch" is not an error.
  • Only comparing grand totals. Two errors can cancel out, so also compare one or two segments.
  • Fixing the number to match. Do not tweak a filter until the total agrees. Find the reason for the difference first.

If the check fails, give the assistant the numbers: "Your total is 13,500 higher than the dashboard; row count rises from 1,820 to 2,103 after the join. What explains it?" Then verify its answer again.

Examples

Breakdown that does not add up

Country Revenue (EUR)
DE 150,000
FR 120,000
ES 100,000
Sum of rows 370,000
Query without breakdown 412,000

The rows are 42,000 short of the plain total. Customers with no country (NULL) were dropped by the grouping or by an inner join to a countries table. Counting WHERE country IS NULL confirms it.

Two quick SQL checks

-- duplicates on the key you expect to be unique
SELECT COUNT(*), COUNT(DISTINCT order_id) FROM result_rows;
-- hidden NULLs in a filtered column
SELECT COUNT(*) FROM orders WHERE status IS NULL;

If the first two numbers differ, rows repeat. If the second query returns more than zero, a filter such as status <> 'refunded' is excluding those rows.

Key points

  • Compare the result with a known total, such as a dashboard figure or last period's report, before using it.
  • A row count that rises after a join means rows are repeated, and one that falls means an inner join is dropping rows.
  • SUM, AVG and COUNT(column) skip NULL values, and a not-equal filter also drops rows where the column is NULL.
  • Test that COUNT(*) equals COUNT(DISTINCT key) to find duplicate rows.
  • Make sure the benchmark number uses the same definition as your query before calling a gap an error.

Common mistakes

  • Using the first result from the assistant without any comparison to a known figure.
  • Comparing only the grand total, so offsetting errors in two segments stay hidden.
  • Adjusting filters until the total matches instead of finding why it differs.
  • Forgetting that NULL values are skipped by aggregates and fail comparisons such as not equal.

Check yourself · 2 of 3

A query that joins orders to order lines returns 2,103 rows. Orders alone in the same period number 1,820. What is the most likely explanation?

Choose one answer

Check yourself · 3 of 3

A column status has some NULL values. A query filters WHERE status <> 'refunded'. What happens to the rows where status is NULL?

Choose one answer

Practice items, not counted towards your mastery.

Start here