A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

SQL Joins

College Depth 71 in the knowledge graph ☐ I know this ☆ Set as goal
11topics build on this
332prerequisites beneath it
See this on the map →
Primary Keys and Foreign KeysSQL: SELECT Statement and Basic Queries→→Denormalization and Performance Trade-offsQuery Optimization+4 more
SQL JOIN INNER JOIN LEFT JOIN OUTER JOIN self-join

Core Idea

Joins combine rows from two or more tables based on a related column condition, enabling the reconstruction of related data stored in separate tables. INNER JOIN returns only rows with matching values in both tables; LEFT OUTER JOIN includes all rows from the left table with NULLs for unmatched right-side rows; FULL OUTER JOIN includes all rows from both sides. Self-joins allow a table to be joined with itself, useful for hierarchical or recursive data. The join condition typically matches a foreign key to the referenced primary key.

How It's Best Learned

Draw Venn diagrams for each join type, then run queries to verify behavior. Work through examples where rows don't match to understand when and where NULLs appear in outer joins. Rewrite a LEFT JOIN as a RIGHT JOIN by swapping table order.

Common Misconceptions

Explainer

You know how to retrieve data from a single table with SELECT and how primary keys uniquely identify rows while foreign keys reference rows in other tables. Joins are the mechanism that puts these pieces together — they let you combine rows from two or more tables based on a matching condition, reconstructing the relationships that normalization split apart.

The most common join is the INNER JOIN. It returns only the rows where the join condition finds a match in both tables. If you join `orders` to `customers` on `orders.customer_id = customers.id`, you get one result row for each order paired with its customer data. Orders with no matching customer (orphaned foreign keys) and customers with no orders are both excluded. Think of it as the intersection: only rows with a partner on both sides survive.

LEFT OUTER JOIN (usually written just `LEFT JOIN`) keeps every row from the left table, whether or not it has a match on the right. When there is no match, the right-side columns are filled with NULL. This is essential when you want to find things that *don't* have a relationship — `SELECT customers.name FROM customers LEFT JOIN orders ON customers.id = orders.customer_id WHERE orders.id IS NULL` finds customers who have never placed an order. RIGHT JOIN is the mirror image, and FULL OUTER JOIN keeps unmatched rows from both sides.

A self-join joins a table to itself, which is useful whenever rows in the same table have relationships to each other. An `employees` table where each row has a `manager_id` pointing to another employee's `id` is the classic case: `SELECT e.name, m.name AS manager FROM employees e LEFT JOIN employees m ON e.manager_id = m.id`. You alias the table twice (here `e` and `m`) so the database can distinguish which "copy" you mean. Without self-joins, querying hierarchical or graph-structured data stored in a single table would require multiple round-trips or procedural code.

One critical detail: the join condition determines which rows pair up, but it does not filter the final result in the same way a WHERE clause does. For inner joins, putting a condition in ON versus WHERE produces the same result. But for outer joins, it matters. A condition in the ON clause filters during the join (unmatched rows still appear with NULLs), while a condition in the WHERE clause filters after the join (removing those NULL rows entirely, effectively converting the outer join back to an inner join). Getting this distinction right is essential for writing correct outer join queries.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Set Operations: Union, Intersection, and Complement → Relational Algebra → SQL: SELECT Statement and Basic Queries → SQL Joins

Longest path: 72 steps · 332 total prerequisite topics

Prerequisites (2)

Leads To (6)