Tsallaka zuwa abun ciki
Justice Tech School

Self Join

Join a table to itself with two aliases to follow relationships inside one table.

Example
SELECT s.name AS student, r.name AS referred_by
FROM students s
JOIN students r ON r.id = s.referred_by;

Result

student | referred_by
Kofi    | Ada
Amina   | Ada
Wanjiru | Kofi
Tunde   | Amina
Zainab  | Ada
Sipho   | Wanjiru
Esi     | Kofi

Exercise

Second alias.

FROM students s JOIN students  ON r.id = s.referred_by

Quiz Test

Why alias in self joins?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.