Sub topic 14
Common mistakes and how to check results
Assume you know SELECT, WHERE, ORDER BY, basic joins, NULL, aggregate functions, and GROUP BY. This chapter focuses on checking grouped queries instead of trusting plausible-looking numbers.
Mistake: counting the wrong thing
COUNT(*) counts rows. COUNT(column) counts only rows where that column is not NULL. COUNT(DISTINCT column) counts each non-NULL value once.
SELECT COUNT(*) AS all_orders,
COUNT(shipped_at) AS shipped_orders,
COUNT(DISTINCT customer_id) AS customers
FROM orders;A common error is using COUNT(*) after a LEFT JOIN and reporting one order for customers with no orders. The join still produces one row for those customers. Count a non-null column from the child table instead.
SELECT c.customer_id,
c.name,
COUNT(o.order_id) AS order_count
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
GROUP BY c.customer_id, c.name;Mistake: selecting a column that is not grouped
After GROUP BY, each output row represents a group. A selected column must either identify that group or be inside an aggregate function.
This query is invalid in many SQL systems:
SELECT customer_id, status, SUM(amount) AS total_amount
FROM orders
GROUP BY customer_id;There may be several statuses inside one customer group, so SQL cannot choose one status to display. Group by both columns if status is part of the desired result.
SELECT customer_id,
status,
SUM(amount) AS total_amount
FROM orders
GROUP BY customer_id, status;Some systems accept the first query when a functional dependency exists, such as selecting a customer name while grouping by a customer’s primary key. Do not depend on relaxed behavior when portability matters.
Mistake: using WHERE where HAVING is needed
WHERE filters individual rows before grouping. HAVING filters groups after aggregation.
SELECT customer_id,
SUM(amount) AS paid_total
FROM orders
WHERE status = 'paid'
GROUP BY customer_id
HAVING SUM(amount) > 1000;This keeps paid orders first, then keeps customers whose paid total exceeds 1,000. Putting SUM(amount) > 1000 in WHERE is invalid because the sum does not exist until after grouping.
Mistake: inflating totals through joins
Joining two one-to-many tables before aggregating can multiply rows. For example, one order with three items and two payments becomes six joined rows. Summing the order amount then counts it six times.
Aggregate each one-to-many source at its intended grain before joining:
WITH item_totals AS (
SELECT order_id,
SUM(quantity * unit_price) AS item_total
FROM order_items
GROUP BY order_id
),
payment_totals AS (
SELECT order_id,
SUM(amount) AS paid_total
FROM payments
GROUP BY order_id
)
SELECT o.order_id,
i.item_total,
p.paid_total
FROM orders AS o
LEFT JOIN item_totals AS i ON i.order_id = o.order_id
LEFT JOIN payment_totals AS p ON p.order_id = o.order_id;The grain is what one row represents. State the grain before writing the query: “one row per customer,” “one row per order,” or “one row per customer and status.”
Mistake: misunderstanding NULL groups
GROUP BY places all NULL values in one group. This does not mean that missing values are equal in ordinary comparisons. It means the grouping operation treats them as one grouping category.
Aggregates still follow their own NULL rules. SUM, AVG, MIN, and MAX ignore NULL; if every input is NULL, they usually return NULL, not zero. Use COALESCE only when zero has the correct meaning.
SELECT customer_id,
COALESCE(SUM(amount), 0) AS total_amount
FROM orders
GROUP BY customer_id;Checks that catch incorrect results
| Check | What it can reveal |
|---|---|
| Compare row counts before and after a join | Unexpected fan-out |
| Compare `SUM(amount)` with the grouped totals | Missing or duplicated rows |
| Inspect `COUNT(*)` and `COUNT(column)` together | Unexpected `NULL` values |
| Check the intended grain | Extra or missing grouping columns |
| Test an empty group or customer with no children | Incorrect outer-join counting |
Run a simple independent total before trusting a report:
SELECT COUNT(*) AS rows_in_source,
SUM(amount) AS source_total
FROM orders;Then compare it with a grouped result. If grouped totals do not reconcile, inspect filters, joins, NULL values, and duplicate keys.
Exercise
A LEFT JOIN report shows one order for customers with no orders. Which change usually fixes the count?
Show answer
Replace COUNT(*) with COUNT(orders.order_id), because the right-side key is NULL when no order matched.