A SQL query can run successfully and still return the wrong number. In an interview, explain the shape of the result before you write the query: does each row represent a customer, an order, or an item? Then inspect how that answer changes after each join, aggregation, or window. This guide uses PostgreSQL syntax. Table and column names are illustrative.

Follow the rows through a worked example

Imagine three tables: customers(id), orders(id, customer_id, status, paid_at, total_cents), and line_items(order_id, quantity). Customer 1 has one paid order with two line items. Customer 2 has one pending order. Customer 3 has no orders. The task is to return one row per customer and count their paid orders, including customers with none. Here is the query to explain:

SELECT c.id, COUNT(o.id) AS paid_orders
FROM customers AS c
LEFT JOIN orders AS o
  ON o.customer_id = c.id AND o.status = 'paid'
GROUP BY c.id
ORDER BY c.id;

The counts are 1, 0, 0. Each customer with no paid order receives a left-join output row with nulls for the order columns. COUNT(o.id) ignores those nulls. The query never touches line items because their grain, one row per item, is not needed for this result.

1. What changes if you move the status predicate to WHERE?

Putting o.status = 'paid' in WHERE after joining only on the customer ID eliminates customers 2 and 3. Customer 2 joins a pending order and fails the filter. Customer 3 has no order, so its status is null and the comparison is unknown, which the filter also excludes. Adding OR o.id IS NULL would recover customer 3 but not customer 2. To keep both, define “matching order” in the join condition or aggregate qualifying orders before the join.

2. Why does COUNT(*) return one for a customer with no order?

It counts the preserved customer row even when all fields from the joined table are null. To count only actual matches, count a right-side column that is non-null on a real match, such as a primary key. If someone suggests replacing nulls with zeros before counting, trace what that would do: zero is also a value, so the manufactured zero would count.

3. What breaks if you also join line items?

The paid order for customer 1 appears twice after joining the two item rows. COUNT(o.id) would report two, and summing the order total would double the revenue. COUNT(DISTINCT o.id) repairs that count, but SUM(DISTINCT o.total_cents) can discard legitimate orders with the same price. If the task also requires units sold, aggregate quantities to one row per order first, then join that result. Explain the grain of the new relation before writing the outer aggregate.

Keep the detail row when the question calls for it

4. How would you select the latest paid order per customer?

Assume each paid order has a non-null paid_at. Use ROW_NUMBER() with a deterministic tie breaker, then filter the result outside the window calculation:

WITH ranked AS (
  SELECT customer_id, id,
         ROW_NUMBER() OVER (
           PARTITION BY customer_id ORDER BY paid_at DESC, id DESC
         ) AS position
  FROM orders
  WHERE status = 'paid'
)
SELECT customer_id, id
FROM ranked
WHERE position = 1;

This returns no row for a customer with no paid orders. If “every customer” is part of the task, join the selected rows back to customers. If equal timestamps must all be returned, use RANK() ordered by timestamp alone and filter to rank one. The choice is a product requirement about ties, not just syntax.

5. Why might two rows show the same running total?

In PostgreSQL, with an ORDER BY and the default frame, a window includes peers that tie on all its ordering expressions. Two orders with the same timestamp may therefore both show a total that includes both amounts. If the required result is a running total after each individual order, define the order and the frame.

SELECT customer_id, id,
       SUM(total_cents) OVER (
         PARTITION BY customer_id ORDER BY paid_at, id
         ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
       ) AS running_cents
FROM orders
WHERE status = 'paid'
ORDER BY customer_id, paid_at, id;

The outer ORDER BY also makes the displayed row order explicit. If the business wants a total per day, combine orders into one row per customer and day first. Adding an ID tie breaker does not turn a per-order result into a per-day result.

Take a five-minute practice pass

  • Draw a customer with no orders, another with only a pending order, and a paid order with two items.
  • Predict the intermediate rows and final count before running the first query.
  • Give two paid orders identical amounts and timestamps; explain which query decisions each tie tests.
  • Explain one alternative solution and how you would verify its equivalence on those fixtures.

If this uncovered a weak spot, use it to choose what to study more deeply. Then try Mentaro's free practice demo to rehearse explaining your reasoning before checking an answer.

Sources