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
INSERT INTO b VALUES (1),(2)
2 rows affected
SELECT a.id
FROM a
LEFT JOIN b on a.b_id = b.id
GROUP BY a.id
HAVING COUNT(b.id) = (SELECT COUNT(*) FROM b)
OR NOT EXISTS(SELECT 1 FROM b);
id |
---|
1 |
TRUNCATE TABLE b;
SELECT a.id
FROM a
LEFT JOIN b on a.b_id = b.id
GROUP BY a.id
HAVING COUNT(b.id) = (SELECT COUNT(*) FROM b)
OR NOT EXISTS(SELECT 1 FROM b);
id |
---|
1 |
2 |
3 |