{"id":501,"date":"2010-05-16T21:17:00","date_gmt":"2010-05-16T21:17:00","guid":{"rendered":"\/blogs\/kimberly\/post\/Determining-the-position-of-search-arguments-in-a-join.aspx"},"modified":"2016-06-17T04:56:58","modified_gmt":"2016-06-17T11:56:58","slug":"determining-the-position-of-search-arguments-in-a-join","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/determining-the-position-of-search-arguments-in-a-join\/","title":{"rendered":"Determining the position of search arguments in a join"},"content":{"rendered":"<p><span style=\"font-family: Arial;\">As a follow-on discussion to my\u00a0SQL Server Magazine <span style=\"font-family: Arial;\">Q&amp;A titled \u201c<a href=\"http:\/\/sqlmag.com\/blog\/determining-position-search-arguments-within-join\" target=\"_blank\">Determining the position of search arguments in a join<\/a>\u201d where I was responding to a reader question about whether or not search arguments could be moved up into the FROM clause instead of placed in the WHERE clause and whether or not it would improve performance. While I was so focused on creating a scenario to highlight the problems when doing this, I missed a great point about what happened in my <em>specific<\/em> example. Fellow SQL Server MVP, Phil Brammer (<a href=\"http:\/\/www.ssistalk.com\/\" target=\"_blank\">blog<\/a> | <a href=\"http:\/\/twitter.com\/PhilBrammer\" target=\"_blank\" class=\"broken_link\">twitter<\/a>)\u00a0brought up this point first and it sparked a very passionate discussion on the SQL Server MVP forum. As a result, I thought I\u2019d take a few minutes to create a few more examples and state a few key points. The most important of which is that search arguments should not be moved from the WHERE clause into the FROM clause unless you really know what you\u2019re doing &#8211; you may completely change the meaning of the query\u2026 Let me explain. <\/span><\/span><\/p>\n<p><span style=\"font-family: Arial;\"><strong><span style=\"font-family: Arial;\">Details:<\/span><\/strong> <\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 10pt;\"><span style=\"font-family: Arial; font-size: small;\">In the prior Q&amp;A, Listing 1 and Listing 2 show INNER JOINs that describe the Customers along with some of their Sales information (specifically: OrderDate, AccountNumber, FirstName, LastName, and StateProvinceCode). In the joins, we asked for only Customers with a RegionCode of US. Regardless of having the search argument in the WHERE clause or the FROM clause, SQL Server executes the same plan, has the same performance and returns the same data. While people often state that a WHERE clause applies to the entire set (and also imply order), that\u2019s NOT the case in terms of how the join is processed. SQL Server knows that the search argument applies to the entire set but it can certainly apply the filter prior to the join if it\u2019s selective enough and would help improve performance. Moving the search argument up into the FROM clause is not a \u201ctip\u201d for improving performance. However, if all of the joins are inner joins, then it really doesn\u2019t matter. You can certainly move the search arguments up; they won\u2019t change the result set and it\u2019s very unlikely that they\u2019ll change the query plan. Listing 1 and Listing 2 were both executed at the same time to produce these two plans: <\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 10pt;\">\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 10pt;\">\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 10pt;\"><span style=\"font-family: Arial; font-size: small;\"><img decoding=\"async\" src=\"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-content\/uploads\/2010\/5\/queryplans.jpg\" alt=\"\" \/> <\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 10pt;\"><span style=\"font-family: Arial; font-size: small;\">First, notice that they are 50% each. Also, without worrying about the plan itself, notice that each plan has the same pattern, the same percentages, and the same join types. These are in fact, the same plan \u2013 Listing 1 has the search argument in the WHERE clause and Listing 2 has the search argument in the FROM clause. <\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 10pt;\"><span style=\"font-family: Arial; font-size: small;\">To illustrate how moving a search argument could affect results, I created OUTER joins, added a somewhat random search argument and then did the same thing \u2013 Listing 3 was written as an outer join and Listing 4 was a copy of that query with the search argument moved up into the FROM clause. While my examples (<strong><em>solely looking at the results between Listing 3 and Listing 4<\/em><\/strong>) prove the point that a search argument cannot be moved from the WHERE clause into the FROM clause without understanding the effects, my example also introduced another phenomena. It was this phenomena that sparked debate. When a search argument exists on an inner table of an outer join, the purpose of the outer join is effectively lost. In fact, the query is not likely to even work the way you expect. To illustrate this, I\u2019ve created a [quick] Venn diagram of what\u2019s logically happening: <\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 10pt;\">\n<div style=\"text-align: center;\"><img decoding=\"async\" src=\"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-content\/uploads\/2010\/5\/Venn%20diagrams%20-%201024x768.jpg\" alt=\"\" \/><\/div>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 10pt;\">\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 10pt;\">\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 10pt;\"><span style=\"font-family: Arial; font-size: small;\">In fact, the two following queries will produce identical results: <\/span><\/p>\n<blockquote><p><span style=\"font-family: 'Lucida Console'; color: green; font-size: 12pt;\">&#8212; The original Listing 3 query<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">SELECT<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> so<span style=\"color: gray;\">.<\/span>OrderDate<span style=\"color: gray;\">,<\/span> c<span style=\"color: gray;\">.<\/span>AccountNumber<span style=\"color: gray;\">,<\/span> p<span style=\"color: gray;\">.<\/span>FirstName<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">,<\/span> p<span style=\"color: gray;\">.<\/span>LastName<span style=\"color: gray;\">,<\/span> sp<span style=\"color: gray;\">.<\/span>StateProvinceCode<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">FROM<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> Sales<span style=\"color: gray;\">.<\/span>Customer <span style=\"color: blue;\">AS<\/span> c<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">LEFT<\/span> <span style=\"color: gray;\">OUTER<\/span> <span style=\"color: gray;\">JOIN<\/span> Sales<span style=\"color: gray;\">.<\/span>SalesOrderHeader <span style=\"color: blue;\">AS<\/span> so<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> so<span style=\"color: gray;\">.<\/span>CustomerID <span style=\"color: gray;\">=<\/span> c<span style=\"color: gray;\">.<\/span>CustomerID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">LEFT<\/span> <span style=\"color: gray;\">OUTER<\/span> <span style=\"color: gray;\">JOIN<\/span> Person<span style=\"color: gray;\">.<\/span>Person <span style=\"color: blue;\">AS<\/span> p<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> c<span style=\"color: gray;\">.<\/span>PersonID <span style=\"color: gray;\">=<\/span> p<span style=\"color: gray;\">.<\/span>BusinessEntityID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">LEFT<\/span> <span style=\"color: gray;\">OUTER<\/span> <span style=\"color: gray;\">JOIN<\/span> Person<span style=\"color: gray;\">.<\/span><span style=\"color: blue;\">Address<\/span> <span style=\"color: blue;\">AS<\/span> pa<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> so<span style=\"color: gray;\">.<\/span>ShipToAddressID <span style=\"color: gray;\">=<\/span> pa<span style=\"color: gray;\">.<\/span>AddressID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">LEFT<\/span> <span style=\"color: gray;\">OUTER<\/span> <span style=\"color: gray;\">JOIN<\/span> Person<span style=\"color: gray;\">.<\/span>StateProvince <span style=\"color: blue;\">AS<\/span> sp<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> sp<span style=\"color: gray;\">.<\/span>StateProvinceID <span style=\"color: gray;\">=<\/span> pa<span style=\"color: gray;\">.<\/span>StateProvinceID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">WHERE<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> sp<span style=\"color: gray;\">.<\/span>CountryRegionCode <span style=\"color: gray;\">=<\/span> <span style=\"color: red;\">&#8216;US&#8217;<br \/>\n<\/span><\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">ORDER<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> <span style=\"color: blue;\">BY<\/span> sp<span style=\"color: gray;\">.<\/span>StateProvinceCode<span style=\"color: gray;\">,<\/span> so<span style=\"color: gray;\">.<\/span>OrderDate<\/span><span style=\"font-family: 'Lucida Console'; color: green; font-size: 12pt;\">\u00a0<\/span><\/p>\n<p><span style=\"font-family: 'Lucida Console'; color: green; font-size: 12pt;\">&#8212; Listing 3 is really an inner join<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">SELECT<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> so<span style=\"color: gray;\">.<\/span>OrderDate<span style=\"color: gray;\">,<\/span> c<span style=\"color: gray;\">.<\/span>AccountNumber<span style=\"color: gray;\">,<\/span> p<span style=\"color: gray;\">.<\/span>FirstName<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">,<\/span> p<span style=\"color: gray;\">.<\/span>LastName<span style=\"color: gray;\">,<\/span> sp<span style=\"color: gray;\">.<\/span>StateProvinceCode<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">FROM<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> Sales<span style=\"color: gray;\">.<\/span>Customer <span style=\"color: blue;\">AS<\/span> c<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">INNER<\/span> <span style=\"color: gray;\">JOIN<\/span> Sales<span style=\"color: gray;\">.<\/span>SalesOrderHeader <span style=\"color: blue;\">AS<\/span> so<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> so<span style=\"color: gray;\">.<\/span>CustomerID <span style=\"color: gray;\">=<\/span> c<span style=\"color: gray;\">.<\/span>CustomerID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">INNER<\/span> <span style=\"color: gray;\">JOIN<\/span> Person<span style=\"color: gray;\">.<\/span>Person <span style=\"color: blue;\">AS<\/span> p<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> c<span style=\"color: gray;\">.<\/span>PersonID <span style=\"color: gray;\">=<\/span> p<span style=\"color: gray;\">.<\/span>BusinessEntityID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">INNER<\/span> <span style=\"color: gray;\">JOIN<\/span> Person<span style=\"color: gray;\">.<\/span><span style=\"color: blue;\">Address<\/span> <span style=\"color: blue;\">AS<\/span> pa<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> so<span style=\"color: gray;\">.<\/span>ShipToAddressID <span style=\"color: gray;\">=<\/span> pa<span style=\"color: gray;\">.<\/span>AddressID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">INNER<\/span> <span style=\"color: gray;\">JOIN<\/span> Person<span style=\"color: gray;\">.<\/span>StateProvince <span style=\"color: blue;\">AS<\/span> sp<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> sp<span style=\"color: gray;\">.<\/span>StateProvinceID <span style=\"color: gray;\">=<\/span> pa<span style=\"color: gray;\">.<\/span>StateProvinceID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">WHERE<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> sp<span style=\"color: gray;\">.<\/span>CountryRegionCode <span style=\"color: gray;\">=<\/span> <span style=\"color: red;\">&#8216;US&#8217;<br \/>\n<\/span><\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">ORDER<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> <span style=\"color: blue;\">BY<\/span> sp<span style=\"color: gray;\">.<\/span>StateProvinceCode<span style=\"color: gray;\">,<\/span> so<span style=\"color: gray;\">.<\/span>OrderDate<\/span><span style=\"font-family: Arial; font-size: small;\">\u00a0 <\/span><\/p><\/blockquote>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 10pt;\"><span style=\"font-family: Arial; font-size: small;\">A better example \u2013 which preserved the purpose of the outer join \u2013 would be to place a search argument on the table which is our outer table in the query. Before I show the query itself, let\u2019s get some background on the data here. <\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 10pt;\"><span style=\"font-family: Arial; font-size: small;\">In the Sales.Customer table there are 3,520 Customers in TerritoryID 1 (the northwestern US): <\/span><\/p>\n<blockquote><p><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">SELECT<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> <span style=\"color: fuchsia;\">COUNT<\/span><span style=\"color: gray;\">(*)<\/span><br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">FROM<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> Sales<span style=\"color: gray;\">.<\/span>Customer <span style=\"color: blue;\">AS<\/span> c<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">WHERE<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> c<span style=\"color: gray;\">.<\/span>TerritoryID <span style=\"color: gray;\">=<\/span> 1 <span style=\"color: green;\">&#8212; 3520 customers<\/span><\/span><span style=\"font-family: 'Lucida Console'; color: green; font-size: 12pt;\">\u00a0<\/span><\/p><\/blockquote>\n<p><span style=\"font-family: Arial; font-size: small;\">These Customers represent a total number of Sales of 4,594 orders: <\/span><\/p>\n<blockquote><p><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">SELECT<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> <span style=\"color: fuchsia;\">COUNT<\/span><span style=\"color: gray;\">(*)<\/span><br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">FROM<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> Sales<span style=\"color: gray;\">.<\/span>Customer <span style=\"color: blue;\">AS<\/span> c<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">JOIN<\/span> Sales<span style=\"color: gray;\">.<\/span>SalesOrderHeader <span style=\"color: blue;\">AS<\/span> so<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> so<span style=\"color: gray;\">.<\/span>CustomerID <span style=\"color: gray;\">=<\/span> c<span style=\"color: gray;\">.<\/span>CustomerID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">WHERE<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> c<span style=\"color: gray;\">.<\/span>TerritoryID <span style=\"color: gray;\">=<\/span> 1\u00a0 <span style=\"color: green;\">&#8212; 4594 orders<\/span><\/span><span style=\"font-family: 'Lucida Console'; color: green; font-size: 12pt;\">\u00a0<\/span><\/p><\/blockquote>\n<p><span style=\"font-family: Arial; font-size: small;\">There are 92 customers that have never placed an order: <\/span><\/p>\n<blockquote><p><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">SELECT<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> <span style=\"color: fuchsia;\">COUNT<\/span><span style=\"color: gray;\">(*)<\/span><br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">FROM<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> Sales<span style=\"color: gray;\">.<\/span>Customer <span style=\"color: blue;\">AS<\/span> c<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">WHERE<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> <span style=\"color: gray;\">NOT<\/span> <span style=\"color: gray;\">EXISTS<\/span> <span style=\"color: gray;\">(<\/span><span style=\"color: blue;\">SELECT<\/span> <span style=\"color: gray;\">*<br \/>\n<\/span><\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">FROM<\/span> Sales<span style=\"color: gray;\">.<\/span>SalesOrderHeader <span style=\"color: blue;\">AS<\/span> so<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">WHERE<\/span> so<span style=\"color: gray;\">.<\/span>CustomerID <span style=\"color: gray;\">=<\/span> c<span style=\"color: gray;\">.<\/span>CustomerID<span style=\"color: gray;\">)<br \/>\n<\/span><\/span><span style=\"font-family: 'Lucida Console'; color: gray; font-size: 12pt;\">AND<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> c<span style=\"color: gray;\">.<\/span>TerritoryID <span style=\"color: gray;\">=<\/span> 1 <span style=\"color: green;\">&#8212; 92 customers have not placed orders<\/span><\/span><span style=\"font-family: 'Lucida Console'; color: green; font-size: 12pt;\">\u00a0<\/span><\/p><\/blockquote>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 10pt;\"><span style=\"font-family: Arial; font-size: small;\">If we want to see ALL Customers in TerritoryID 1 \u2013 regardless of whether or not they have placed an order and with their order information if they have, we can use an outer join to do this. For those customers who have not placed an order, NULLs will be produced: <\/span><\/p>\n<blockquote><p><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">SELECT<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> so<span style=\"color: gray;\">.<\/span>OrderDate<span style=\"color: gray;\">,<\/span> c<span style=\"color: gray;\">.<\/span>AccountNumber<span style=\"color: gray;\">,<\/span> p<span style=\"color: gray;\">.<\/span>FirstName<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">,<\/span> p<span style=\"color: gray;\">.<\/span>LastName<span style=\"color: gray;\">,<\/span> sp<span style=\"color: gray;\">.<\/span>StateProvinceCode<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">FROM<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> Sales<span style=\"color: gray;\">.<\/span>Customer <span style=\"color: blue;\">AS<\/span> c<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">LEFT<\/span> <span style=\"color: gray;\">OUTER<\/span> <span style=\"color: gray;\">JOIN<\/span> Sales<span style=\"color: gray;\">.<\/span>SalesOrderHeader <span style=\"color: blue;\">AS<\/span> so<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> so<span style=\"color: gray;\">.<\/span>CustomerID <span style=\"color: gray;\">=<\/span> c<span style=\"color: gray;\">.<\/span>CustomerID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">LEFT<\/span> <span style=\"color: gray;\">OUTER<\/span> <span style=\"color: gray;\">JOIN<\/span> Person<span style=\"color: gray;\">.<\/span>Person <span style=\"color: blue;\">AS<\/span> p<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> c<span style=\"color: gray;\">.<\/span>PersonID <span style=\"color: gray;\">=<\/span> p<span style=\"color: gray;\">.<\/span>BusinessEntityID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">LEFT<\/span> <span style=\"color: gray;\">OUTER<\/span> <span style=\"color: gray;\">JOIN<\/span> Person<span style=\"color: gray;\">.<\/span><span style=\"color: blue;\">Address<\/span> <span style=\"color: blue;\">AS<\/span> pa<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> so<span style=\"color: gray;\">.<\/span>ShipToAddressID <span style=\"color: gray;\">=<\/span> pa<span style=\"color: gray;\">.<\/span>AddressID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">LEFT<\/span> <span style=\"color: gray;\">OUTER<\/span> <span style=\"color: gray;\">JOIN<\/span> Person<span style=\"color: gray;\">.<\/span>StateProvince <span style=\"color: blue;\">AS<\/span> sp<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> sp<span style=\"color: gray;\">.<\/span>StateProvinceID <span style=\"color: gray;\">=<\/span> pa<span style=\"color: gray;\">.<\/span>StateProvinceID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">WHERE<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> C<span style=\"color: gray;\">.<\/span>TerritoryID <span style=\"color: gray;\">=<\/span> 1 <span style=\"color: green;\">&#8212; 4686 rows<br \/>\n<\/span><\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">ORDER<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> <span style=\"color: blue;\">BY<\/span> sp<span style=\"color: gray;\">.<\/span>StateProvinceCode<span style=\"color: gray;\">,<\/span> so<span style=\"color: gray;\">.<\/span>OrderDate<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0<\/span><\/p><\/blockquote>\n<p><span style=\"font-family: Arial; font-size: small;\">This is different from an inner join because the INNER join will return only those customers that have a matching row in each and every joined table. In this case, all 3,520 customers have a matching row in Person.Person, Person.Address and Person.StateProvince. As a result, the inner join produces all 3,520 customers with their sales for 4,594 total rows. If you notice, the outer join above returned 4686 rows (4686 &#8211; 4594 = 92). <\/span><\/p>\n<blockquote><p><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">SELECT<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> so<span style=\"color: gray;\">.<\/span>OrderDate<span style=\"color: gray;\">,<\/span> c<span style=\"color: gray;\">.<\/span>AccountNumber<span style=\"color: gray;\">,<\/span> p<span style=\"color: gray;\">.<\/span>FirstName<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">,<\/span> p<span style=\"color: gray;\">.<\/span>LastName<span style=\"color: gray;\">,<\/span> sp<span style=\"color: gray;\">.<\/span>StateProvinceCode<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">FROM<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> Sales<span style=\"color: gray;\">.<\/span>Customer <span style=\"color: blue;\">AS<\/span> c<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">INNER<\/span> <span style=\"color: gray;\">JOIN<\/span> Sales<span style=\"color: gray;\">.<\/span>SalesOrderHeader <span style=\"color: blue;\">AS<\/span> so<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> so<span style=\"color: gray;\">.<\/span>CustomerID <span style=\"color: gray;\">=<\/span> c<span style=\"color: gray;\">.<\/span>CustomerID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">INNER<\/span> <span style=\"color: gray;\">JOIN<\/span> Person<span style=\"color: gray;\">.<\/span>Person <span style=\"color: blue;\">AS<\/span> p<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> c<span style=\"color: gray;\">.<\/span>PersonID <span style=\"color: gray;\">=<\/span> p<span style=\"color: gray;\">.<\/span>BusinessEntityID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">INNER<\/span> <span style=\"color: gray;\">JOIN<\/span> Person<span style=\"color: gray;\">.<\/span><span style=\"color: blue;\">Address<\/span> <span style=\"color: blue;\">AS<\/span> pa<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> so<span style=\"color: gray;\">.<\/span>ShipToAddressID <span style=\"color: gray;\">=<\/span> pa<span style=\"color: gray;\">.<\/span>AddressID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">INNER<\/span> <span style=\"color: gray;\">JOIN<\/span> Person<span style=\"color: gray;\">.<\/span>StateProvince <span style=\"color: blue;\">AS<\/span> sp<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> sp<span style=\"color: gray;\">.<\/span>StateProvinceID <span style=\"color: gray;\">=<\/span> pa<span style=\"color: gray;\">.<\/span>StateProvinceID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">WHERE<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> C<span style=\"color: gray;\">.<\/span>TerritoryID <span style=\"color: gray;\">=<\/span> 1 <span style=\"color: green;\">&#8212; 4594<br \/>\n<\/span><\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">ORDER<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> <span style=\"color: blue;\">BY<\/span> sp<span style=\"color: gray;\">.<\/span>StateProvinceCode<span style=\"color: gray;\">,<\/span> so<span style=\"color: gray;\">.<\/span>OrderDate<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0<\/span><\/p><\/blockquote>\n<p><span style=\"font-family: Arial; font-size: small;\">Using this query as our example, let\u2019s go back to the original discussion about whether or not moving the search argument can improve performance\u2026 In the query below, I *solely* move the search argument (c.TerritoryID = 1) up into the FROM clause for the following query: <\/span><\/p>\n<blockquote><p><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">SELECT<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> so<span style=\"color: gray;\">.<\/span>OrderDate<span style=\"color: gray;\">,<\/span> c<span style=\"color: gray;\">.<\/span>AccountNumber<span style=\"color: gray;\">,<\/span> p<span style=\"color: gray;\">.<\/span>FirstName<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">,<\/span> p<span style=\"color: gray;\">.<\/span>LastName<span style=\"color: gray;\">,<\/span> sp<span style=\"color: gray;\">.<\/span>StateProvinceCode<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">FROM<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> Sales<span style=\"color: gray;\">.<\/span>Customer <span style=\"color: blue;\">AS<\/span> c<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">LEFT<\/span> <span style=\"color: gray;\">OUTER<\/span> <span style=\"color: gray;\">JOIN<\/span> Sales<span style=\"color: gray;\">.<\/span>SalesOrderHeader <span style=\"color: blue;\">AS<\/span> so<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> so<span style=\"color: gray;\">.<\/span>CustomerID <span style=\"color: gray;\">=<\/span> c<span style=\"color: gray;\">.<\/span>CustomerID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">AND<\/span> C<span style=\"color: gray;\">.<\/span>TerritoryID <span style=\"color: gray;\">=<\/span> 1 <span style=\"color: green;\">&#8212; 20986 rows<br \/>\n<\/span><\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">LEFT<\/span> <span style=\"color: gray;\">OUTER<\/span> <span style=\"color: gray;\">JOIN<\/span> Person<span style=\"color: gray;\">.<\/span>Person <span style=\"color: blue;\">AS<\/span> p<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> c<span style=\"color: gray;\">.<\/span>PersonID <span style=\"color: gray;\">=<\/span> p<span style=\"color: gray;\">.<\/span>BusinessEntityID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">LEFT<\/span> <span style=\"color: gray;\">OUTER<\/span> <span style=\"color: gray;\">JOIN<\/span> Person<span style=\"color: gray;\">.<\/span><span style=\"color: blue;\">Address<\/span> <span style=\"color: blue;\">AS<\/span> pa<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> so<span style=\"color: gray;\">.<\/span>ShipToAddressID <span style=\"color: gray;\">=<\/span> pa<span style=\"color: gray;\">.<\/span>AddressID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">LEFT<\/span> <span style=\"color: gray;\">OUTER<\/span> <span style=\"color: gray;\">JOIN<\/span> Person<span style=\"color: gray;\">.<\/span>StateProvince <span style=\"color: blue;\">AS<\/span> sp<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> sp<span style=\"color: gray;\">.<\/span>StateProvinceID <span style=\"color: gray;\">=<\/span> pa<span style=\"color: gray;\">.<\/span>StateProvinceID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">ORDER<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> <span style=\"color: blue;\">BY<\/span> sp<span style=\"color: gray;\">.<\/span>StateProvinceCode<span style=\"color: gray;\">,<\/span> so<span style=\"color: gray;\">.<\/span>OrderDate<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0<\/span><\/p><\/blockquote>\n<p><span style=\"font-family: Arial; font-size: small;\">And it returns a drastically different result set of 20,986 rows. What\u2019s happened here? <\/span><\/p>\n<p><span style=\"font-family: Arial; font-size: small;\">In this case, we\u2019ve preserved ALL customer rows and then found the matching rows for ONLY those customers where TerritoryID = 1. To show you the numbers, it breaks down like this: <\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 10pt;\"><span style=\"font-family: Arial; font-size: small;\">There are 19,820 customers \u2013 of which, 3,520 are in TerritoryID 1. Of these 3,520 customers in TerritoryID 1, all but 92 have placed orders \u2013 for a total of 4,594 orders. Bringing everything together: <\/span><\/p>\n<p class=\"MsoNormal\" style=\"text-indent: 0.5in; margin: 0in 0in 10pt;\"><span style=\"font-family: Arial; font-size: small;\">19,820 customers \u2013 3,520 in TerritoryID 1 = 16,300 rows <\/span><\/p>\n<p class=\"MsoNormal\" style=\"text-indent: 0.5in; margin: 0in 0in 10pt;\"><span style=\"font-family: Arial; font-size: small;\">.4,594 orders + 92 customers that haven\u2019t placed an order (remember \u2013 all customers are preserved in the outer join) = 4,686 rows <\/span><\/p>\n<p class=\"MsoNormal\" style=\"text-indent: 0.5in; margin: 0in 0in 10pt;\"><span style=\"font-family: Arial; font-size: small;\">16,300 + 4,686 = 20,986 rows <\/span><\/p>\n<p><span style=\"font-family: Arial;\">Bringing this all together and to quote fellow MVP Hugo Kornelis (<a href=\"http:\/\/sqlblog.com\/blogs\/hugo_kornelis\/\" target=\"_blank\" class=\"broken_link\">blog<\/a>) in the discussion: &#8220;<em>For OUTER joins, moving predicates does change the results. This makes performance comparisons moot. If a query gives incorrect results,\u00a0I don&#8217;t care if it&#8217;s faster or slower than one that does yield the desired results.&#8221;<\/em> <\/span><\/p>\n<p><span style=\"font-family: Arial; font-size: small;\">This is absolutely the most important point. You cannot just move a search argument \u201cup\u201d into the FROM without repercussions. In general, I recommend search arguments to be in the WHERE clause and join conditions in the FROM clause. <\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 10pt;\"><span style=\"font-family: Arial; font-size: small;\">But, to make it even better, I thought I\u2019d end with an even more simplified example of the sets described above: <\/span><\/p>\n<blockquote><p><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">SELECT<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> c<span style=\"color: gray;\">.<\/span>CustomerID<span style=\"color: gray;\">,<\/span> so<span style=\"color: gray;\">.<\/span>SalesOrderNumber<span style=\"color: gray;\">,<\/span> so<span style=\"color: gray;\">.<\/span>OrderDate<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">FROM<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> Sales<span style=\"color: gray;\">.<\/span>Customer <span style=\"color: blue;\">AS<\/span> c<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">INNER<\/span> <span style=\"color: gray;\">JOIN<\/span> Sales<span style=\"color: gray;\">.<\/span>SalesOrderHeader <span style=\"color: blue;\">AS<\/span> so<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> so<span style=\"color: gray;\">.<\/span>CustomerID <span style=\"color: gray;\">=<\/span> c<span style=\"color: gray;\">.<\/span>CustomerID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">WHERE<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> c<span style=\"color: gray;\">.<\/span>TerritoryID <span style=\"color: gray;\">=<\/span> 1 <span style=\"color: green;\">&#8212; Northwest US, 4594 rows<br \/>\n<\/span><\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">go<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">\u00a0<\/span><\/p>\n<p><span style=\"font-family: 'Lucida Console'; color: green; font-size: 12pt;\">&#8212; in this simple inner join<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">SELECT<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> c<span style=\"color: gray;\">.<\/span>CustomerID<span style=\"color: gray;\">,<\/span> so<span style=\"color: gray;\">.<\/span>SalesOrderNumber<span style=\"color: gray;\">,<\/span> so<span style=\"color: gray;\">.<\/span>OrderDate<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">FROM<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> Sales<span style=\"color: gray;\">.<\/span>Customer <span style=\"color: blue;\">AS<\/span> c<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">INNER<\/span> <span style=\"color: gray;\">JOIN<\/span> Sales<span style=\"color: gray;\">.<\/span>SalesOrderHeader <span style=\"color: blue;\">AS<\/span> so<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> so<span style=\"color: gray;\">.<\/span>CustomerID <span style=\"color: gray;\">=<\/span> c<span style=\"color: gray;\">.<\/span>CustomerID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">AND<\/span> c<span style=\"color: gray;\">.<\/span>TerritoryID <span style=\"color: gray;\">=<\/span> 1 <span style=\"color: green;\">&#8212; Northwest US, 4594 rows<br \/>\n<\/span><\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">go<\/span><\/p>\n<p><span style=\"font-family: 'Lucida Console'; color: green; font-size: 12pt;\">&#8212; What if we want ALL Customers in the Northwest of the US,<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: green; font-size: 12pt;\">&#8212; regardless of whether or not they placed an order:<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">SELECT<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> c<span style=\"color: gray;\">.<\/span>CustomerID<span style=\"color: gray;\">,<\/span> so<span style=\"color: gray;\">.<\/span>SalesOrderNumber<span style=\"color: gray;\">,<\/span> so<span style=\"color: gray;\">.<\/span>OrderDate<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">FROM<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> Sales<span style=\"color: gray;\">.<\/span>Customer <span style=\"color: blue;\">AS<\/span> c<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">LEFT<\/span> <span style=\"color: gray;\">OUTER<\/span> <span style=\"color: gray;\">JOIN<\/span> Sales<span style=\"color: gray;\">.<\/span>SalesOrderHeader <span style=\"color: blue;\">AS<\/span> so<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> so<span style=\"color: gray;\">.<\/span>CustomerID <span style=\"color: gray;\">=<\/span> c<span style=\"color: gray;\">.<\/span>CustomerID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">WHERE<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> c<span style=\"color: gray;\">.<\/span>TerritoryID <span style=\"color: gray;\">=<\/span> 1 <span style=\"color: green;\">&#8212; Northwest US, 4594 rows<br \/>\n<\/span><\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">go<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> <span style=\"color: green;\">&#8212; Northwest US, 4686 rows<\/span><\/span><span style=\"font-family: 'Lucida Console'; color: green; font-size: 12pt;\">\u00a0<\/span><\/p>\n<p><span style=\"font-family: 'Lucida Console'; color: green; font-size: 12pt;\">&#8212; So, now what if we thought that the performance could be<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: green; font-size: 12pt;\">&#8212; better if we pushed this up into the FROM clause&#8230;<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">SELECT<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> c<span style=\"color: gray;\">.<\/span>CustomerID<span style=\"color: gray;\">,<\/span> so<span style=\"color: gray;\">.<\/span>SalesOrderNumber<span style=\"color: gray;\">,<\/span> so<span style=\"color: gray;\">.<\/span>OrderDate<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">FROM<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\"> Sales<span style=\"color: gray;\">.<\/span>Customer <span style=\"color: blue;\">AS<\/span> c<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">LEFT<\/span> <span style=\"color: gray;\">OUTER<\/span> <span style=\"color: gray;\">JOIN<\/span> Sales<span style=\"color: gray;\">.<\/span>SalesOrderHeader <span style=\"color: blue;\">AS<\/span> so<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: blue;\">ON<\/span> so<span style=\"color: gray;\">.<\/span>CustomerID <span style=\"color: gray;\">=<\/span> c<span style=\"color: gray;\">.<\/span>CustomerID<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; font-size: 12pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: gray;\">AND<\/span> c<span style=\"color: gray;\">.<\/span>TerritoryID <span style=\"color: gray;\">=<\/span> 1 <span style=\"color: green;\">&#8212; 20986 rows<\/span><\/span><\/p>\n<p><span style=\"font-family: 'Lucida Console'; color: green; font-size: 12pt;\">&#8212; Absolutely ALL customers, regardless of whether or not they<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: green; font-size: 12pt;\">&#8212; placed an order. For the Northwest territory we will see their<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: green; font-size: 12pt;\">&#8212; order information but for all other customers (even those that<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: green; font-size: 12pt;\">&#8212; have placed an order), we will NOT see their order information.<br \/>\n<\/span><span style=\"font-family: 'Lucida Console'; color: blue; font-size: 12pt;\">go<\/span><span style=\"font-family: Arial; font-size: small;\">\u00a0 <\/span><\/p><\/blockquote>\n<p><span style=\"line-height: 120%; font-family: 'Arial','sans-serif'; font-size: 10pt;\">What&#8217;s probably most important is for you to thoroughly review the output of the last result set (of 20,986 rows). While I&#8217;ve explained the number &#8211; do you understand the results? Out of the 3,520 customers in TerritoryID 1 &#8211; 3,428 of them create 4,594 sales. For ONLY\u00a0these sales, we see the SalesOrderNumber and SalesOrderDate. For all other customers &#8211; even those that have placed orders &#8211; we see NULLs. Why? Because moving this search argument up into the FROM has defined the criteria for the INNER join *BEFORE* the outer join is performed. This is why we see NULLs for those that have placed orders. If you order the results by TerritoryID this may help you better visualize what&#8217;s happening!<\/span><\/p>\n<p><span style=\"line-height: 120%; font-family: 'Arial','sans-serif'; font-size: 10pt;\">Thanks for reading and thanks for the comments and\u00a0discussions&#8230; I love the SQL Community for its never ending passion!<\/span><\/p>\n<p><span style=\"line-height: 120%; font-family: 'Arial','sans-serif'; font-size: 10pt;\">Cheers,<br \/>\nKimberly<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a follow-on discussion to my\u00a0SQL Server Magazine Q&amp;A titled \u201cDetermining the position of search arguments in a join\u201d where I was responding to a reader question about whether or not search arguments could be moved up into the FROM clause instead of placed in the WHERE clause and whether or not it would improve [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40,49,69],"tags":[],"class_list":["post-501","post","type-post","status-publish","format-standard","hentry","category-joins","category-opinions","category-sql-server-magazine"],"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts\/501","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/comments?post=501"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts\/501\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/media?parent=501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/categories?post=501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/tags?post=501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}