Differentiate between WHERE and HAVING clauses in SQL.

Prepare for the TJR Bootcamp Test with quizzes and flashcards. Each question includes hints and explanations to boost your readiness for the exam!

Multiple Choice

Differentiate between WHERE and HAVING clauses in SQL.

The key idea is understanding the order in which SQL evaluates a query. WHERE runs first, filtering individual rows from the table before any grouping or aggregation happens. HAVING runs after the rows have been grouped and aggregate values have been computed, so it can filter those groups based on the results of the aggregation.

For example, to count how many active employees there are in each department but only for departments with a lot of active staff, you’d filter first with WHERE to keep only active rows, then GROUP BY department, and finally use HAVING to keep only those departments where the count exceeds a threshold:

SELECT department, COUNT(*) AS active_count

FROM employees

WHERE status = 'active'

GROUP BY department

HAVING COUNT(*) > 3;

This demonstrates why the statement that WHERE filters rows before grouping and HAVING filters groups after aggregation is the correct distinction. HAVING can reference aggregate functions (like COUNT, AVG, SUM), which is not possible in WHERE.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy