Skip to content

Incorrect result when JOINing a subselect #832

Closed

Description

@aravinds502

Hi Team,

I found that joins are not working(not giving the desired result). Please follow below steps to recreate problem.

//Create table one
CREATE TABLE one (a INT, b INT);
INSERT INTO one VALUES(1,10),(2,20);

//Create Table two
CREATE TABLE two (a INT, c INT);
INSERT INTO two VALUES(1,100),(2,200);

Now when I run the following query

SELECT t0.`a` AS a, c -b  AS bc FROM (SELECT a,b FROM one) t0 JOIN (SELECT a,c FROM two) t1 ON t0.`a`=t1.`a`;

Result is:

-----------
#	a	c
1      1    100
2	2    200

But the expected result is(if u run in any other relational db)

#  a  bc
1  1  90
2  2  180

Thanks

Activity

  1. mathiasrw commented on Feb 20, 2017

    @mathiasrw
    Member

    Thank you for a very well described bug.

    We might need @agershun to give a hint to where the but could be located?

  2. changed the title [-]Joins are not working[/-] [+]Incorrect result when combining JOIN with select alias on calculated value[/+] on Feb 20, 2017
  3. changed the title [-]Incorrect result when combining JOIN with select alias on calculated value[/-] [+]Incorrect result when JOINing a subselect[/+] on Feb 20, 2017
  4. mathiasrw commented on Feb 20, 2017

    @mathiasrw
    Member

    The problem is in joining a subselect.

    Demonstration of the problem: http://jsfiddle.net/L11cnfbu/

    For now you can get by if you put the result from your subselects into a (temporary) table (or in this example use the name two instead of the subselect) you can also put your subselect into a parameter (as shown in the jsfiddle code)

    Any inputs from @agershun why a join on a subselect will reset the list of column names in use?

  5. aravinds502 commented on Feb 21, 2017

    @aravinds502
    Author

    @mathiasrw Thank you for your very quick reply and work around suggested.

    Could you please let us know when we can expect the support for joining the subselect. Just curious, as it is highly required for one of our current project.

  6. mathiasrw commented on Feb 21, 2017

    @mathiasrw
    Member

    All code here is added when people feel like contributing. There is no timeline.

    As soon as someone provides a PR with a fix I will release a new version.

    Another alternative is to pay to get the fix implemented. Let me know if that has any interest to you.

  7. sasha-alias commented on Mar 12, 2017

    @sasha-alias

    not sure, is this the part of the code related to the issue?
    looks like some implementation started but commented out, most probably not finished.

  8. mathiasrw commented on Mar 12, 2017

    @mathiasrw
    Member

    Hmmmmm. Interesting...

  9. besquared commented on Aug 31, 2017

    @besquared
    Contributor

    +1 just ran into this today.

  10. caesarsol commented on Mar 30, 2018

    @caesarsol

    Hi, a solution for this would be very appreciated!

    In the meanwhile, could you describe what is the most straight-ahead workaround?

  11. besquared commented on Mar 30, 2018

    @besquared
    Contributor

    I haven't worked on this in a few month (I'll be getting back to it soon) but right now you have to materialize each of the sub-query result sets and pass them in using question marks in the final statement.

    var sub = alasql("SELECT * FROM my_sub_table WHERE...");
    var sub2 = alasql("SELECT * FROM my_sub_table2 WHERE...");
    var final = alasql("SELECT * FROM my_table ? ON (...) JOIN ? ON (...) WHERE ...", [sub, sub2]);

    This works and is pretty fast since it's all in memory and whatnot.

  12. caesarsol commented on Mar 30, 2018

    @caesarsol

    I actually got the JOIN working with a WITH clause!

    WITH
    foo AS (
      SELECT geo, SUM(num) AS num
      FROM table1
      WHERE
        type = "B"
      GROUP BY geo
    ),
    bar AS (
      SELECT geo, SUM(num) AS num
      FROM table1
      WHERE
        type = "A"
      GROUP BY geo
    )
    SELECT
      foo.geo AS aggregation,
      foo.num / bar.num AS val
    FROM foo
    JOIN bar
    ON foo.geo = bar.geo;

    Thanks for your approach though, it can be very useful to reuse the temp tables.

    The strange thing is that using the (unsupported) sub-SELECT there is no error given, just the result of the last sub-SELECT. Is that intended?

  13. mathiasrw commented on Apr 2, 2018

    @mathiasrw
    Member

    I actually got the JOIN working with a WITH clause!

    thank you for a great input @caesarsol

  14. caesarsol commented on Apr 10, 2018

    @caesarsol

    Glad you found it useful!
    Let me know if a documentation PR can be useful to include this info in the README, and where you think it might be inserted.

  15. mathiasrw commented on Apr 11, 2018

    @mathiasrw
    Member

    That would be awesome!

  16. randomlysa commented on Jan 17, 2019

    @randomlysa

    is there any way to make this work using JS templates strings? I'm getting "cannot read property 'team' of undefined" - or is there a better way to do this?

        const x = alasql(
          `
          WITH
          homeScore AS (
            SELECT team, SUM(homeScore) as homePoints FROM boys WHERE homeAway = "H" GROUP BY team
          ),
          awayScore AS (
            SELECT team, SUM(awayScore) as awayPoints FROM boys WHERE homeAway = "A" GROUP BY team
          )
          SELECT *
          FROM boys
          JOIN homeScore
          ON awayScore.team = homeScore.team
          `
        );
    

    edit: I was able to use this syntax

    const x = alasql(
          `WITH
          homeScore AS (
            SELECT team, SUM(homeScore) as homePoints FROM boys WHERE homeAway = 'H' GROUP BY team
          ),
          awayScore AS (
            SELECT team, SUM(awayScore) as awayPoints FROM boys WHERE homeAway = 'A' GROUP BY team
          )
          SELECT *
          FROM homeScore
          JOIN  awayScore
          USING team
          `
        );
    
  17. pabloBelt commented on Feb 12, 2023

    @pabloBelt

    The problem is in joining a subselect.

    Demonstration of the problem: http://jsfiddle.net/L11cnfbu/

    For now you can get by if you put the result from your subselects into a (temporary) table (or in this example use the name two instead of the subselect) you can also put your subselect into a parameter (as shown in the jsfiddle code)

    Any inputs from @agershun why a join on a subselect will reset the list of column names in use?

    The right question here would be why the main query returns the subselect results. And bobviously this is because the join is not finished properly! This is relevant otherwise more confusion is brought instead as this is not related to columns... at this fisrt stage.

    Once solved there is another issue (second stage): the subselect support in joins is incomplete (in fact, there is no code to resolve defColumns and it takes a default resolution which is wrong to get the right data from the scope).

    Therefore, to resolve this issue = bug fix in joins + add missing code to support the use case.

  18. added a commit that references this issue on Oct 20, 2026
    c9e3b53
  19. mathiasrw commented on Oct 21, 2026

    @mathiasrw
    Member

    Fix released as part of v4.21.0

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

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions