Back to AGGREGATIONS & GROUP BY, ORDER BY

HAVING CLAUSE AND WHERE

Understand filtering with GROUP BY and difference between HAVING and WHERE

4 minutesVideo LessonPDF notes
🎯 Free Guest Mode: You are learning for free. Sign in to save your completion progress and quiz answers.

Ready to continue?

Mark this lesson as complete when you're ready to proceed.

Key moments

  1. WHERE Clause Basics — The WHERE clause is introduced as a tool for filtering data at the row level.
  2. HAVING Clause Introduction — HAVING is explained as the necessary tool for filtering results based on aggregate functions after grouping.
  3. Combined Filtering — A detailed example demonstrates how to use WHERE and HAVING together in a single query.
  4. WHERE Execution — The instructor clarifies that WHERE runs before aggregation, preventing the use of aggregate functions.
  5. HAVING Execution — The execution timing of HAVING is reinforced, showing it operates on the grouped output.
PDF notes

Frequently asked questions

Why can't I use WHERE with AVG()?

WHERE executes before the data is grouped, so the average (AVG) has not yet been calculated and is unavailable for filtering.

If I don't use GROUP BY, can I still use HAVING?

No, HAVING requires grouping, even if the entire result set is treated as a single group implicitly by the database engine.

Does the order of WHERE and HAVING matter?

Yes, WHERE must always come first to filter rows, followed by GROUP BY, and then HAVING to filter the resulting groups.

How was this lesson?

Your feedback helps us refine explanations and catch bugs.