GROUP BY can be combined with COUNT to determine how many records belong to each group. This creates a summarized count for every unique value in the grouping column.
Counting records by category is a common analytical requirement. Analysts may need to know how many employees are in each department, customers in each city, or orders in each region.
An HR manager wants to know how many employees work in each department. GROUP BY Department combined with COUNT(*) provides the required summary.
SELECT
Department,
COUNT(*) AS EmployeeCount
FROM Employees
GROUP BY Department;
This pattern is useful for employee analysis, customer counts, order reporting, regional analysis, product counts, and operational dashboards.
- GROUP BY can be combined with COUNT.
- COUNT calculates the number of records in each group.
- Each group produces a summarized result.
- This pattern is common in business reporting.
- The result can be sorted using ORDER BY.
How do you count records by department?
Use GROUP BY Department together with COUNT(*).
What does COUNT(*) count in each GROUP BY group?
It counts the rows belonging to each individual group.