Mixed Joins

  • Site Owners

    SSC Guru

    Points: 80373

    Comments posted to this topic are about the item Mixed Joins

  • Stewart "Arturius" Campbell

    SSC Guru

    Points: 71868

    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”

  • Tom Litt

    SSC Journeyman

    Points: 75

    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....

  • Andrew Watson-478275

    SSCarpal Tunnel

    Points: 4652

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

  • ChrisM@Work

    SSC Guru

    Points: 186107

    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

    [font="Arial"]“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw[/font]


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

  • call.copse

    SSCoach

    Points: 17211

    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?

  • ChrisM@Work

    SSC Guru

    Points: 186107

    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

    [font="Arial"]“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw[/font]


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

  • Carlo Romagnano

    SSC-Insane

    Points: 21987

    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!

  • ChrisM@Work

    SSC Guru

    Points: 186107

    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.

    [font="Arial"]“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw[/font]


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

  • Shayn Thomas

    SSCertifiable

    Points: 5626

    nice and simple,

    ta

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

  • call.copse

    SSCoach

    Points: 17211

    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!

  • john.lynn-871155

    SSC-Addicted

    Points: 448

    Nice one Ben..

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

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