This lesson on HAVING CLAUSE AND WHERE is hands-on and example-driven. You will be able to correctly apply filtering logic in SQL queries that use aggregation. You will know when to use the WHERE clause for row-level filtering and when to use the HAVING clause for filtering aggregated results. This ensures your queries execute efficiently and without errors related to aggregate function usage.
What You'll Be Able To Do
- Differentiate between row-level and group-level filtering in SQL.
- Construct queries that correctly use aggregate functions in the HAVING clause.
- Explain why aggregate functions cannot be used directly in the WHERE clause.
- Combine WHERE and HAVING clauses effectively within a single query.
- Identify the correct execution order of WHERE, GROUP BY, and HAVING.
Topics Covered in HAVING CLAUSE AND WHERE
- WHERE Aggregate Error (0:00 - 0:55) — Attempting to filter by an aggregate function in the WHERE clause results in an error because the aggregation has not yet occurred.
- HAVING Clause Purpose (0:55 - 1:45) — The HAVING clause was created specifically to filter results based on aggregate functions after the GROUP BY clause executes.
- Basic HAVING Example (1:45 - 2:05) — A successful query filters groups where the average age is greater than 40 using HAVING.
- WHERE Row Filtering (2:05 - 3:15) — The WHERE clause is used first to filter individual rows, such as selecting only occupations that are managers.
- Combined Filtering Logic (3:15 - 3:50) — Both WHERE and HAVING are used in one query to filter rows first, then filter the resulting aggregated groups.
- Summary and Distinction (3:50 - 4:15) — WHERE filters row data before grouping, while HAVING filters aggregated results after grouping.
SQL Cheat Sheet
-
WHERE <condition>— Filters individual rows before grouping or aggregation occursSELECT * FROM salary WHERE occupation LIKE '%manager%'; -
GROUP BY <col>— Groups rows based on identical values in specified columnsSELECT gender, AVG(age) FROM demographics GROUP BY gender; -
HAVING <aggregate condition>— Filters groups based on the results of aggregate functionsSELECT gender, AVG(age) FROM demographics GROUP BY gender HAVING AVG(age) > 40; -
WHERE ... GROUP BY ... HAVING ...— Filters rows first, then groups, then filters the resulting groupsSELECT occupation, AVG(salary) FROM salary WHERE occupation LIKE '%manager%' GROUP BY occupation HAVING AVG(salary) > 75000;
Comparison Table
| Clause | Filters | Execution Order | Function Usage |
|---|---|---|---|
| WHERE | Individual rows | Before GROUP BY | Cannot use aggregates |
| HAVING | Groups or aggregates | After GROUP BY | Must use aggregates |
| Both | Rows then groups | WHERE -> GROUP BY -> HAVING | Filters at two levels |
Common Pitfalls
- Mistake: Using an aggregate function like AVG() in the WHERE clause. Avoid: Use HAVING instead of WHERE when filtering based on aggregate results.
- Mistake: Placing HAVING before the GROUP BY clause in the query. Avoid: HAVING must always come immediately after the GROUP BY clause.
- Mistake: Trying to filter non-aggregated columns using HAVING. Avoid: Use WHERE for filtering non-aggregated columns at the row level.
FAQs
- Why does using an aggregate function in WHERE cause an error? The aggregate function result has not been calculated yet because the grouping (GROUP BY) has not occurred when WHERE executes.
- Can I use both WHERE and HAVING in the same query? Yes, WHERE filters the initial rows, and HAVING filters the resulting groups after aggregation.
- Does HAVING replace WHERE? No, they serve different purposes; WHERE filters rows, and HAVING filters groups based on aggregate results.