Join
This page first looks at the INNER JOIN / LEFT JOIN actually used in the project, then continues with the theoretical classification of joins from a set perspective (OUTER JOIN, equi/non-equi/self join).
JOIN
A join combines data stored across several tables into one view, based on a common column (usually an FK-PK relationship). This project splits information into users/reels/likes/comments, so requests like "I want to see a reel's title together with its uploader's name" are solved with joins.
- INNER JOIN (intersection): keeps only rows that match in both tables. Q6 attached
reelsanduserswithreels.user_id = users.user_idto build "reel + uploader name," and as in Q7 and Q9, puttingcomments/likesin the middle and applying INNER JOIN twice links "comment -> author -> target reel" and "reel -> like -> user who liked it" into a single row. - LEFT JOIN (left-based): keeps every row of the left (base) table even without a match, and fills the spot with
NULLwhen there is no partner on the right. In Q8, doingreels LEFT JOIN commentsand then addingWHERE comments.comment_id IS NULLfilters out only "reels with no comments at all." This "LEFT JOIN + IS NULL" is the classic pattern for finding "things that exist on one side but not on the other." - Separating the roles of ON and WHERE:
ONdecides under what condition the two tables are connected (the linking key), andWHEREdecides which rows to keep from the connected result (the filter). That is why Q8 put the join condition inONand the filtering condition (IS NULL) inWHERE.
| Type | Rows kept | Example in this project |
|---|---|---|
INNER JOIN | Only rows present on both sides | Q6 (reel + uploader), Q7 (comment + author + reel), Q9 (reel + users who liked it) |
LEFT JOIN | All of the left + right filled in if present | Q8 (reels without comments = LEFT JOIN + IS NULL) |
The reason an index was put on reels.user_id, the linking key of these joins (see the "Applying an Index" section), is also that joins based on user_id like these happen often.
Joins as Sets: INNER vs OUTER
If you think of joins as Venn diagrams (sets), the differences between types become clear at a glance. Given two tables A and B:
INNER JOIN LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN
A ∩ B all of A + overlap overlap + all of B A ∪ B (union)
(A)(B) (A)(B) (A)(B) (A)(B)
▓▓ ██▓▓ ▓▓██ ██▓▓██
overlap only keep all of A keep all of B keep everything
NULL if no match NULL if no match NULL on missing side| INNER JOIN | LEFT OUTER JOIN | RIGHT OUTER JOIN | FULL OUTER JOIN |
|---|---|---|---|
![]() | ![]() | ![]() | ![]() |
- INNER JOIN: keeps only rows that match on both sides. This is the intersection.
- LEFT OUTER JOIN: always keeps every row of the left (base) table, and fills in the right side when it matches, leaving NULL when it doesn't. Think of it as "attaching based on the left table."
- RIGHT OUTER JOIN: the opposite of LEFT. It keeps everything based on the right table. Since a
RIGHT OUTER JOINcan be written the same way as aLEFT OUTER JOINjust by swapping the order of the base tables, in practice people often standardize on LEFT and rarely use RIGHT. - FULL OUTER JOIN: keeps every row of both tables (union). The side with no match is filled with NULL. The very idea of a "base table" basically stops meaning anything.
The basic shape of the join syntax is always the same; only the keyword in front of JOIN changes.
SELECT alias.column_to_select, alias.column_to_select
FROM base_table alias
[INNER | LEFT OUTER | RIGHT OUTER | FULL OUTER] JOIN join_table alias
ON base_alias.join_key = join_alias.join_key;-- Inner join (intersection)
SELECT Star.Name, Dep.DepName
FROM Star
INNER JOIN Dep ON Star.DepNo = Dep.DepNo;
-- Left outer join
SELECT Star.Name, Dep.Name
FROM Star
LEFT OUTER JOIN Dep ON Star.DepNo = Dep.DepNo;
-- Right outer join
SELECT Star.Name, Dep.Name
FROM Star
RIGHT OUTER JOIN Dep ON Star.DepNo = Dep.DepNo;
-- Full outer join (union)
SELECT Star.Name, Dep.Name
FROM Star
FULL OUTER JOIN Dep ON Star.DepNo = Dep.DepNo;It also helps to know a few other names for ways of joining.
- Equi join: the most common join, matching with the
=operator (every example so far uses this) - Non-equi join: a join that matches with something other than
=, such as<orBETWEEN(e.g. finding which grade range a salary falls into) - Self join: joining a table with itself. Used for tables with a self-referencing relationship, such as finding "this employee's manager's name" in an employee table.
Summary
| Type | Rows kept |
|---|---|
INNER JOIN | Only rows matching on both sides (intersection) |
LEFT (OUTER) JOIN | All of the left + NULL on the right when there is no match (use + IS NULL to find "things only on one side") |
RIGHT OUTER JOIN | All of the right (same as LEFT if you swap the order, so in practice people standardize on LEFT) |
FULL OUTER JOIN | Everything on both sides (union), NULL on the missing side |
ONis the connection condition, andWHEREis the filter on the connected result.- By matching method, joins are also called equi joins (
=), non-equi joins (<,BETWEEN), and self joins (joining with itself).
Source: Codyssey-B1/B6-1
Reference: personal Notion study notes




