Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL inner vs left - precedence

Posted on 2013-12-20
1
Medium Priority
?
921 Views
Last Modified: 2013-12-20
Hi,
 In MS SQL 2008, I am looking at a sample code like below:

Select *
from table1  t1 inner join table2 t2 on t1.id = t2.id

inner join table3 t3 on t1.id = t3.id

left join table4 t4 on t1.id = t4.id


I wanted to understand which join takes precedence:
1. Do all the three inner join execute first and then the resultset is left joined to the last table? In this case, the precedence is by virtue of which join is written first?

2. Inner joins take precedence over left join , regardless of in which order they are submitted?

3. Left join takes precedence over the inner join?

thx
0
Comment
Question by:LuckyLucks
1 Comment
 
LVL 10

Accepted Solution

by:
PadawanDBA earned 2000 total points
ID: 39732593
Join order evaluates left to right.  The result of the previous join is the left input to the next join.  Where this can introduce unintended consquences is when you have an outer join before an inner join and then you join to the table that was outer joined.  If there are null values that are preserved from the outer join and those values are used to join another table via an inner join, they will be filtered out.  You can get around this by parenthetically grouping your joins together to force the inner join to be evaluated before the outer join.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Loops Section Overview

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question