0
0
SQLquery~10 mins

CASE with aggregate functions in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - CASE with aggregate functions
Start Query
Aggregate Function Calculates
CASE Evaluates Condition on Aggregate Result
WHEN condition True
Return Value 1
WHEN condition False
Return Value 2
Output Final Result
End
The query first calculates an aggregate value, then the CASE statement checks conditions on that value to decide what output to return.
Execution Sample
SQL
SELECT
  department_id,
  CASE
    WHEN AVG(salary) > 5000 THEN 'High'
    ELSE 'Low'
  END AS salary_level
FROM employees
GROUP BY department_id;
This query groups employees by department, calculates average salary per department, and labels it 'High' or 'Low' based on the average.
Execution Table
Stepdepartment_idAVG(salary)CASE Condition (AVG(salary) > 5000)CASE ResultOutput Row
11060006000 > 5000 = True'High'(10, 'High')
22045004500 > 5000 = False'Low'(20, 'Low')
33052005200 > 5000 = True'High'(30, 'High')
44030003000 > 5000 = False'Low'(40, 'Low')
5EndQuery complete, all groups processed
💡 All department groups processed, query ends.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
department_idN/A10203040N/A
AVG(salary)N/A6000450052003000N/A
CASE ResultN/A'High''Low''High''Low'N/A
Key Moments - 2 Insights
Why does the CASE statement use the aggregate AVG(salary) instead of individual salaries?
Because the query groups rows by department_id, the aggregate AVG(salary) calculates one average per group. The CASE evaluates this single value per group, not individual salaries. See execution_table rows 1-4.
What happens if AVG(salary) equals exactly 5000?
The CASE condition is AVG(salary) > 5000, so if AVG(salary) is 5000, the condition is False and the ELSE branch 'Low' is returned. This is shown by the strict greater-than operator in the CASE condition.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the CASE Result for department_id 20?
A'Low'
B'High'
CNULL
D5000
💡 Hint
Check the row where department_id is 20 in the execution_table under CASE Result column.
At which step does the AVG(salary) first exceed 5000?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the AVG(salary) values in execution_table rows and find the first greater than 5000.
If the CASE condition changed to AVG(salary) >= 5000, what would be the CASE Result for department_id 30?
A'Low'
B'High'
CNULL
DError
💡 Hint
Refer to variable_tracker for AVG(salary) for department_id 30 and consider the new condition.
Concept Snapshot
CASE with aggregate functions:
- Aggregate functions (e.g., AVG, SUM) compute values per group.
- CASE evaluates conditions on these aggregate results.
- Syntax: CASE WHEN aggregate_condition THEN result ELSE other_result END
- Use GROUP BY to group rows before aggregation.
- Output depends on aggregate values per group.
Full Transcript
This visual execution shows how a SQL query uses CASE with aggregate functions. The query groups employees by department_id and calculates the average salary per group. Then, the CASE statement checks if the average salary is greater than 5000. If true, it outputs 'High'; otherwise, 'Low'. The execution table traces each department's average salary, the CASE condition evaluation, and the final output row. The variable tracker shows how department_id, average salary, and CASE results change step-by-step. Key moments clarify why CASE uses aggregate results and what happens at boundary values. The quiz tests understanding of CASE results and condition changes. This helps beginners see how CASE works with aggregates in SQL.