GROUP BY
clause, allowing you to filter groups based on a specified condition. Unlike the WHERE
clause, which is used to filter rows before they are grouped, the HAVING
clause is applied to each group of grouped data.SQL Group Filtering: SQL provides different clauses for filtering data at different stages of query execution.
Key Differences:
Example Demonstration:
SELECT department, AVG(salary) FROM employees GROUP BY department HAVING AVG(salary) > 50000; ← Filters after grouping
Why HAVING is correct:
The correct answer is (2) having, as it's the only clause that applies conditions after groups are formed.
On a relation named Loan of a bank: