CREATE TABLE aa (a int primary key); CREATE TABLE bb (a int primary key references aa); CREATE TABLE cc (a int primary key references bb); SELECT union2.a FROM (SELECT union1.a FROM (SELECT aa.a FROM aa UNION ALL SELECT bb.a FROM bb) AS union1 UNION ALL SELECT cc.a FROM cc) AS union2 INNER JOIN aa ON union2.a = aa.a;