Why You Can't Group on Column Aliases

January 11, 2025 full-stack note-to-self

When SQLing, I sometimes run across this -- I do

SELECT department AS dept, COUNT(*)
FROM employees
GROUP BY dept;

and it won't work and I wonder why. Here's why: The GROUP BY clause is processed before the SELECT. Usually, the order is:

  1. FROM: Where the data comes from
  2. WHERE: Filters the data from the source (from)
  3. GROUP BY: If necessary, the data is grouped
  4. HAVING: If necessary, the groups are filtered by the aggregate conditions
  5. SELECT: The selected columns, including any aliases, are processed

To reference the same column in both the SELECT and GROUP BY clauses, you should use the actual column name instead of the alias:

SELECT department AS dept, COUNT(*)
FROM employees
GROUP BY department;
Most posts are for my own reference and reflection, and shouldn’t be taken as fully accurate or instructional.