GROUP BY and Nonaggregate Operations
Nonaggregated variables in SELECT, ORDER BY, and HAVING need to appear in the group by list.
All nonaggregate groups in a SELECT expression list or HAVING expression list must be included in the GROUP BY clause.