0PricingLogin
SQL Interview Prep · Lesson

The GROUP BY Rule for SELECT Columns

Why every non-aggregated column must appear in GROUP BY and the only-full-group-by mode.

Why Interviewers Start With GROUP BY

GROUP BY is where interviews separate juniors from mid-levels. The single rule they test most: every column in your SELECT list must either be inside an aggregate function or be listed in GROUP BY.

If you break this rule, the engine cannot decide which value to show for a group that has many rows. Interviewers plant this exact mistake to see if you understand what a group really is.

What a Group Actually Is

GROUP BY collapses many rows into one row per distinct key. After grouping, the engine no longer has individual rows. It only has one summary row per group.

  • Columns you grouped by have one clear value per group.
  • Aggregates like COUNT, SUM, AVG reduce the many values down to one.
  • Any other raw column is ambiguous: which of the many values should appear?
SELECT department, COUNT(*) AS headcount
FROM employees
GROUP BY department;

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