What is the purpose of the SQL HAVING clause?
a) To filter groups based on a condition after aggregation
b) To filter individual rows before aggregation
c) To order the result set in descending order
d) To calculate the sum of numeric columns
Answer:
a) To filter groups based on a condition after aggregation
Explanation:
The HAVING clause in SQL is used to filter groups based on a specified condition after the aggregation has been performed. It is commonly used with GROUP BY to restrict the result set to groups that meet a specific condition.
For example, SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 5
will only include departments that have more than five employees.
HAVING is similar to WHERE, but WHERE is used to filter individual rows before aggregation, while HAVING filters groups after aggregation.