By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE a (id int, b_id int)
CREATE TABLE b (id int)
INSERT INTO a VALUES (1, 1), (1, 2), (1, 3), (2, 2), (3, 2), (3, 3)
6 rows affected
SELECT a.id FROM a
JOIN b on a.b_id = b.id
GROUP BY a.id
HAVING COUNT(a.id) = (SELECT COUNT(*) FROM b)
UNION
SELECT a.id
FROM a
WHERE NOT EXISTS (SELECT 1 FROM b)
id |
---|
1 |
2 |
3 |