Skip to content

[Correlated subqueries] (NOT) EXISTS in other table #72459

@qoega

Description

@qoega

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

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions