Having: if you want to use a filter that contains an aggregate function, you can't use WHERE.

Study for the Database Systems Test. Prepare with flashcards and multiple choice questions, each with hints and explanations. Get ready for success!

Multiple Choice

Having: if you want to use a filter that contains an aggregate function, you can't use WHERE.

Explanation:
The main idea is that filtering based on an aggregate must happen after the data has been grouped. WHERE runs before aggregation and evaluates each row individually, so it can’t reference the result of an aggregate like SUM or AVG. After you group the rows and compute those aggregates for each group, HAVING lets you filter those groups based on the aggregate values. For example, to keep only departments whose total sales exceed a threshold, you’d write something like: SELECT department, SUM(sales) AS total_sales FROM sales GROUP BY department HAVING SUM(sales) > 10000. If you tried to put that condition in WHERE, it wouldn’t work because the aggregate hasn’t been computed yet. The other clauses serve different roles: GROUP BY creates the groups for aggregation, and ORDER BY just sorts the final result without filtering.

The main idea is that filtering based on an aggregate must happen after the data has been grouped. WHERE runs before aggregation and evaluates each row individually, so it can’t reference the result of an aggregate like SUM or AVG. After you group the rows and compute those aggregates for each group, HAVING lets you filter those groups based on the aggregate values. For example, to keep only departments whose total sales exceed a threshold, you’d write something like: SELECT department, SUM(sales) AS total_sales FROM sales GROUP BY department HAVING SUM(sales) > 10000. If you tried to put that condition in WHERE, it wouldn’t work because the aggregate hasn’t been computed yet. The other clauses serve different roles: GROUP BY creates the groups for aggregation, and ORDER BY just sorts the final result without filtering.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy