3 responses

  1. Paul White
    2011-10-04

    Hi Wayne,

    A good read, thanks. Regarding the bit with ON clauses in reverse order, it’s probably clearer to write the query another way, for example:

    SELECT
    p.ProductId,
    p.Name,
    i.InvoiceId,
    s.ShipperId
    FROM
    @Invoices AS i
    INNER JOIN @Shippers AS s ON
    s.InvoiceId = i.InvoiceId
    RIGHT JOIN @Products AS p ON
    p.ProductId = i.ProductId

    — or

    SELECT
    p.ProductId,
    p.Name,
    i.InvoiceId,
    s.ShipperId
    FROM @Products AS p
    LEFT JOIN
    (
    @Invoices AS i
    INNER JOIN @Shippers AS s ON
    s.InvoiceId = i.InvoiceId
    ) ON i.ProductId = p.ProductId

    The second one is essentially the same, but I think the parentheses make the intended logic a bit clearer.

    Cheers,

    Paul

    Reply

  2. Wayne Sheffield
    2011-10-05

    Thanks Paul.
    I agree that the examples I gave could have easily been written a different way. I also agree with the use of parenthesis or indention to make the intention clearer (hey, didn’t I say that already?)

    I think that Tracy McKibben did a better example of the Nested Joins for his blog post for today at //www.real-sql-guy.com/2011/09/t-sql-tuesday-23-nested-joins.html

    Reply

    • Paul White
      2011-10-05

      Wayne,

      Sorry, for some reason I completely skipped reading the conclusion! Shame on me.

      Paul

      Reply

Leave a Reply to let me know how you liked this post

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top
mobile desktop