Differentiate between WHERE and HAVING clauses in SQL.

Prepare for the TJR Bootcamp Test with flashcards and detailed questions. Get hints and explanations for each query. Ace your exam!

Multiple Choice

Differentiate between WHERE and HAVING clauses in SQL.

Explanation:
The key idea is how SQL handles filtering at different stages of a query: rows are filtered first, then groups are formed and filtered after aggregation. The where clause applies to each individual row, keeping only those that meet the condition before any grouping happens. After grouping (if you have a GROUP BY), the having clause applies to the resulting groups and can reference aggregate values like SUM, COUNT, or AVG to decide which groups to keep. For example, imagine selecting departments with more than five active employees: SELECT department, COUNT(*) as cnt FROM employees WHERE status = 'active' GROUP BY department HAVING COUNT(*) > 5; First, the where clause removes inactive employees. Then rows are grouped by department, counts are calculated for each group, and the having clause filters those groups to keep only departments with more than five active employees. That clarifies why the statement about where filtering rows before grouping and having filtering groups after aggregation is the correct one. The other options mix up the order or claim there’s no relationship between the two clauses.

The key idea is how SQL handles filtering at different stages of a query: rows are filtered first, then groups are formed and filtered after aggregation. The where clause applies to each individual row, keeping only those that meet the condition before any grouping happens. After grouping (if you have a GROUP BY), the having clause applies to the resulting groups and can reference aggregate values like SUM, COUNT, or AVG to decide which groups to keep.

For example, imagine selecting departments with more than five active employees:

SELECT department, COUNT(*) as cnt

FROM employees

WHERE status = 'active'

GROUP BY department

HAVING COUNT(*) > 5;

First, the where clause removes inactive employees. Then rows are grouped by department, counts are calculated for each group, and the having clause filters those groups to keep only departments with more than five active employees.

That clarifies why the statement about where filtering rows before grouping and having filtering groups after aggregation is the correct one. The other options mix up the order or claim there’s no relationship between the two clauses.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy