Junior — Middle
In what situations is the GROUP BY operator used without the HAVING condition?
sobes.tech AI
Answer from AI
The GROUP BY operator is used to group the result rows by one or more columns to apply aggregate functions (such as SUM, COUNT, AVG) to each group.
HAVING is used to filter groups after they are formed, similar to how WHERE filters rows before grouping.
Situations when GROUP BY is used without HAVING:
- To obtain aggregated data by groups without additional filtering, for example, counting the number of records in each group.
- When it is sufficient to group data and display aggregates for all groups.
Example:
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;
Here, we simply group employees by departments and count the number in each, without filtering groups.