Quick revision
The short answer
Use conditional aggregation: filter the rows to the first quarter of 2026 in WHERE, group by product, and add one SUM(CASE WHEN sold_on is in that month THEN amount ELSE 0 END) per column. PostgreSQL offers the shorter SUM(amount) FILTER (WHERE ...) form, which returns NULL instead of 0 for an empty month, so we wrap it in COALESCE; PostgreSQL has no PIVOT keyword, and the tablefunc extension's crosstab function is its built-in pivot, with the output columns declared by hand.
What to remember
In this answer, we pivoted sales into one row per product with a column per month using conditional aggregation, the portable SUM(CASE ...) form, and the PostgreSQL FILTER form, which needs COALESCE because an empty month sums to NULL. We saw that PostgreSQL has no PIVOT keyword but offers crosstab from tablefunc with declared output columns, while SQL Server and Oracle have PIVOT and MySQL relies on conditional aggregation, and that filtering the date range in WHERE lets indexes and partition pruning work.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Use conditional aggregation: filter the rows to the first quarter of 2026 in WHERE, group by product, and add one SUM(CASE WHEN sold_on is in that month THEN amount ELSE 0 END) per column. PostgreSQL offers the shorter SUM(amount) FILTER (WHERE ...) form, which returns NULL instead of 0 for an empty month, so we wrap it in COALESCE; PostgreSQL has no PIVOT keyword, and the tablefunc extension's crosstab function is its built-in pivot, with the output columns declared by hand.
Why this question is asked
Pivot reports, one row per thing and one column per period, are what business users ask for constantly, because that is the shape of a spreadsheet. Data is stored the other way round, one row per sale, so we have to turn rows into columns. Interviewers ask this to see whether we know the portable technique, the PostgreSQL shortcut and the limits of each database. In this answer, we will recap what pivoting means, write the portable conditional aggregation query, rewrite it with FILTER, look at crosstab and the PIVOT keyword in other databases, and see why the WHERE clause matters for performance.
1A Quick Recap of Pivoting
The sales table is in long format: each row is one sale with a product, a date and an amount. The report wants wide format: one row per product and a column per month. Pivoting is the transformation from the first to the second.
In SQL, the number and names of output columns must be known when the query is written. So a pivot in SQL always lists its columns explicitly, here jan, feb and mar. That fact will matter a lot when columns are dynamic, but for three fixed months it is no problem.
2The Portable Way: Conditional Aggregation
Grouping by product gives one row per product. Inside each group, a CASE expression keeps the amount only for the rows of one month and contributes 0 for the others, and SUM adds them up. One such expression per month produces one column per month.
| product | sold_on | amount | goes to column |
|---|---|---|---|
| Lamp | 2026-01-10 | 40 | jan |
| Lamp | 2026-01-22 | 10 | jan |
| Lamp | 2026-03-05 | 25 | mar |
| Desk | 2026-02-14 | 300 | feb |
This works in PostgreSQL, MySQL, SQL Server, Oracle and almost any SQL database, which is why it is the answer to portable SQL. Notice the half-open date ranges: >= the first day and < the first day of the next month. They work even if sold_on later becomes a timestamp, where a BETWEEN ending on the 31st would silently drop the last day's afternoon.
3The PostgreSQL FILTER Form
PostgreSQL supports the standard FILTER clause on aggregates, which says which rows an aggregate should see. It replaces the CASE and reads more clearly.
There is one difference to remember. With CASE ... ELSE 0, an empty month sums zeros and gives 0. With FILTER, an empty month means SUM sees no rows at all, and SUM of no rows is NULL. That is why we wrap each column in COALESCE. Testing on the month number with extract is safe here only because the WHERE clause already limits the rows to one year; without it, January 2025 would be added to jan as well.
4Crosstab and the PIVOT Keyword
PostgreSQL has no PIVOT keyword. The tablefunc extension provides crosstab, which takes a query returning row name, category and value, and spreads the categories into columns. Because a function in SQL must have a known result type, we declare the output columns ourselves.
The second argument lists the categories, so a month without sales lands in the right column as NULL instead of shifting values left. In practice, many teams skip crosstab because it adds an extension and is less readable than conditional aggregation.
SQL Server and Oracle do have a PIVOT clause, for example PIVOT (SUM(amount) FOR month IN (1 AS jan, 2 AS feb, 3 AS mar)) in Oracle, but it too needs the columns listed. MySQL has no PIVOT and uses conditional aggregation, where SUM(IF(...)) is a common shorthand for the CASE.
5Filter First So Indexes and Partitions Help
We could leave out the WHERE clause, since the CASE expressions already ignore other dates. The query would still be correct, but it would read every sale ever recorded, evaluate three CASE expressions on each one and produce rows for products that sold nothing in the quarter.
With the date range in WHERE, PostgreSQL can use an index on sold_on, or prune partitions if sales is partitioned by month, so only the first quarter's rows are read at all. The CASE or FILTER expressions then only distribute those rows into columns. Notice also that the WHERE condition compares the raw column with constants, which keeps it index-friendly, while extract(month FROM sold_on) in WHERE would not be.
6Follow-up questions
How would you add a total column and a total row?
The total column is one more aggregate, SUM(amount) AS total, since WHERE already limits the rows to the quarter. For a total row, group with GROUP BY ROLLUP (product), which adds a grand total row where product is NULL; we can label it with COALESCE(product, 'Total') or, more safely, with GROUPING(product) = 1, which distinguishes the total row from a product that is genuinely NULL. In MySQL the syntax is GROUP BY product WITH ROLLUP.
Why does the FILTER version need COALESCE but the CASE version does not?
Because CASE ... ELSE 0 feeds a 0 for every non-matching row, so the sum of an empty month is 0. FILTER removes the non-matching rows before the aggregate, so for an empty month SUM receives no input and returns NULL. Writing CASE without ELSE 0 would also return NULL.
Which form would you choose in a real codebase?
On PostgreSQL only, the FILTER form, for readability. In code that must run on several databases, or when colleagues may port it, conditional aggregation with CASE. I would avoid crosstab unless the extension is already used.
7Conclusion
In this answer, we pivoted sales into one row per product with a column per month using conditional aggregation, the portable SUM(CASE ...) form, and the PostgreSQL FILTER form, which needs COALESCE because an empty month sums to NULL. We saw that PostgreSQL has no PIVOT keyword but offers crosstab from tablefunc with declared output columns, while SQL Server and Oracle have PIVOT and MySQL relies on conditional aggregation, and that filtering the date range in WHERE lets indexes and partition pruning work.
Open with
I would use conditional aggregation because it works on every database. I filter the rows to the first quarter in WHERE, with sold_on greater or equal to January 1 and less than April 1, group by product, and add one column per month as SUM of CASE WHEN sold_on is inside that month's half-open range THEN amount ELSE 0 END.
Then, if they want more
Each CASE keeps only its month's amounts, so the sums land in the right columns, and ELSE 0 makes empty months show zero. In PostgreSQL I can write it more readably with the FILTER clause: SUM of amount FILTER WHERE the month is one, and so on.
The difference is that FILTER gives NULL for a month with no rows, because SUM of nothing is NULL, so I wrap each column in COALESCE with zero.
PostgreSQL has no PIVOT keyword; the tablefunc extension's crosstab function pivots a row, category, value query, but I must declare the output columns and types myself, and I pass the list of categories so missing months do not shift. SQL Server and Oracle have a PIVOT clause, which also needs the column list, and MySQL uses conditional aggregation.
Finally, keeping the date range in WHERE matters for performance, because then an index on sold_on or partition pruning limits the rows read before the CASE expressions run.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, The SQL Language
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Table sales(product text, sold_on date, amount numeric). Produce one row per product...": https://scrivio-interview-library.netlify.app/databases/sql-coding-problems/3748c52c9e/