Question presented to candidate: "Using an orders table, walk me through the aggregate functions you use most. What does each return, how do they treat NULLs and empty input, and how would you compute several different totals in one pass over the table?"
What a strong answer should cover:
- The core five:
COUNT,SUM,AVG,MIN,MAX; each turns many rows into one value (per group with GROUP BY, for the whole table without) - NULL handling: all of them except
COUNT(*)skip NULLs, soAVG(col)divides by the non-NULL count; over zero rowsCOUNTis 0 and the others are NULL - Types:
MIN/MAXwork on text and dates,SUM/AVGneed numbers; integerSUM(x) / COUNT(*)truncates,AVGdoes not - Conditional aggregation with
FILTER (WHERE ...)orSUM(CASE ...), so one scan answers several questions - Beyond the core: string aggregation (
string_agg,GROUP_CONCAT), statistics (stddev,percentile_cont),bool_and, and the difference between an aggregate and a window function
Code / implementation expected: Yes β one query with several aggregates, and one with conditional aggregates per region.
Schema used by the examples: