SQL Practice Logo

SQLPractice Online

HAVING Clause: Concept

Module: Aggregate Functions & Grouping

HAVING filters groups after GROUP BY, using aggregate functions. WHERE filters rows before grouping and cannot use aggregates.

**WHERE vs HAVING:**

WHERE:

- Filters individual rows BEFORE grouping

- Cannot use aggregate functions

- More efficient (reduces data before grouping)

HAVING:

- Filters groups AFTER aggregation

- Can use aggregate functions

- Necessary for group-level conditions

**Execution Order:**

1. FROM - Get table

2. WHERE - Filter rows

3. GROUP BY - Create groups

4. Aggregates - Calculate COUNT, SUM, etc.

5. HAVING - Filter groups

6. SELECT - Project columns

7. ORDER BY - Sort results

**Key Pattern:**

- Use WHERE for row filters (salary > 50000)

- Use HAVING for group filters (COUNT(*) > 10)

Critical for data analysts and BI developers. HAVING appears in most GROUP BY interview questions and is fundamental to creating filtered analytical reports.

HAVING enables threshold-based reporting: find customers with revenue >$10K, departments with >50 employees, products with ratings <3 stars. Essential for KPI dashboards and executive reports.