Which SQL clause is used to filter groups based on a condition?
a) HAVING
b) WHERE
c) GROUP BY
d) ORDER BY
Answer:
a) HAVING
Explanation:
The HAVING clause in SQL is used to filter groups based on a condition after the GROUP BY clause has been applied. HAVING is similar to the WHERE clause, but WHERE filters individual rows before aggregation, while HAVING filters the groups after aggregation.
For example, SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 5
will return only those departments with more than five employees.
HAVING is essential when you need to apply conditions to grouped data, such as filtering by aggregate functions like COUNT(), SUM(), or AVG().