0
0
SQLquery~10 mins

Why joins are needed in SQL - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why joins are needed
Start with Table A
Identify related data
Combine matching rows
Create joined result
Use combined data
We start with two tables, find related rows, combine them, and get a new table with connected information.
Execution Sample
SQL
SELECT employees.name, departments.name
FROM employees
JOIN departments ON employees.dept_id = departments.id;
This query joins employees with their departments to show employee names alongside their department names.
Execution Table
StepActionemployees rowdepartments rowMatch ConditionOutput row
1Check employees row 1 with departments row 1{id:1, name:'Alice', dept_id:10}{id:10, name:'HR'}10 = 10 (True){employee_name:'Alice', department_name:'HR'}
2Check employees row 1 with departments row 2{id:1, name:'Alice', dept_id:10}{id:20, name:'Sales'}10 = 20 (False)No output
3Check employees row 2 with departments row 1{id:2, name:'Bob', dept_id:20}{id:10, name:'HR'}20 = 10 (False)No output
4Check employees row 2 with departments row 2{id:2, name:'Bob', dept_id:20}{id:20, name:'Sales'}20 = 20 (True){employee_name:'Bob', department_name:'Sales'}
5Check employees row 3 with departments row 1{id:3, name:'Carol', dept_id:30}{id:10, name:'HR'}30 = 10 (False)No output
6Check employees row 3 with departments row 2{id:3, name:'Carol', dept_id:30}{id:20, name:'Sales'}30 = 20 (False)No output
7No more rows to checkN/AN/AN/AEnd
💡 All employees rows checked against all departments rows; join complete.
Variable Tracker
VariableStartAfter Step 1After Step 4Final
Current employees rowNone{id:1, name:'Alice', dept_id:10}{id:2, name:'Bob', dept_id:20}None
Current departments rowNone{id:10, name:'HR'}{id:20, name:'Sales'}None
Output rows[][{employee_name:'Alice', department_name:'HR'}][{employee_name:'Alice', department_name:'HR'}, {employee_name:'Bob', department_name:'Sales'}][{employee_name:'Alice', department_name:'HR'}, {employee_name:'Bob', department_name:'Sales'}]
Key Moments - 2 Insights
Why do we need to check each row of one table against rows of another?
Because related data is stored separately, we must compare rows to find matches, as shown in execution_table steps 1-6.
What happens if no matching department is found for an employee?
That employee's row does not appear in the output, as seen in steps 5 and 6 where no matches produce no output.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output row at step 4?
A{employee_name:'Bob', department_name:'Sales'}
B{employee_name:'Alice', department_name:'HR'}
CNo output
D{employee_name:'Carol', department_name:'Sales'}
💡 Hint
Check the 'Output row' column in row 4 of execution_table.
At which step does the join process end?
AStep 6
BStep 5
CStep 7
DStep 4
💡 Hint
Look for the step with 'No more rows to check' in execution_table.
If employees had a dept_id that doesn't exist in departments, what happens to that employee in the output?
AThey appear with empty strings
BThey do not appear in the output
CThey appear with NULL department name
DThey appear multiple times
💡 Hint
Refer to steps 5 and 6 where no matching department means no output row.
Concept Snapshot
Joins combine rows from two tables based on a related column.
They match rows where the join condition is true.
Only matching rows appear in the output.
This lets us see connected data from separate tables.
Syntax example: SELECT * FROM A JOIN B ON A.key = B.key;
Full Transcript
Joins are needed because data is often split into different tables to keep it organized. To see related information together, we combine rows from these tables where their related columns match. For example, employees and departments are separate tables. By joining on department IDs, we get employee names with their department names. The process checks each employee row against each department row, outputs combined rows when IDs match, and skips rows without matches. This way, we get a new table showing connected data from both tables.