How do you group data by multiple fields?
To group data by multiple fields, list several columns separated by commas in GROUP BY.
Example
sql
SELECT department, position, COUNT(*)
FROM employees
GROUP BY department, position;Here:
- SQL groups by
departmentfirst, - then, within each department, by
position.
The result ends up looking like, for example:
- Department "Sales" + Position "Manager" -> 5 people
- Department "Sales" + Position "Assistant" -> 3 people
So the result is every unique combination of values from the listed fields.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.