What does SQL clause "GROUP BY 1" mean?

What does SQL clause "GROUP BY 1" mean?

SELECT account_id, open_emp_id
         ^^^^        ^^^^
          1           2

FROM account
GROUP BY 1;

It means to group by the first column of your result set regardless of what it's called. You can do the same with ORDER BY.

And it should never be used in production code unless you absolutely have to, eg. if you're dynamically selecting columns. It's hard to read and when someone modifies the select statement without also fixing the ordinals it can silently cause catastrophic issues.


What does SQL clause "GROUP BY 1" mean?
https://shikai.info/archives/what-does-sql-clause-group-by-1-mean
作者
石 凯
发布于
2023年08月10日
许可协议