join
Self join
Joins a relation to another alias of itself. The aliases distinguish roles; the inequality here avoids pairing a row with itself or repeating reversed pairs.
Formal Syntax & Structure
sql syntaxsql
a AS x JOIN a AS y ON conditionWorking Example
Example Snippetsql
CREATE TABLE a(id INTEGER PRIMARY KEY, k INTEGER); CREATE TABLE b(bid INTEGER PRIMARY KEY, k INTEGER); INSERT INTO a VALUES (1,10),(2,20),(3,NULL),(4,10); INSERT INTO b VALUES (101,10),(102,10),(103,30),(104,NULL);SELECT x.id,y.id FROM a AS x JOIN a AS y ON x.k=y.k AND x.id<y.id ORDER BY x.id,y.id;