Mixed Joins

  • Comments posted to this topic are about the item Mixed Joins

  • Nice question, thanks Ben

    Trying to explain this to the uninitiated is not simple....

    ____________________________________________
    Space, the final frontier? not any more...
    All limits henceforth are self-imposed.
    “libera tute vulgaris ex”

  • Definitely a good question! I've seen lots of people query this behaviour. And then trying to explain to them how to "fix" it to get what they intended....

  • Of course, in a case sensitive database, the answer is "none of the above".  table_a <> Table_a <> TABLE_A.

  • Force the join between B and C to take place first, and you change the result:
    SELECT *
    FROM TABLE_A
    LEFT JOIN  TABLE_B
    INNER JOIN TABLE_C
     ON TABLE_B.ID = TABLE_C.ID
     ON TABLE_A.ID = TABLE_B.ID;
    -- 3 rows

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • ChrisM@Work - Wednesday, November 7, 2018 3:36 AM

    Force the join between B and C to take place first, and you change the result:
    SELECT *
    FROM TABLE_A
    LEFT JOIN  TABLE_B
    INNER JOIN TABLE_C
     ON TABLE_B.ID = TABLE_C.ID
     ON TABLE_A.ID = TABLE_B.ID;
    -- 3 rows

    I get what you're saying, but wouldn't it be clearer to left join to C?

  • call.copse - Wednesday, November 7, 2018 3:50 AM

    ChrisM@Work - Wednesday, November 7, 2018 3:36 AM

    Force the join between B and C to take place first, and you change the result:
    SELECT *
    FROM TABLE_A
    LEFT JOIN  TABLE_B
    INNER JOIN TABLE_C
     ON TABLE_B.ID = TABLE_C.ID
     ON TABLE_A.ID = TABLE_B.ID;
    -- 3 rows

    I get what you're saying, but wouldn't it be clearer to left join to C?

    No, because the queries are not logically equivalent:
    SELECT *
    FROM TABLE_A
    LEFT JOIN  TABLE_B
    INNER JOIN TABLE_C
     ON TABLE_B.ID = TABLE_C.ID
     ON TABLE_A.ID = TABLE_B.ID;
    SELECT *
    FROM TABLE_A
      LEFT JOIN  TABLE_B
        ON TABLE_A.ID = TABLE_B.ID
      left JOIN TABLE_C
        ON TABLE_B.ID = TABLE_C.ID
    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • ChrisM@Work - Wednesday, November 7, 2018 3:36 AM

    Force the join between B and C to take place first, and you change the result:
    SELECT *
    FROM TABLE_A
    LEFT JOIN  TABLE_B
    INNER JOIN TABLE_C
     ON TABLE_B.ID = TABLE_C.ID
     ON TABLE_A.ID = TABLE_B.ID;
    -- 3 rows

    Thanks! That is what I'm searching for!

  • Carlo Romagnano - Wednesday, November 7, 2018 4:32 AM

    ChrisM@Work - Wednesday, November 7, 2018 3:36 AM

    Force the join between B and C to take place first, and you change the result:
    SELECT *
    FROM TABLE_A
    LEFT JOIN  TABLE_B
    INNER JOIN TABLE_C
     ON TABLE_B.ID = TABLE_C.ID
     ON TABLE_A.ID = TABLE_B.ID;
    -- 3 rows

    Thanks! That is what I'm searching for!

    You're welcome. There's a little more info and a better example here.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • nice and simple,

    ta

    ---------------------------------------------------------------------------------------
    The more you know, the more you know that you dont know

  • ChrisM@Work - Wednesday, November 7, 2018 3:57 AM

    call.copse - Wednesday, November 7, 2018 3:50 AM

    ChrisM@Work - Wednesday, November 7, 2018 3:36 AM

    Force the join between B and C to take place first, and you change the result:
    SELECT *
    FROM TABLE_A
    LEFT JOIN  TABLE_B
    INNER JOIN TABLE_C
     ON TABLE_B.ID = TABLE_C.ID
     ON TABLE_A.ID = TABLE_B.ID;
    -- 3 rows

    I get what you're saying, but wouldn't it be clearer to left join to C?

    No, because the queries are not logically equivalent:
    SELECT *
    FROM TABLE_A
    LEFT JOIN  TABLE_B
    INNER JOIN TABLE_C
     ON TABLE_B.ID = TABLE_C.ID
     ON TABLE_A.ID = TABLE_B.ID;
    SELECT *
    FROM TABLE_A
      LEFT JOIN  TABLE_B
        ON TABLE_A.ID = TABLE_B.ID
      left JOIN TABLE_C
        ON TABLE_B.ID = TABLE_C.ID

    I read the link you posted below and now consider myself elucidated, thank you. After all these years there is plenty to learn even on relatively basic querying!

  • Nice one Ben..

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply