Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are inner and left and right outer joins implemented in SQL Server?

I'm looking for a high-level, algorithmic understanding so that I can get a Big-O sense of what SQL-Server is doing to perform joins. Feel free to be concise, I'm not looking for the extremely nitty gritty. The thing that prompted me to understand how joins are implemented better is the scenario behind this question that I also just posted. I felt like they were ultimately two separate questions though, which is why I didn't combine them.

Thanks!

like image 345
JoeCool Avatar asked Feb 17 '11 20:02

JoeCool


People also ask

What are inner outer left and right joins in SQL?

Different Types of SQL JOINs(INNER) JOIN : Returns records that have matching values in both tables. LEFT (OUTER) JOIN : Returns all records from the left table, and the matched records from the right table. RIGHT (OUTER) JOIN : Returns all records from the right table, and the matched records from the left table.

How is inner join implemented?

Inner Join syntax basically compares rows of Table1 with Table2 to check if anything matches based on the condition provided in the ON clause. When the Join condition is met, it returns matched rows in both tables with the selected columns in the SELECT clause.

What is the difference between and inner left outer and right outer join?

The main difference between the Left Join and Right Join lies in the inclusion of non-matched rows. Left outer join includes the unmatched rows from the table which is on the left of the join clause whereas a Right outer join includes the unmatched rows from the table which is on the right of the join clause.

What is the difference between inner join and left outer join in SQL Server?

The biggest difference between an INNER JOIN and an OUTER JOIN is that the inner join will keep only the information from both tables that's related to each other (in the resulting table). An Outer Join, on the other hand, will also keep information that is not related to the other table in the resulting table.


2 Answers

EDIT (2020-03-11): The links in this 9+ year old answer are all invalid today. I would delete the answer, but SO won't let me since it was accepted back when it was actually useful.

Original Answer:

Here's some reading to get you started.

  • Nested Loop Join ( Wayback )
  • Merge Join ( Wayback )
  • Hash Join ( Wayback )
  • Summary of Join Properties ( Wayback )
like image 79
Joe Stefanelli Avatar answered Nov 03 '22 02:11

Joe Stefanelli


Honestly if you are interested at that level of detail I would suggest you read Microsoft SQL Server 2008 Internals.

And learn to read execution plans. SQL Server has a pretty good optimization engine. It doesn't always do things the way we humans would expect though or even the same way for two queries that appear to us be similar.

like image 38
HLGEM Avatar answered Nov 03 '22 01:11

HLGEM