Multiple choice technology databases

Can you use both HAVING and WHERE SQL clauses in one SQL statement?

  1. Yes

  2. No

  3. Sometimes possible

  4. Can't Say

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

HAVING and WHERE can both be used in the same SQL statement. WHERE filters individual rows before grouping, while HAVING filters groups after aggregation. For example: SELECT department, COUNT() FROM employees WHERE salary > 50000 GROUP BY department HAVING COUNT() > 5. The WHERE clause filters rows first, then GROUP BY creates groups, then HAVING filters those groups.

AI explanation

Yes is correct. WHERE and HAVING serve different, complementary filtering stages in a SQL query and are commonly used together: WHERE filters individual rows before any grouping/aggregation happens, while HAVING filters groups after GROUP BY aggregation, typically based on an aggregate condition (e.g., HAVING COUNT(*) > 5). A typical combined query looks like: SELECT dept, COUNT(*) FROM employees WHERE active = true GROUP BY dept HAVING COUNT(*) > 5 — WHERE trims rows first, then HAVING trims the resulting groups. This is standard, well-supported SQL, so 'No', 'Sometimes possible', and 'Can't Say' are all incorrect.