0
0
SQLquery~10 mins

WHERE vs HAVING mental model in SQL - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - WHERE vs HAVING mental model
Start with Table Data
Apply WHERE filter
Group Data (if GROUP BY used)
Apply HAVING filter on groups
Return final result
Data is first filtered row-by-row using WHERE, then grouped, and groups are filtered using HAVING before final output.
Execution Sample
SQL
SELECT department, COUNT(*) AS emp_count
FROM employees
WHERE salary > 3000
GROUP BY department
HAVING COUNT(*) > 2;
This query filters employees with salary > 3000, groups them by department, then shows only departments with more than 2 such employees.
Execution Table
StepActionData StateFilter AppliedResulting Rows/Groups
1Start with all employeesAll rows from employees tableNoneAll employee rows
2Apply WHERE salary > 3000Rows with salary > 3000Row-level filterFiltered employee rows
3Group by departmentGrouped filtered rowsGroupingGroups of employees per department
4Apply HAVING COUNT(*) > 2Groups with count > 2Group-level filterDepartments with more than 2 employees
5Return final resultFiltered groupsNoneFinal output rows
💡 No more steps; query returns groups filtered by HAVING after WHERE filtering and grouping.
Variable Tracker
VariableStartAfter WHEREAfter GROUP BYAfter HAVINGFinal
RowsAll employeesEmployees with salary > 3000Employees grouped by departmentGroups with count > 2Final filtered groups
CountN/AN/ACount per groupCount > 2 filter appliedCount per returned group
Key Moments - 3 Insights
Why can't we use aggregate functions like COUNT() in WHERE?
WHERE filters rows before grouping, so aggregate functions which need groups are not available yet (see execution_table step 2 vs 4).
Can HAVING be used without GROUP BY?
Yes, HAVING can filter on aggregate results even without GROUP BY, but it filters on the whole result treated as one group (see execution_table step 4).
Does WHERE filter groups or individual rows?
WHERE filters individual rows before grouping, so it affects which rows enter groups (see execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the row-level filter applied?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Check the 'Filter Applied' column for 'Row-level filter' in execution_table.
According to variable_tracker, what happens to 'Rows' after GROUP BY?
ARows are grouped by department
BRows are counted
CRows are filtered by salary
DRows are returned as final output
💡 Hint
Look at the 'Rows' variable state under 'After GROUP BY' in variable_tracker.
If we remove the WHERE clause, how does the execution_table change at step 2?
AStep 2 applies HAVING filter
BStep 2 groups rows
CStep 2 applies no filter and keeps all rows
DStep 2 returns final result
💡 Hint
Refer to execution_table step 2 description about WHERE filtering.
Concept Snapshot
WHERE filters rows before grouping.
HAVING filters groups after grouping.
WHERE cannot use aggregates like COUNT().
HAVING can filter on aggregates.
Use WHERE for row-level conditions.
Use HAVING for group-level conditions.
Full Transcript
This visual execution shows how SQL processes WHERE and HAVING clauses. First, the database starts with all rows. Then WHERE filters rows individually before any grouping. Next, rows are grouped by the specified column(s). After grouping, HAVING filters groups based on aggregate conditions like COUNT(). Finally, the filtered groups are returned as the query result. WHERE works on raw rows, so it cannot use aggregate functions. HAVING works on groups, so it can use aggregates. This helps understand when to use WHERE versus HAVING in SQL queries.