0PricingLogin
SQL Interview Prep · Lesson

Grouping by Multiple Columns and Expressions

Composite grouping keys and grouping on computed values.

Beyond a Single Grouping Key

Real reports rarely group by one column. Interviewers escalate from 'sales per region' to 'sales per region per month' to see if you understand composite grouping keys.

The rule scales cleanly: listing more columns in GROUP BY creates one row per distinct combination of those columns.

What Multiple Columns Mean

When you write GROUP BY region, product, the group key is the pair (region, product). Each unique pair becomes one output row.

  • 5 regions and 4 products yield up to 20 groups.
  • Combinations that never occur produce no row at all.
  • Order of columns in GROUP BY does not change the result set, only sometimes the plan.
SELECT region, product, SUM(amount) AS total
FROM sales
GROUP BY region, product;

All lessons in this course

  1. The GROUP BY Rule for SELECT Columns
  2. HAVING vs WHERE
  3. Grouping by Multiple Columns and Expressions
  4. Counting and Filtering Groups
← Back to SQL Interview Prep