0
0
MySQLquery~10 mins

JOIN with aggregate functions in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - JOIN with aggregate functions
Start with two tables
Perform JOIN on matching keys
Group rows if needed
Apply aggregate functions (SUM, COUNT, AVG, etc.)
Return aggregated results with joined data
This flow shows how two tables are joined, then aggregate functions are applied on the joined data to summarize information.
Execution Sample
MySQL
SELECT d.department_name, COUNT(e.employee_id) AS employee_count
FROM departments d
JOIN employees e ON d.department_id = e.department_id
GROUP BY d.department_name;
This query joins departments with employees and counts how many employees are in each department.
Execution Table
StepActionTables InvolvedIntermediate ResultAggregate Result
1Start with departments and employees tablesdepartments, employeesdepartments (4 rows), employees (6 rows)
2Perform JOIN on department_iddepartments JOIN employeesJoined rows (6 rows): each employee matched with their department
3Group rows by department_nameJoined rowsGroups: Sales(2 rows), HR(1 row), IT(3 rows)
4Apply COUNT(employee_id) per groupGrouped rowsSales:2, HR:1, IT:3
5Return final result with department_name and employee_countAggregated data[{Sales,2}, {HR,1}, {IT,3}]
6Query endsAll groups counted, query complete
💡 All joined rows processed and aggregated by department_name, query returns counts per department.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
departments4 rows4 rowsGrouped by nameGrouped by nameGrouped by name
employees6 rows6 rowsGrouped by departmentGrouped by departmentGrouped by department
joined_rowsN/A6 rows6 rows grouped6 rows grouped6 rows grouped
groupsN/AN/A3 groups3 groups with counts3 groups with counts
employee_countN/AN/AN/ACounts calculatedCounts calculated
Key Moments - 3 Insights
Why do we need to GROUP BY after JOIN when using aggregate functions?
Because aggregate functions like COUNT summarize data per group, GROUP BY tells SQL how to group joined rows before aggregation, as shown in execution_table step 3.
What happens if we forget to GROUP BY when using COUNT with JOIN?
The COUNT would apply to the entire joined result, giving one total count instead of counts per department. This is why step 3 grouping is essential.
Does JOIN increase the number of rows before aggregation?
Yes, JOIN combines matching rows from both tables, often increasing rows as seen in step 2 where 6 joined rows appear from 4 and 6 original rows.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, how many rows are in the joined result after step 2?
A4 rows
B3 rows
C6 rows
D1 row
💡 Hint
Check the 'Intermediate Result' column at step 2 in the execution_table.
At which step does the query group rows by department_name?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the 'Group rows by department_name' action in the execution_table.
If we remove GROUP BY, what would happen to the employee_count?
AIt would count all employees in one total
BIt would count employees per department
CIt would cause a syntax error
DIt would return zero
💡 Hint
Refer to key_moments about the importance of GROUP BY with aggregate functions.
Concept Snapshot
JOIN with aggregate functions:
- JOIN combines rows from two tables on matching keys.
- GROUP BY groups joined rows by specified columns.
- Aggregate functions (COUNT, SUM, AVG) summarize each group.
- Without GROUP BY, aggregates apply to entire joined set.
- Use GROUP BY to get meaningful summaries per group.
Full Transcript
This visual execution shows how a SQL query joins two tables, groups the joined rows, and applies aggregate functions. First, the departments and employees tables are joined on department_id, producing 6 joined rows. Then, rows are grouped by department_name to prepare for aggregation. The COUNT function counts employees in each department group. Finally, the query returns the department names with their employee counts. Key points include the necessity of GROUP BY to get counts per department and how JOIN increases rows before aggregation.