0
0
MySQLquery~10 mins

INNER JOIN in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - INNER JOIN
Start with Table A
Start with Table B
Compare rows from A and B
Match rows where join condition is true
Combine matched rows into result
Return combined rows only
INNER JOIN takes two tables and returns only the rows where the join condition matches in both tables.
Execution Sample
MySQL
SELECT employees.name, departments.dept_name
FROM employees
INNER JOIN departments
ON employees.dept_id = departments.id;
This query returns employee names with their department names, only where the department ID matches in both tables.
Execution Table
Stepemployees rowdepartments rowJoin Condition (employees.dept_id = departments.id)ActionOutput row
1{id:1, name:'Alice', dept_id:10}{id:10, dept_name:'HR'}10 = 10 (True)Combine rows{name:'Alice', dept_name:'HR'}
2{id:1, name:'Alice', dept_id:10}{id:20, dept_name:'Sales'}10 = 20 (False)SkipNo output
3{id:2, name:'Bob', dept_id:20}{id:10, dept_name:'HR'}20 = 10 (False)SkipNo output
4{id:2, name:'Bob', dept_id:20}{id:20, dept_name:'Sales'}20 = 20 (True)Combine rows{name:'Bob', dept_name:'Sales'}
5{id:3, name:'Carol', dept_id:30}{id:10, dept_name:'HR'}30 = 10 (False)SkipNo output
6{id:3, name:'Carol', dept_id:30}{id:20, dept_name:'Sales'}30 = 20 (False)SkipNo output
7No more employees rowsN/AN/AEndFinal output rows returned
💡 All rows from employees checked against all departments; only matching dept_id rows combined and returned.
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 7 (End)
Current employees rowNone{id:1, name:'Alice', dept_id:10}{id:2, name:'Bob', dept_id:20}None
Current departments rowNone{id:10, dept_name:'HR'}{id:20, dept_name:'Sales'}None
Output rows[][{name:'Alice', dept_name:'HR'}][{name:'Alice', dept_name:'HR'}, {name:'Bob', dept_name:'Sales'}][{name:'Alice', dept_name:'HR'}, {name:'Bob', dept_name:'Sales'}]
Key Moments - 2 Insights
Why are some rows from employees not in the output?
Rows from employees without a matching dept_id in departments are skipped, as shown in steps 5 and 6 where Carol's dept_id 30 does not match any department id.
Does INNER JOIN include rows with no matching partner in the other table?
No, INNER JOIN only returns rows where the join condition is true for both tables, as seen in the execution table where only matching dept_id rows produce output.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output row at Step 4?
A{name:'Alice', dept_name:'HR'}
B{name:'Bob', dept_name:'Sales'}
CNo output
D{name:'Carol', dept_name:'HR'}
💡 Hint
Check the 'Output row' column at Step 4 in the execution_table.
At which step does the join condition first become false?
AStep 1
BStep 4
CStep 2
DStep 7
💡 Hint
Look at the 'Join Condition' column in the execution_table for the first 'False' value.
If a new department with id 30 is added, how would the output change?
ACarol's row would appear in the output
BNo change in output
CAll employees would be excluded
DOnly departments with id 10 and 20 would appear
💡 Hint
Refer to the variable_tracker and execution_table where Carol's dept_id 30 had no match before.
Concept Snapshot
INNER JOIN syntax:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;

Returns rows only where join condition matches in both tables.
Rows without matches are excluded.
Full Transcript
INNER JOIN combines rows from two tables where the join condition is true. It compares each row from the first table with each row from the second table. Only rows with matching values in the join columns are combined and returned. Rows without a match in the other table are skipped. For example, employees are joined with departments on dept_id. Only employees with a matching department id appear in the result. This is shown step-by-step in the execution table, where each employee row is checked against department rows, and only matching pairs produce output rows.