Relational Joins, Cartesian Products & Set Semantics in SQL
Rigorous exploration of relational joins: Cartesian product cardinality (m x n), INNER JOIN predicate filtering, LEFT/RIGHT OUTER JOIN tuple preservation, and Three-Valued Logic.
Learning Objectives
- •Calculate the cardinality of Cartesian products across relational tables.
- •Differentiate between INNER JOIN, LEFT OUTER JOIN, and FULL OUTER JOIN semantics.
- •Diagnose query bugs caused by three-valued logic and NULL equality comparisons.
- •Construct multi-table SQL queries joining normalized entity schemas.
Essential Prerequisites
- •Relational tables, primary keys, and foreign key constraints
- •Basic SELECT, FROM, WHERE query syntax
The Core Mental Model
Why This Exists
Almost all real-world data is normalized into separate tables to eliminate redundancy. Joins are how database query engines reconstruct meaningful business entities (such as Students with their University and Enrolled Subjects) in milliseconds.
Beginner Foundation
A join connects two tables based on a shared column (usually a foreign key referencing a primary key). Syntax: SELECT ... FROM TableA JOIN TableB ON TableA.id = TableB.a_id.
Micro Concepts Decomposition
The Cartesian Product (CROSS JOIN)
The foundational relational operation R x S combines every row of table R with every row of table S. If table R has m rows and table S has n rows, the Cartesian product produces exactly m * n rows.
INNER JOIN: Predicate Matching
An INNER JOIN filters the Cartesian product, returning only rows where the join predicate evaluates to TRUE. Rows from either table that have no matching counterpart are discarded.
LEFT OUTER JOIN: Preserving Unmatched Rows
A LEFT JOIN returns all rows from the left table, plus matched rows from the right table. If a row in the left table has no match in the right table, right-side columns are filled with NULL.
Three-Valued Logic & NULL Comparisons in SQL
In SQL, comparisons with NULL (e.g. 'val = NULL' or 'NULL = NULL') evaluate to UNKNOWN, not TRUE. To filter for NULL values, you must use 'IS NULL' or 'IS NOT NULL'.
Hardware State Machine Architecture
Interactive Simulator
Visual SQL Relational JOIN Laboratory
SELECT s.id, s.name, s.major, e.course, e.grade FROM students s INNER JOIN enrollments e ON s.id = e.student_id;
students (s)| id (PK) | name | major |
|---|---|---|
| 1 | Aarav | CS |
| 2 | Diya | AI |
| 3 | Kabir | Data |
| 4 | Riya | Cyber |
enrollments (e)| student_id (FK) | course | grade |
|---|---|---|
| 1 | CS301 | A |
| 2 | CS301 | B+ |
| 2 | CS304 | A+ |
| 5 | CS305 | A |
| s.id | s.name | s.major | e.course | e.grade |
|---|---|---|---|---|
| 1 | Aarav | CS | CS301 | A |
| 2 | Diya | AI | CS301 | B+ |
| 2 | Diya | AI | CS304 | A+ |
In INNER JOIN: Only rows with matching keys in BOTH tables are preserved. Any unmatched student (like Kabir or Riya) or unmatched enrollment (like Student 5) is completely omitted.
End-to-End Execution Trace
Step-by-Step Code Execution (SQL)
Sandbox Terminal Ready
Click Run Code or press Ctrl+Enter to compile and execute.
Where Students Lose Marks
Active Assessment Quiz
Relational Joins, Cartesian Products & Set Semantics in SQL — Practice Questions
Table A has 10 rows and Table B has 5 rows. How many rows will a CROSS JOIN (Cartesian product) of Table A and Table B produce?