View Single Post

Thread: SQL: RIGHT or LEFT JOIN more efficient

  1. - Top - End - #1
    Ettin in the Playground

    Join Date
    Jan 2009

    Default SQL: RIGHT or LEFT JOIN more efficient

    From my understanding, a RIGHT or LEFT JOIN is functionally the same in the sense that data1 RIGHT JOIN data2 is the meaning as data2 LEFT JOIN data1. That makes me think they would be equally efficient, because the SQL engine would be doing the same thing 'behind the scenes'.
    But in a recent programming job, something made me wonder. (the 'something' was I used up the entire server's memory, but I think it was more the ORDER BY than any JOIN)

    I know there's a lot of 'dialects' of SQL out there, and realize the answer may change based on the actual SQL compiler (?) being used. I'm using the PROC SQL utility in SAS to do SQL coding right now, but this is also a general question.

    Is RIGHT or LEFT sometimes more efficient?
    I could see it being the case when you only expect a small number of matches via the JOIN. E.g., maybe LEFT is more efficient if the data on the right side will have only a few matches via the ON variables.

    edit: FYI that I learned the server was clogged with a ton of orphaned data, so it wasn't just my task being overly large.
    Last edited by JeenLeen; 2021-03-08 at 11:21 AM.