0
0
MySQLquery~10 mins

HAVING clause in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - HAVING clause
Start with a table
GROUP BY groups rows
Aggregate functions calculate
HAVING filters groups
Return filtered groups
The HAVING clause filters groups created by GROUP BY after aggregation, unlike WHERE which filters rows before grouping.
Execution Sample
MySQL
SELECT department, COUNT(*) AS emp_count
FROM employees
GROUP BY department
HAVING emp_count > 2;
This query counts employees per department and returns only departments with more than 2 employees.
Execution Table
StepActionIntermediate ResultHAVING Condition CheckOutput
1Group rows by departmentGroups: Sales(3), HR(2), IT(4)N/ANo output yet
2Calculate COUNT(*) for each groupSales=3, HR=2, IT=4N/ANo output yet
3Apply HAVING emp_count > 2Check Sales: 3 > 2TrueInclude Sales
4Apply HAVING emp_count > 2Check HR: 2 > 2FalseExclude HR
5Apply HAVING emp_count > 2Check IT: 4 > 2TrueInclude IT
6Return filtered groupsSales and IT groupsN/AOutput rows for Sales and IT
💡 All groups checked; only those with emp_count > 2 are returned
Variable Tracker
VariableStartAfter GroupingAfter AggregationAfter HAVING Filter
groupsemptySales(3), HR(2), IT(4)Sales=3, HR=2, IT=4Sales=3, IT=4
emp_countN/AN/A3, 2, 43, 4
Key Moments - 2 Insights
Why doesn't HAVING filter individual rows like WHERE?
HAVING works after grouping and aggregation, filtering groups, not individual rows. See execution_table rows 1-3 where grouping and aggregation happen before HAVING filters.
Can HAVING use column names not in GROUP BY?
Yes, HAVING can use aggregate results like emp_count which are not in GROUP BY. This is shown in execution_table row 3 where emp_count is used in HAVING.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which department is excluded by the HAVING clause?
AHR
BIT
CSales
DNone
💡 Hint
Check the HAVING Condition Check column in rows 4 and 5
At which step are the groups created?
AStep 2
BStep 3
CStep 1
DStep 6
💡 Hint
Look at the Action column describing grouping
If the HAVING condition changed to emp_count > 3, which groups would be included?
ASales and IT
BOnly IT
COnly Sales
DAll groups
💡 Hint
Refer to emp_count values in variable_tracker after aggregation
Concept Snapshot
HAVING clause filters groups after aggregation
Used with GROUP BY to restrict grouped results
Syntax: SELECT ... GROUP BY ... HAVING condition
HAVING can use aggregate functions like COUNT(), SUM()
Different from WHERE which filters rows before grouping
Full Transcript
The HAVING clause is used in SQL to filter groups created by the GROUP BY clause. First, rows are grouped by a column, then aggregate functions like COUNT calculate values per group. HAVING applies a condition to these aggregated values to decide which groups to keep. Unlike WHERE, which filters individual rows before grouping, HAVING filters after grouping. For example, a query can count employees per department and use HAVING to show only departments with more than two employees. The execution steps show grouping, aggregation, then filtering by HAVING. Variables track groups and counts before and after filtering. Common confusions include why HAVING filters groups not rows, and that HAVING can use aggregate results. Visual quiz questions help reinforce these ideas by referencing the execution steps and variable changes.