Data Analyst Interview
Menu
Browse in your language. All mock interviews, preparation sessions and feedback are in English only.
SQL · Learning guide

SQL GROUP BY, aggregates and HAVING

Define what one output row represents, then summarise the input and inspect missing values.

Start the worked examples ↓

Summarise orders by status

This query changes the grain from one row per order to one row per status. The pending order has an unknown amount.

WITH orders(order_id, customer_id, amount_usd, status) AS (
  VALUES (101, 1, 50.0, 'completed'),
         (102, 1, 30.0, 'returned'),
         (103, 2, 80.0, 'completed'),
         (104, 3, NULL, 'pending')
)
SELECT status, COUNT(*) AS order_count,
  COUNT(amount_usd) AS known_amount_count,
  SUM(amount_usd) AS total_usd, AVG(amount_usd) AS average_usd
FROM orders
GROUP BY status
ORDER BY status;

Expected: completed orders total $130 with a $65 mean. The pending group has one order, zero known amounts, and NULL for both sum and mean.

Understand the denominator

COUNT(*) counts input rows; COUNT(amount_usd) counts known amounts. AVG ignores NULL amounts. That is a mean of observed values, not proof of the mean across missing observations. Missingness can bias a result in either direction.

SUM over no known values is NULL. Replacing it with zero is a reporting decision. Explain whether zero means no qualifying transactions or whether the underlying value is unknown.

Filter rows before groups

WITH orders(order_id, customer_id, amount_usd, status) AS (
  VALUES (101, 1, 50.0, 'completed'),
         (102, 1, 30.0, 'returned'),
         (103, 2, 80.0, 'completed'),
         (104, 3, NULL, 'pending')
)
SELECT customer_id, SUM(amount_usd) AS completed_usd
FROM orders
WHERE status = 'completed'
GROUP BY customer_id
HAVING SUM(amount_usd) >= 60
ORDER BY customer_id;

WHERE removes non-completed orders. HAVING keeps customer groups worth at least $60; only customer 2 remains. Include non-aggregated dimensions in GROUP BY for clear, portable queries.

Count conditions in one result

WITH orders(order_id, customer_id, amount_usd, status) AS (
  VALUES (101, 1, 50.0, 'completed'),
         (102, 1, 30.0, 'returned'),
         (103, 2, 80.0, 'completed'),
         (104, 3, NULL, 'pending')
)
SELECT COUNT(*) AS orders,
  SUM(CASE WHEN status = 'completed' THEN 1 ELSE 0 END) AS completed
FROM orders;

Expect four orders and two completed orders. Before turning this into a rate, state whether pending and returned orders belong in the denominator.

Does a missing value count as zero?

No. NULL marks an unknown or absent value; zero is a known number. In the pending-order example, COUNT(*) is 1 but COUNT(amount_usd) is 0. Replacing the unknown amount with zero would change the reported mean from unknown to $0, even though no amount was observed.

Check both the number of rows and the number of known values before describing an average. If only some amounts are known, label the result as the average of observed amounts and report coverage. A high coverage rate alone does not establish that the missing values resemble the observed ones.

The same rule affects user metrics: counting events, non-null customer IDs and distinct customer IDs answers three different questions. Try the missing customer-ID audit with expected SQL results before using an event table to report user growth.

Continue learning

Reference: PostgreSQL tutorial. Check behaviour for your installed version.

These are educational exercises with fictional data, not a verified employer question bank. Amounts in examples are USD unless otherwise stated.

Practise your explanation

Use the self-practice guide to review your approach. Mock-interview booking and payment are currently inactive. All planned preparation sessions and feedback are in English.

View preparation options
✉ WhatsApp