0
0
PostgreSQLquery~10 mins

HAVING for filtering groups in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - HAVING for filtering groups
Start with Table Data
GROUP BY specified columns
Calculate aggregates per group
Apply HAVING condition on groups
Yes | No
Exclude group
Return groups that meet HAVING condition
Data is grouped by columns, aggregates are calculated, then HAVING filters groups based on conditions.
Execution Sample
PostgreSQL
SELECT department, COUNT(*) AS emp_count
FROM employees
GROUP BY department
HAVING COUNT(*) > 2;
This query groups employees by department and returns only departments with more than 2 employees.
Execution Table
StepActionGroups FormedAggregate CalculatedHAVING ConditionGroup Included?
1Group employees by department[Sales, HR, IT]Count employees in each groupN/AN/A
2Calculate counts[Sales=3, HR=2, IT=4]Sales=3, HR=2, IT=4N/AN/A
3Apply HAVING COUNT(*) > 2Same groupsSame countsSales: 3 > 2 = TrueYes
4Apply HAVING COUNT(*) > 2Same groupsSame countsHR: 2 > 2 = FalseNo
5Apply HAVING COUNT(*) > 2Same groupsSame countsIT: 4 > 2 = TrueYes
6Return filtered groups[Sales, IT]Counts: 3, 4Condition metIncluded
💡 All groups checked; only those with count > 2 are included.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
GroupsNone[Sales, HR, IT][Sales=3, HR=2, IT=4][Sales=3, HR=2, IT=4][Sales=3, IT=4]
HAVING Condition ResultN/AN/AN/A[True, False, True][True, False]
Key Moments - 2 Insights
Why doesn't HAVING filter individual rows like WHERE?
HAVING filters after grouping and aggregation (see execution_table rows 3-5), so it works on groups, not single rows.
What happens if HAVING condition is false for a group?
That group is excluded from the final result (see execution_table row 4 where HR group is excluded).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the HAVING condition result for HR group?
ATrue
BNot evaluated
CFalse
DError
💡 Hint
Check the 'HAVING Condition' column at step 4 for HR group.
At which step are groups formed from the table data?
AStep 2
BStep 1
CStep 3
DStep 6
💡 Hint
Look at the 'Action' column to find when grouping happens.
If HAVING condition changed to COUNT(*) > 4, which groups would be included?
ANo groups
BOnly IT
COnly Sales
DSales and IT
💡 Hint
Refer to 'Aggregate Calculated' counts and compare with new condition.
Concept Snapshot
HAVING filters groups after GROUP BY and aggregation.
Syntax: SELECT columns FROM table GROUP BY columns HAVING condition;
HAVING works on aggregated data, unlike WHERE.
Use HAVING to filter groups, e.g., HAVING COUNT(*) > 2.
Groups failing HAVING condition are excluded from results.
Full Transcript
The HAVING clause filters groups formed by GROUP BY after aggregates are calculated. First, data is grouped by specified columns. Then, aggregate functions like COUNT calculate values per group. HAVING applies conditions on these aggregates to decide which groups to keep. Groups not meeting the HAVING condition are excluded from the final output. This differs from WHERE, which filters rows before grouping. For example, grouping employees by department and counting them, HAVING COUNT(*) > 2 returns only departments with more than two employees.