Why Your SQL Numbers Are Wrong: 7 Silent Mistakes That Make Reports Lie
This article is available in English. Browse translated guides in the language menu.
The most dangerous SQL query is not the one that throws an error. It is the one that runs perfectly and returns the wrong number.
No red text. No warning. Just a revenue figure that is 3x too high, a “conversion rate” that quietly ignores half your users, or a dashboard that disagrees with Finance. Then someone makes a decision based on it.
This guide covers the 7 silent mistakes behind most wrong SQL results, each with a way to spot it and a way to fix it. You do not need to know how databases work internally. You only need to know how to question your own results.
Mistake 1: Joins that multiply your rows
The symptom: Totals are far larger than reality.
Say you want total revenue per customer, and you join orders to order_items (one order can have many items):
SELECT c.name, SUM(o.total_amount) AS revenue
FROM customers c
JOIN orders o ON o.customer_id = c.id
JOIN order_items i ON i.order_id = o.id
GROUP BY c.name;
An order worth 100 with 4 items appears 4 times, so it is counted as 400. The query is valid and the number is wrong.
The fix: Sum at the level where the value lives, then join.
SELECT c.name, SUM(o.total_amount) AS revenue
FROM customers c
JOIN orders o ON o.customer_id = c.id
GROUP BY c.name;
The habit: Before and after every join, ask “Is this one-to-one or one-to-many?” If one-to-many, any amount from the “one” side will be repeated.
Quick test: Compare COUNT(*) before and after adding the join. If the count grew and you did not expect it to, you have row multiplication.
Mistake 2: Treating NULL like a normal value
NULL does not mean zero or empty. It means unknown, and SQL treats unknowns strangely.
| You write | What you expect | What happens |
|---|---|---|
WHERE status <> 'cancelled' | Everything not cancelled | Rows where status is NULL are dropped |
AVG(rating) | Average over all rows | NULL ratings are ignored, so the denominator shrinks |
COUNT(discount) | Number of orders | Counts only rows where discount is not NULL |
price + shipping | Total cost | If shipping is NULL, the result is NULL |
Fixes:
-- Keep NULL statuses
WHERE status <> 'cancelled' OR status IS NULL
-- Treat missing shipping as zero (only if that is truly correct)
SELECT price + COALESCE(shipping, 0) AS total_cost
-- Count all rows, not just non-NULL ones
SELECT COUNT(*) FROM orders;
The habit: For every column you filter or aggregate, ask “Can this be NULL, and what should that mean?” Never replace NULL with 0 automatically. Sometimes “unknown” and “zero” are very different things.
Mistake 3: The “average of averages” trap
Two stores report average order value:
- Store A: 10 orders, average 100
- Store B: 1,000 orders, average 20
The company-wide average is not (100 + 20) / 2 = 60. It is about 20.8.
Averaging pre-computed averages gives every group equal weight, no matter its size. This happens constantly when people AVG() over a summary table.
The fix: Aggregate from raw rows, or carry the sum and the count.
-- Correct: weighted by actual orders
SELECT SUM(total_amount) * 1.0 / COUNT(*) AS avg_order_value
FROM orders;
-- If you only have a summary table with total_sales and order_count
SELECT SUM(total_sales) * 1.0 / SUM(order_count) AS avg_order_value
FROM store_summary;
The same applies to percentages and rates. Never average percentages. Divide the total numerator by the total denominator.
Mistake 4: Integer division silently rounds down
In many databases (PostgreSQL, SQL Server, and others), dividing two whole numbers returns a whole number:
SELECT 3 / 4; -- returns 0, not 0.75
So your “conversion rate” of purchasers / visitors shows 0 for every row, or is rounded to a misleading whole number.
The fix: Force decimal math.
SELECT purchasers * 1.0 / NULLIF(visitors, 0) AS conversion_rate
FROM daily_stats;
Two bonus protections here:
* 1.0forces decimal division.NULLIF(visitors, 0)turns a zero denominator into NULL, so you get a blank instead of a “division by zero” crash that stops your whole report.
Mistake 5: Counting rows when you meant to count people
COUNT(*) counts rows. If a customer places 5 orders, they are 5 rows.
-- "How many customers bought in March?"
SELECT COUNT(*) FROM orders
WHERE order_date >= '2026-03-01' AND order_date < '2026-04-01';
-- This answers: how many ORDERS were there?
SELECT COUNT(DISTINCT customer_id) FROM orders
WHERE order_date >= '2026-03-01' AND order_date < '2026-04-01';
-- This answers the real question.
The habit: Write the question in plain words first. “How many orders?” and “How many customers?” are different queries. Also define what counts: does a refunded or test order count as a customer?
Mistake 6: Date ranges that lose a day (and time zones that shift it)
Two classic traps:
The “BETWEEN” trap. If created_at includes a time, this misses almost all of the last day:
-- Misses everything after 00:00:00 on March 31
WHERE created_at BETWEEN '2026-03-01' AND '2026-03-31'
The fix: Use a half-open range (start inclusive, end exclusive). It works for any precision.
WHERE created_at >= '2026-03-01'
AND created_at < '2026-04-01'
The time zone trap. Timestamps are often stored in UTC. A sale at 11:30 PM in New York on March 31 is stored as April 1 in UTC, so your “March” report quietly moves it to April. If your business reports in local time, convert first:
-- PostgreSQL
SELECT (created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/New_York')::date AS local_day
FROM orders;
The habit: Always know which time zone your timestamps are in, and which one your report should use. Document it on the report.
Mistake 7: Duplicates you did not know existed
If a table has accidental duplicates (a double-submitted form, a re-run import), every count and sum is inflated, and nothing tells you.
Detect duplicates in seconds:
SELECT email, COUNT(*) AS times_seen
FROM customers
GROUP BY email
HAVING COUNT(*) > 1
ORDER BY times_seen DESC;
Keep one row per key (latest wins):
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY email ORDER BY updated_at DESC) AS rn
FROM customers
) t
WHERE rn = 1;
Avoid slapping DISTINCT on a query to “make the duplicates go away.” It hides the cause and can remove legitimate rows.
The 5-Minute Trust Checklist
Run this before sending any number to anyone:
- Sanity check the size. Is the total in a plausible range? Compare against last month, or a number you already trust.
- Reconcile with a simple query.
SELECT COUNT(*), SUM(amount) FROM orderswith the same filters. Does your complex query’s total match? - Check row counts across each join. Did they grow unexpectedly?
- Scan for NULLs.
SELECT COUNT(*) - COUNT(column_name) AS null_count FROM tablefor each important column. - Look at 5 raw rows. Pick one customer or order and trace it by hand through the query.
- Check edges. First day, last day, zero values, the biggest customer, the smallest.
- Write down the definition. “Revenue = paid orders, excluding refunds, in local time, by order date.” If you cannot write it, you cannot defend it.
Step 5 catches more bugs than any other. If one customer’s numbers are right by hand, the logic is probably right. If they are not, you have found the bug.
Quick Reference: Symptom to Likely Cause
| Symptom | Most likely cause |
|---|---|
| Totals way too high | Join multiplied rows (1) or duplicate data (7) |
| Counts lower than expected | NULLs dropped by a filter (2), or an inner join removing rows |
| Rate shows 0 or a whole number | Integer division (4) |
| Average looks “off” | Average of averages (3) or NULLs ignored (2) |
| Last day of the month missing | BETWEEN with timestamps (6) |
| Numbers differ from Finance by a few days or hours | Time zone mismatch (6) |
| Customer count equals order count | COUNT(*) instead of COUNT(DISTINCT ...) (5) |
Final Thought
Good SQL is not about writing clever queries. It is about not fooling yourself. Every one of these mistakes produces output that looks professional. The only defense is a habit of suspicion: “Does this number make sense, and can I prove it?”
Next time a result surprises you, in either direction, do not just fix the query. Ask which of these seven it was. You will get faster at spotting them, and people will start trusting your numbers.
Related Guides & Interactive Practice
- INNER JOIN vs LEFT JOIN: Examples, NULLs, and Common Mistakes
- Interactive Tutorial: Handling NULL Values
- Interactive Tutorial: Aggregate Functions & Grouping
- Breaking ‘Blank Screen Syndrome’: 5-Step Incremental Framework
- Interview Challenge: Customer Who Visited but Did Not Make Any Transactions
- Interactive SQL Practice Lab (Client-Side WASM)
Ready to Level Up Your SQL?
Don't just read about databases. Write queries, build schemas, and practice real-world interview questions interactively.