QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › Interview tasks

Task: the number and sum of transactions by month.

Count and sum by month use grouping on DATE_TRUNC('month', created_at) with the status filter in WHERE. Grouping does not create groups absent from the data, so a month with no transactions disappears. To show it with zero, a calendar is generated and joined with LEFT JOIN with conditions in ON, counting COUNT(t.id). The report's time zone is agreed, because it shifts transactions at month boundaries.

SQL · Tasks

The quarterly report has no September. Did the month with no transactions simply vanish?

A "count and sum by month" report appears in every project with payments, and QA reconciles it with data from other sources. The interview task checks two things: whether the candidate can group time by a period and whether they know that grouping does not create groups that are absent from the data. The doubts here usually come from time zones and empty months.

idcreated_atamountstatus
12026-08-05100completed
22026-08-2050completed
32026-08-2570failed
42026-10-02200completed
52026-10-1530completed

How it works and why. DATE_TRUNC('month', created_at) truncates a point in time to the start of the month: every August date becomes 2026-08-01 00:00, and these values are equal, so they can be grouped. The filter status = 'completed' sits in WHERE and works before grouping. A group is built from rows that passed the filter, so a month with none does not appear in the result. This is not a DBMS bug but a property of grouping.

SELECT DATE_TRUNC('month', created_at)::date AS month,
       COUNT(*)    AS tx_count,
       SUM(amount) AS volume
FROM transactions
WHERE status = 'completed'
GROUP BY 1
ORDER BY 1;
-- 2026-08-01 | 2 | 150
-- 2026-10-01 | 2 | 230

In MySQL the counterpart is DATE_FORMAT(created_at, '%Y-%m'). August gives 2 and 150, because the failed transaction of 70 is filtered out. September is absent.

Example: align the report with a calendar. The months must be generated separately and the transactions joined to them with a LEFT JOIN. Conditions on the right side, including the status, go in ON: in WHERE they would drop the empty months and bring the same problem back. Count with COUNT(t.id), not COUNT(*): for an empty month COUNT(*) would give 1 (the one calendar row), while COUNT(t.id) gives 0.

SELECT m.month::date AS month,
       COUNT(t.id) AS tx_count,
       COALESCE(SUM(t.amount), 0) AS volume
FROM generate_series(timestamp '2026-08-01',
                     timestamp '2026-10-01',
                     interval '1 month') AS m(month)
LEFT JOIN transactions t
  ON t.created_at >= m.month
 AND t.created_at <  m.month + interval '1 month'
 AND t.status = 'completed'
GROUP BY m.month
ORDER BY m.month;
-- 2026-08-01 | 2 | 150
-- 2026-09-01 | 0 | 0
-- 2026-10-01 | 2 | 230

Groups exist only where rows exist

GROUP BY
monthcntvolume
2026-08-012150
2026-10-012230
Calendar + LEFT JOIN
monthcntvolume
2026-08-012150
2026-09-0100 added
2026-10-012230
Figure 1. September is not in the first result at all, rather than "with zero". A report reading it will skip the month silently.

COALESCE replaces the NULL sum of the empty month with zero. The month boundaries are a range [start; start + 1 month) rather than BETWEEN, so the last day is not lost.

Limits and alternatives. A typical source of mismatches with a report is the time zone. If created_at is timestamptz, DATE_TRUNC works in the session time zone. A transaction from 31 October 23:30 UTC in a session with the Europe/Kyiv zone (after daylight saving ends this is UTC+2) lands in November, while in UTC it stays in October. So agree which zone the report uses and set it explicitly: DATE_TRUNC('month', created_at AT TIME ZONE 'UTC'). On bigger tables grouping by an expression reads everything, so ranges over an indexed column are faster.

Common mistakes.

  • Assuming an empty month will appear: groups come only from existing rows.
  • A filter on the right table in WHERE after a LEFT JOIN: the empty months vanish again.
  • COUNT(*) instead of COUNT(t.id) in the aligned report: an empty month shows 1.
  • Not fixing the time zone: the sum on a month boundary differs from the report.

Check yourself

In the aligned query COUNT(t.id) was replaced with COUNT(*). What does September show?

Answer

A count of 1 instead of 0. For September the LEFT JOIN gives one row: the calendar row with NULL in the transaction columns. COUNT(*) counts rows, COUNT(t.id) only those where the id is not NULL.

A transaction was created at 2026-10-31 23:30 UTC, the column is timestamptz, the session zone is Europe/Kyiv. Which month does DATE_TRUNC('month', created_at) put it in?

Answer

In November: in Kyiv it is already 1 November 01:30 (UTC+2). If the report is computed in UTC, the transaction belongs to October. The zone must be fixed in the requirements and in the query.

In an interview. Junior: I truncate the date to the month with DATE_TRUNC, group by it and count the number and the sum. I put the status filter in WHERE. Middle/Senior: I point out that months without transactions will not appear: if they are needed with zeros, I generate a calendar with generate_series and attach it with a LEFT JOIN with the conditions in ON. I also ask which time zone the report is computed in.

More articles in this chapter