You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Correlated subquery with an unqualified outer column returns no rows #2644
A correlated subquery that refers to a column of the outer query without its table name returns no rows.
Reproduced on develop, compared with SQLite 3.43:
CREATETABLEa (id INT, ak INT); INSERT INTO a VALUES (1, 1), (2, 2), (3, 1);
CREATETABLEb (id INT, bk INT); INSERT INTO b VALUES (1, 1), (2, 2), (3, 2);
SELECT id FROM a WHERE id IN (SELECTb.idFROM b WHEREb.bk=a.ak); -- AlaSQL 1, 2 SQLite 1, 2SELECT id FROM a WHERE id IN (SELECTb.idFROM b WHERE bk = ak); -- AlaSQL none SQLite 1, 2
ak exists only in the outer table a. Inside the subquery it is resolved against the subquery's own source, so it is undefined. The same result on the commit before #2281.
Direction
An unqualified column that no source of the subquery has is looked up in the enclosing query's row, as SQL does.
When this is fixed, the correlation check added by #2281 (which only looks at table-qualified columns) must also recognise such subqueries as correlated, or the result of the first row is reused for all rows.
This fixes a wrong result nobody can rely on, so it can go into 4.x.
Tests: both queries above; NOT IN, EXISTS and a scalar subquery in the select list with an unqualified outer column; a name that both the subquery and the outer query have still resolves to the subquery's own source.
The same shape in JOIN ... ON. Since #2795 an EXISTS in ON compiles, and with an unqualified outer column it is false for every row, without an error.
vardb=newalasql.Database();db.exec('CREATE TABLE t (id INT, g INT); CREATE TABLE p (v INT); CREATE TABLE u (ug INT)');db.exec('INSERT INTO t VALUES (1,1),(2,2); INSERT INTO p VALUES (1),(2); INSERT INTO u VALUES (1),(2)');db.exec('SELECT t.id, p.v FROM t JOIN p ON p.v = t.id AND EXISTS (SELECT 1 FROM u WHERE u.ug = t.g)');// [{id: 1, v: 1}, {id: 2, v: 2}]db.exec('SELECT t.id, p.v FROM t JOIN p ON p.v = t.id AND EXISTS (SELECT 1 FROM u WHERE ug = g)');// [], SQLite gives the same two rows as above
Measured on main (e534707). The dialect test lists two of these cases. A fix for this issue should cover ON as well as WHERE and the select list.
Activity
mateuscmtropical commentedon Nov 11, 2026
Hi, I'd like to work on this one.
mateuscmtropical commentedon Nov 11, 2026
Hi, I've opened a PR for this: #3017
mathiasrw commentedon Nov 22, 2026
The same shape in
JOIN ... ON. Since #2795 an EXISTS in ON compiles, and with an unqualified outer column it is false for every row, without an error.Measured on
main(e534707). The dialect test lists two of these cases. A fix for this issue should cover ON as well as WHERE and the select list.