Skip to content

A correlated subquery in the WHERE of UPDATE or DELETE throws a TypeError #2792

Open

Description

@mathiasrw

A subquery in the WHERE of UPDATE or DELETE that refers to the table being changed by its name throws a TypeError. The same subquery works in a SELECT.

Seen in the NIST SQL Test Suite run in dml011.sql, dml012.sql and dml158.sql. Reproduced on develop (38bf3c3):

alasql("CREATE TABLE staff (empnum CHAR(3), grade INT, city CHAR(15))");
alasql("INSERT INTO staff VALUES ('E1', 12, 'Deale'), ('E2', 10, 'Vienna'), ('E3', 13, 'Vienna')");
alasql("CREATE TABLE works (empnum CHAR(3), pnum CHAR(3), hours INT)");
alasql("INSERT INTO works VALUES ('E1', 'P1', 40), ('E2', 'P1', 20)");

alasql('SELECT empnum FROM staff WHERE staff.empnum NOT IN (SELECT works.empnum FROM works WHERE staff.empnum = works.empnum)');
// [{empnum: 'E3'}]
alasql('UPDATE staff SET grade = 10 * staff.grade WHERE staff.empnum NOT IN (SELECT works.empnum FROM works WHERE staff.empnum = works.empnum)');
// TypeError: undefined is not an object (evaluating 'r['staff']['empnum']')
alasql("DELETE FROM works WHERE EXISTS (SELECT * FROM staff WHERE staff.empnum = works.empnum AND staff.city = 'Vienna')");
// TypeError: undefined is not an object (evaluating 'p['works']['empnum']')

Expected: the UPDATE changes 1 row (E3 gets grade 130), and the DELETE removes 1 row (E2).

Breaking?

No. Today both throw.

Related: #2644 (correlated subquery with an unqualified outer column), #2632.

Activity

  1. mathiasrw commented on Oct 21, 2026

    @mathiasrw
    MemberAuthor

    A simpler case throws the same error, without a subquery: a table-qualified column anywhere in UPDATE (UPDATE t SET u = t.u + 1). Filed as #2814 from the PostgreSQL regression tests; fixing that is likely to fix this one too.

  2. mateuscmtropical commented on Nov 11, 2026

    @mateuscmtropical
    Contributor

    Hi, I'd like to work on this one, together with #2814 (same cause).

  3. mateuscmtropical commented on Nov 11, 2026

    @mateuscmtropical
    Contributor

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

  4. modified the milestones: v4.x Focus, v5.0.0 on Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions