-
Notifications
You must be signed in to change notification settings - Fork 8.3k
[Correlated subqueries] (NOT) EXISTS in other table #72459
Copy link
Copy link
Closed
Labels
Description
Example schemas
CREATE TABLE t1 (c1 UInt64, c2 UInt64) ORDER BY c1;
CREATE TABLE t2 (c1 UInt64) ORDER BY c1;
INSERT INTO t1 SELECT number, number % 100 FROM numbers(1_000_000);
INSERT INTO t2 SELECT number*number FROM numbers(1_000_000);EXISTS
Query to rewrite
SELECT count(t1.c1)
FROM t1
WHERE t1.c2 = 10
AND EXISTS (
SELECT * FROM t2 WHERE t1.c1 = t2.c1)Can be rewritten via IN + subquery
SELECT count(t1.c1)
FROM t1
WHERE t1.c2 = 10 AND t1.c1 IN (SELECT c1 FROM t2)NOT EXISTS
Same for NOT EXISTS
SELECT count(t1.c1)
FROM t1
WHERE t1.c2 = 10 AND NOT EXISTS (SELECT * FROM t2 WHERE t1.c1 = t2.c1)Can be rewritten via NOT IN + subquery
SELECT count(t1.c1)
FROM t1
WHERE t1.c2 = 10 AND t1.c1 NOT IN (SELECT c1 FROM t2)https://fiddle.clickhouse.com/70cb2e57-f0ba-430f-823c-418559cc3c1c
Reactions are currently unavailable