Skip to content

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 reels and users with reels.user_id = users.user_id to build "reel + uploader name," and as in Q7 and Q9, putting comments/likes in 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 NULL when there is no partner on the right. In Q8, doing reels LEFT JOIN comments and then adding WHERE comments.comment_id IS NULL filters 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: ON decides under what condition the two tables are connected (the linking key), and WHERE decides which rows to keep from the connected result (the filter). That is why Q8 put the join condition in ON and the filtering condition (IS NULL) in WHERE.
TypeRows keptExample in this project
INNER JOINOnly rows present on both sidesQ6 (reel + uploader), Q7 (comment + author + reel), Q9 (reel + users who liked it)
LEFT JOINAll of the left + right filled in if presentQ8 (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 JOINLEFT OUTER JOINRIGHT OUTER JOINFULL OUTER JOIN
INNER JOIN Venn diagramLEFT OUTER JOIN Venn diagramRIGHT OUTER JOIN Venn diagramFULL OUTER JOIN Venn diagram
  • 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 JOIN can be written the same way as a LEFT OUTER JOIN just 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.

sql
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;
sql
-- 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 < or BETWEEN (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 ​

TypeRows kept
INNER JOINOnly rows matching on both sides (intersection)
LEFT (OUTER) JOINAll of the left + NULL on the right when there is no match (use + IS NULL to find "things only on one side")
RIGHT OUTER JOINAll of the right (same as LEFT if you swap the order, so in practice people standardize on LEFT)
FULL OUTER JOINEverything on both sides (union), NULL on the missing side
  • ON is the connection condition, and WHERE is 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