Which clause is used to filter groups after a GROUP BY based on a condition on aggregates?

Study for the SQL Basics Test. Improve your knowledge with multiple choice questions and detailed explanations. Prepare effectively to master SQL concepts!

Multiple Choice

Which clause is used to filter groups after a GROUP BY based on a condition on aggregates?

Explanation:
When you group rows, you often want to keep only those groups whose aggregated values meet a condition. That’s what the HAVING clause does: it filters the results after the GROUP BY has created the groups and the aggregates have been computed, so you can use aggregate results like SUM, COUNT, AVG, or MAX in the condition. For example, asking for departments with more than 10 employees uses HAVING to apply the filter to each group’s COUNT(*) value after grouping: SELECT department, COUNT(*) AS total FROM employees GROUP BY department HAVING COUNT(*) > 10; If you used WHERE, it would filter individual rows before any grouping or aggregation, so it couldn’t reference the grouped aggregates. GROUP BY itself only defines the groups; it doesn’t filter the groups. FILTER is related to how a specific aggregate is calculated in some dialects, not a general mechanism to filter groups after grouping.

When you group rows, you often want to keep only those groups whose aggregated values meet a condition. That’s what the HAVING clause does: it filters the results after the GROUP BY has created the groups and the aggregates have been computed, so you can use aggregate results like SUM, COUNT, AVG, or MAX in the condition.

For example, asking for departments with more than 10 employees uses HAVING to apply the filter to each group’s COUNT(*) value after grouping:

SELECT department, COUNT() AS total FROM employees GROUP BY department HAVING COUNT() > 10;

If you used WHERE, it would filter individual rows before any grouping or aggregation, so it couldn’t reference the grouped aggregates. GROUP BY itself only defines the groups; it doesn’t filter the groups. FILTER is related to how a specific aggregate is calculated in some dialects, not a general mechanism to filter groups after grouping.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy