Skip to main content

SQL guide

SQL mistakes that silently return wrong answers

The SQL bugs that do not raise errors: NULL comparisons, integer division, join fan-out, BETWEEN on timestamps, NOT IN with NULLs, and more, with fixes for each.

By Simas Bandzevicius · Updated November 24, 2026 · 9 min read

An error message is a gift: it tells you something is wrong. The dangerous SQL bugs are the ones that run successfully and return a believable number. Here are the ones that most often end up in real reports, with how to spot and fix each.

1. Comparing with NULL using equals

-- Returns zero rows, always
SELECT first_name FROM employees WHERE department_id = NULL;

Any comparison with NULL is unknown, and WHERE keeps only rows where the condition is true. Use IS NULL / IS NOT NULL. The same logic hides rows in negative filters: WHERE status <> 'cancelled' also excludes rows whose status is NULL.

2. NOT IN with a NULL in the list

-- Returns nothing if any employee has a NULL department_id
SELECT name FROM departments
WHERE id NOT IN (SELECT department_id FROM employees);

NOT IN expands to a chain of <> comparisons joined by AND; one NULL makes the whole chain unknown. Use NOT EXISTS, which is NULL-safe:

SELECT d.name FROM departments d
WHERE NOT EXISTS (SELECT 1 FROM employees e WHERE e.department_id = d.id);

3. Integer division

SELECT 7 / 2;      -- 3 in PostgreSQL, not 3.5
SELECT 7 / 2.0;    -- 3.5000000000000000

When both sides are integers, the result is an integer. Multiply by 100.0 or cast one side (completed::numeric) before dividing.

4. Join fan-out inflating totals

Joining a parent to two one-to-many children multiplies rows: an order with 3 lines and 2 payments becomes 6 rows, and every SUM is too big. Aggregate each child to the parent's grain in a CTE first, then join the summaries. A quick check: if COUNT(*) and COUNT(DISTINCT parent_id) differ when you expected one row per parent, something is fanning out. The join decision guide covers this in detail.

5. BETWEEN on timestamps

-- Misses everything after 00:00:00 on 31 January
WHERE created_at BETWEEN '2024-01-01' AND '2024-01-31'

The upper bound is midnight at the start of the 31st. Use a half-open range instead: created_at >= '2024-01-01' AND created_at < '2024-02-01'. It is correct for any precision and makes adjacent months line up without gaps or overlaps.

6. AVG that ignores missing values

AVG(bonus) skips NULLs, so if NULL really means "no bonus" the average is overstated. Decide what NULL means in your data. If it means zero, write AVG(COALESCE(bonus, 0)); if it means unknown, report how many values were missing alongside the average.

7. COUNT(*) after a LEFT JOIN

SELECT c.id, COUNT(*) AS orders   -- customers with no orders show 1
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id;

The unmatched customer still has one joined row. Count a column from the right-hand table, COUNT(o.id), which is NULL for unmatched rows.

8. A WHERE clause that undoes a LEFT JOIN

Filtering the right-hand table in WHERE removes the NULL rows the left join was keeping, so the query quietly becomes an inner join. Put conditions on the right-hand table in the ON clause.

9. LIMIT without a deterministic ORDER BY

"Top 10" without a unique tie-breaker can return different rows on different runs. Sort by the metric and then by a unique column: ORDER BY revenue DESC, id.

10. Rounding before aggregating

Rounding each row and then summing gives a different total from summing and then rounding, and the error grows with row count. Keep full precision through every calculation and round only in the final SELECT.

A checklist before you share a number

  1. Does the row count make sense for the grain you intended?
  2. Did any join change the row count unexpectedly?
  3. How many NULLs are in the columns you filtered or averaged?
  4. Is every division done in decimal, with zero denominators handled by NULLIF?
  5. Are date ranges half-open?
  6. Is the final order deterministic?

Five minutes on this list catches most of the mistakes above before anyone else sees them.