Question presented to candidate:
"Given a sales table and a reps table, show revenue per product and per region per month. Then explain what GROUP BY actually does to the rows, and why some databases reject SELECT product, rep_id, MAX(amount) ... GROUP BY product while others run it."
What a strong answer should cover:
- GROUP BY splits the filtered rows into groups with equal grouping keys and returns one row per group; aggregates are computed per group
- The single-value rule: every SELECT, HAVING and ORDER BY expression must be a grouping key, an aggregate, or functionally dependent on the keys (PostgreSQL allows other columns of a table grouped by its primary key)
- Engine differences: PostgreSQL and MySQL with
ONLY_FULL_GROUP_BYreject bare columns; SQLite accepts them and, with a single MIN or MAX, takes them from that row;ANY_VALUEis the explicit form - NULL keys form one group; grouping by an expression; no GROUP BY means one group (one row even on empty input), GROUP BY on empty input returns no rows
- Where it sits in the logical order (after WHERE, before HAVING), how engines execute it (hash or sort), and when to use a window function instead
Code / implementation expected: Yes β grouping by one column, by several columns and an expression, and the bare-column case.
Schema used by the examples: