0
0
SQLquery~10 mins

Combining multiple aggregates in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Combining multiple aggregates
Start Query
Scan Table Rows
Apply Aggregate Functions
SUM()
Combine Results
Return Single Row with All Aggregates
The query scans the table, applies each aggregate function to all rows, then combines these results into one output row.
Execution Sample
SQL
SELECT SUM(sales), COUNT(sales), AVG(sales) FROM orders;
This query calculates the total sales, number of sales, and average sale amount from the orders table.
Execution Table
StepActionSUM(sales)COUNT(sales)AVG(sales)
1Start scanning orders tableNULLNULLNULL
2Read row 1: sales=1001001100
3Read row 2: sales=2003002150
4Read row 3: sales=503503116.67
5Read row 4: sales=1505004125
6All rows processed5004125
7Return combined aggregates5004125
💡 All rows processed, aggregates computed and combined into one result row.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
SUM(sales)0100300350500500
COUNT(sales)012344
AVG(sales)NULL100150116.67125125
Key Moments - 2 Insights
Why does AVG(sales) change after each row instead of only at the end?
AVG is calculated as SUM divided by COUNT at each step, so it updates as new rows are processed (see execution_table rows 2-5).
Why do all aggregates return a single row instead of multiple rows?
Aggregate functions summarize all rows into one result, so the query returns one combined row with all aggregates (see execution_table row 7).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the COUNT(sales)?
A2
B4
C3
D1
💡 Hint
Check the COUNT(sales) column in execution_table row with Step 4.
At which step does SUM(sales) reach 500?
AStep 3
BStep 5
CStep 6
DStep 2
💡 Hint
Look at the SUM(sales) column and find when it first equals 500.
If a new row with sales=100 is added, what will COUNT(sales) be at final?
A5
B4
C6
D3
💡 Hint
COUNT increases by 1 for each new row processed (see variable_tracker COUNT row).
Concept Snapshot
Combining multiple aggregates:
Use multiple aggregate functions (SUM, COUNT, AVG) in one SELECT.
Each processes all rows and returns one combined result row.
AVG is SUM divided by COUNT, updated as rows are read.
Result is a single row with all aggregates together.
Full Transcript
This visual execution shows how SQL combines multiple aggregate functions in one query. The query scans each row of the orders table, updating SUM, COUNT, and AVG step by step. SUM adds sales amounts, COUNT counts rows, and AVG calculates the average as SUM divided by COUNT. After all rows are processed, the query returns one row with all three aggregate results combined. This helps beginners see how aggregates work together and produce a single summary output.