Skip to content

Correlated subquery with an unqualified outer column returns no rows #2644

Open

Description

@mathiasrw

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:

CREATE TABLE a (id INT, ak INT); INSERT INTO a VALUES (1, 1), (2, 2), (3, 1);
CREATE TABLE b (id INT, bk INT); INSERT INTO b VALUES (1, 1), (2, 2), (3, 2);

SELECT id FROM a WHERE id IN (SELECT b.id FROM b WHERE b.bk = a.ak);  -- AlaSQL 1, 2   SQLite 1, 2
SELECT id FROM a WHERE id IN (SELECT b.id FROM 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.

Related: #2281.

Activity

  1. mateuscmtropical commented on Nov 11, 2026

    @mateuscmtropical
    Contributor

    Hi, I'd like to work on this one.

  2. mateuscmtropical commented on Nov 11, 2026

    @mateuscmtropical
    Contributor

    Hi, I've opened a PR for this: #3017

  3. modified the milestones: v4.x Focus, v5.0.0 on Nov 15, 2026
  4. mathiasrw commented on Nov 22, 2026

    @mathiasrw
    MemberAuthor

    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.

    var db = new alasql.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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions