Student rows carry a department foreign key.
Combine Tables without Losing Control of Rows
Match related records, preserve intentional non-matches and combine compatible results while predicting duplicates and cardinality.
- Trace inner and outer joins.
- Place predicates correctly.
- Predict row multiplication.
- Write self and cross joins.
- Compare UNION and UNION ALL.
- Use INTERSECT and EXCEPT.
A Join Combines Rows When Its ON Condition Is TRUE
SELECT s.name, d.dept_name
FROM students AS s
JOIN departments AS d
ON d.dept_id = s.dept_id;Department rows provide descriptive attributes.
d.dept_id = s.dept_id decides which pairs qualify.
Join Type Defines Row Preservation
Students without a matching department and departments without students disappear.
Unmatched left rows survive with NULL values for right-side columns.
Equivalent logic can often be written as a LEFT JOIN after swapping table order.
Preserves left-only, matched and right-only rows where supported.
ON and WHERE Can Produce Different Results
LEFT JOIN departments d
ON d.dept_id = s.dept_id
AND d.active = TRUEEvery student remains; non-active or absent departments appear as NULL on the right.
LEFT JOIN departments d
ON d.dept_id = s.dept_id
WHERE d.active = TRUERows with no qualifying right side fail WHERE, often making the result behave like an inner join.
Each Matching Pair Becomes One Result Row
At most one right match per left row.
One parent repeats once for every matching child.
Matching rows can multiply rapidly; a bridge table usually represents the relationship.
Every left row pairs with every right row: m × n rows.
Do not immediately add DISTINCT. Inspect the join key, its uniqueness and the expected cardinality. DISTINCT can hide an incorrect join while leaving the logic wrong.
Self Joins Compare Roles; Cross Joins Generate Combinations
SELECT e.name, m.name AS manager
FROM employees e
LEFT JOIN employees m
ON m.employee_id = e.manager_id;Aliases give two logical roles to the same table.
SELECT c.color, s.size
FROM colors c
CROSS JOIN sizes s;Produces every combination. Useful deliberately; dangerous accidentally.
Set Operations Stack Compatible Query Results
Returns rows from either input and removes duplicates.
Returns every row from both inputs and preserves duplicates.
Returns rows present in both inputs.
Returns rows in the first input but not the second. Some products use MINUS.
Interactive Join Visualizer
Switch join types and inspect matching pairs, preserved rows, NULL padding and exact result size.
Set-Operation Laboratory
AIMLCSEAIMLECECSEITAIMLITTen Formative Concept Checks
1. INNER JOIN returns:
2. LEFT JOIN preserves:
3. CROSS JOIN of 3 and 4 rows returns:
4. Unmatched outer-join columns contain:
5. Unexpected join duplicates should first prompt you to inspect:
6. UNION differs from UNION ALL because UNION:
7. INTERSECT returns rows:
8. EXCEPT returns rows:
9. A self join needs aliases mainly to:
10. A right-table condition in WHERE after LEFT JOIN may:
University and Interview Questions
- Define join.
- INNER versus LEFT?
- What is a self join?
- UNION versus UNION ALL?
- Trace all outer joins.
- Explain row multiplication.
- Compare ON and WHERE filters.
- Write all set operations.
- Why does a join duplicate rows?
- How do you find missing matches?
- When is CROSS JOIN useful?
- What makes set inputs compatible?
Show the join answer framework
- Name both row sources and their grain.
- State expected cardinality.
- Write the exact match predicate.
- Choose which unmatched side must survive.
- Place filters at the intended stage.
- Predict result count and NULL padding.
- Validate duplicates against keys.
You Can Now Combine Data Predictably
- Joins create one row per qualifying pair.
- Outer joins preserve designated non-matches.
- Cardinality explains row multiplication.
- Self joins assign roles to one table.
- Set operations combine compatible complete rows.
- UNION removes duplicates; UNION ALL retains them.
Mark this level when you can predict every matched, unmatched and duplicate row.
Saved in this browser only.