SQLite User Forum

Possible wrong-result with nested EXISTS in 3.54.0 when optimizations are enabled
Login

Possible wrong-result with nested EXISTS in 3.54.0 when optimizations are enabled

(1) By Pavan Nambi (Pavan-Nambi) on 2026-04-21 14:25:02 [source]

similar to this issue

this is on HEAD, exist on 3.53 too

 CREATE TABLE t1(b,c);
  CREATE TABLE t2(x,y);
  CREATE TABLE t3(p,q);

  INSERT INTO t1 VALUES(7,10);
  INSERT INTO t2 VALUES(1,1),(4,7);
  INSERT INTO t3 VALUES(4,7);

  CREATE INDEX t2_yx ON t2(y,x);
  CREATE INDEX t3_pq ON t3(p,q);

  SELECT count(*),sum(c)
  FROM t1 AS A
  WHERE EXISTS (
    SELECT 1
    FROM t2 AS B
    WHERE EXISTS (
      SELECT 1
      FROM t3 AS C
      WHERE C.p=B.x AND C.q=A.b
    )
  );

with optimizations disabled:

SQLite in-memory> .testctrl optimizations 0xffffffff 
-All
SQLite in-memory> .read testing.sql
╭──────────┬────────╮
│ count(*) │ sum(c) │
╞══════════╪════════╡
│        1 │     10 │
╰──────────┴────────╯

with default optimizations:

>  ./sqlite3 < testing.sql 
╭──────────┬────────╮
│ count(*) │ sum(c) │
╞══════════╪════════╡
│        0 │ NULL   │
╰──────────┴────────╯