Skip to content

Nested SubQueries do not work as expected #1924

Closed

Description

@bc-saranya

Description -
Nested subqueries leads to an error TypeError: Cannot read properties of undefined (reading '0')

I have created three tables like below:

alasql("CREATE TABLE cities (city string, population number)");
alasql("INSERT INTO cities VALUES ('Rome',2863223), ('Paris',2249975), ('Berlin',3517424),  ('Madrid',3041579),('Easingwold',4627)");

alasql("CREATE TABLE countries (name string, population number, city string)");
alasql("INSERT INTO countries VALUES ('Italy', 89764679009, 'Rome'), ('France', 165247191, 'Paris'), ('Germany', 346186257, 'Berlin')");

alasql("CREATE TABLE population (number int)");
alasql("INSERT INTO population VALUES (89764679009), (165247191)");

I use a SELECT query with nested subqueries, e.g -

SELECT * FROM cities WHERE city IN (SELECT DISTINCT city FROM countries WHERE population IN (SELECT DISTINCT number from population))

Expected output - Rome, Paris

Actual Output - TypeError: Cannot read properties of undefined (reading '0')

Activity

  1. SuvitsonHarrese commented on Jun 25, 2024

    @SuvitsonHarrese

    alasql("CREATE TABLE cities (city string, population number)");
    alasql("INSERT INTO cities VALUES ('Rome',2863223), ('Paris',2249975), ('Berlin',3517424), ('Madrid',3041579),('Easingwold',4627)");

    alasql("CREATE TABLE countries (name string, population number, city string)");
    ^^^^^^ -> population column is declared as number which is not a valid type replcaing it with int has yield the expected output

    alasql("INSERT INTO countries VALUES ('Italy', 89764679009, 'Rome'), ('France', 165247191, 'Paris'), ('Germany', 346186257, 'Berlin')");

    alasql("CREATE TABLE population (number int)");
    alasql("INSERT INTO population VALUES (89764679009), (165247191)");

    SELECT * FROM cities WHERE city IN (SELECT DISTINCT city FROM countries WHERE population IN (SELECT DISTINCT number from population))

  2. paulrutter commented on Jun 30, 2024

    @paulrutter
    Contributor

    @SuvitsonHarrese https://github.com/AlaSQL/alasql#traditional-sql-table does show number as possible column value though.

    @bc-saranya we can check this out.

    It would be nice if the parser would throw an error on unknown data types though.

    Somewhere here probably

    if (this.dbtypeid) {

  3. paulrutter commented on Jun 30, 2024

    @paulrutter
    Contributor

    @SuvitsonHarrese I tried by changing the data type to int, but still the same error occurs.
    What did you try to resolve it?

  4. SuvitsonHarrese commented on Jun 30, 2024

    @SuvitsonHarrese

    yeah now i checked again I got the exact same error
    dont know how it worked on that day

  5. SuvitsonHarrese commented on Jun 30, 2024

    @SuvitsonHarrese

    @paulrutter i think the problem is with the nesting in subqueries that means a query can have one subquery and no subquery within subquery of main query

    alasql("CREATE TABLE t1 (points int)");
    alasql("INSERT INTO  t1 (1),(2),(3),(4)");
    
    alasql("CREATE TABLE t2 (height int)");
    
    alasql("INSERT INTO t2 VALUES (2), (3), (4)");
    
    alasql("CREATE TABLE t3 (breadth int)");
    alasql("INSERT INTO t3 VALUES (3), (4)");
    console.log(
      alasql("SELECT * FROM t2 WHERE height IN (SELECT breadth from t3)")
    );
    
    console.log(alasql("SELECT * FROM t2 WHERE height IN (SELECT points from t1)"));
    
    console.log(
      alasql("SELECT * FROM t3 WHERE breadth IN (SELECT points from t1)")
    );
    console.log(
      alasql("SELECT * FROM t3 WHERE breadth IN (SELECT breadth from t3)")
    );
    
    console.log(
      alasql(
        "SELECT * FROM t3 WHERE breadth IN (SELECT * FROM t3 WHERE breadth IN (SELECT breadth from t3))"
      )
    );
    // last line of code is returning the same error whereas other query with different combination of subqueries are working fine
    
  6. choia4 commented on Jul 17, 2024

    @choia4

    hi, I was wondering if this issue got resolved. If not, can I pick it up?

  7. paulrutter commented on Jul 17, 2024

    @paulrutter
    Contributor

    It's not resolved as far as i know, so you can work on it for sure. @choia4

  8. znenuko commented on Aug 21, 2024

    @znenuko

    Verify Subqueries Individually:

    Subquery 1:
    SELECT DISTINCT city FROM countries WHERE population IN (
        SELECT DISTINCT number FROM population
    );
    

    Ensure this subquery returns expected city values.

    Subquery 2:

    SELECT DISTINCT number FROM population;
    

    Ensure this subquery returns the correct numbers.

    Execute the Main Query:

    With verified subqueries, combine them into the main query:

    alasql("CREATE TABLE cities (city string, population number)");
    alasql("INSERT INTO cities VALUES ('Rome',2863223), ('Paris',2249975), ('Berlin',3517424),  ('Madrid',3041579),('Easingwold',4627)");
    
    alasql("CREATE TABLE countries (name string, population number, city string)");
    alasql("INSERT INTO countries VALUES ('Italy', 89764679009, 'Rome'), ('France', 165247191, 'Paris'), ('Germany', 346186257, 'Berlin')");
    
    alasql("CREATE TABLE population (number int)");
    alasql("INSERT INTO population VALUES (89764679009), (165247191)");
    
    // Execute the query
    let result = alasql(`
        SELECT * FROM cities WHERE city IN (
            SELECT DISTINCT city FROM countries WHERE population IN (
                SELECT DISTINCT number FROM population
            )
        )
    `);
    
    console.log(result);  // Expected to show 'Rome' and 'Paris'
    
  9. deanmcgregor commented on Jul 30, 2025

    @deanmcgregor

    Hi,

    I'm seeing the same issue with some nesting subqueries. I reduced my case down a bit, see below:

            const data = [
                {"PullRequest.number": "111"},
                {"PullRequest.number": "112"},
                {"PullRequest.number": "113"},
                {"PullRequest.number": "114"},
                {"PullRequest.number": "115"},
                {"PullRequest.number": "116"},
                {"PullRequest.number": "117"},
                {"PullRequest.number": "118"},
                {"PullRequest.number": "119"},
            ];
            const sql = `select count(*) as c from
                (select [PullRequest.number] from ?  where
                    [PullRequest.number] not in (select "111")
                )`
            const res = alasql(sql, [data]);
    

    I would expect a result with 8, but instead I get the same error (TypeError: Cannot read properties of undefined (reading '0')).

    Just to sanity check I ran the query through our Postgres database (with table/field names changed) and this appears to be compliant there.

  10. assigned and unassigned on Nov 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions