Home Forums SQL Server 2012 SQL 2012 - General LEFT JOIN without predicate , still works but what is that?<!-- 864 --><!-- 864 --> RE: LEFT JOIN without predicate , still works but what is that?<!-- 864 --><!-- 864 --><!-- 864 -->

  • Here's something I wrote some time ago (inspired by something Jack Corbett wrote years ago) which explains why you have two joins followed by two ON clauses:

    The order of tables in the FROM clause is usually irrelevant because SQL Server will join the tables in whatever order results in the lowest-cost plan. However, the order of ON clauses matters...and can be manipulated to do some funky stuff.

    “You have a query which joins two tables, say Customers and Orders. It’s an outer join because you want all customers whether or not they’ve ever placed an order – but the order can’t be empty, it must actually have some lines.”

    Create a customer table with three customers, Peter, Simon and Chris

    CREATE TABLE #Customers (CustomerID INT IDENTITY(1,1), CustomerName VARCHAR(20))

    INSERT INTO #Customers (CustomerName) VALUES ('Peter'), ('Simon'), ('Chris')

    Three orders for Peter, two orders for Simon and none for Chris (boo hoo)

    CREATE TABLE #Orders (OrderID INT IDENTITY(1,1), CustomerID INT)

    INSERT INTO #Orders (CustomerID) VALUES (1),(1),(1),(2),(2)

    Only one of those orders has any items on it – Peter’s first order

    CREATE TABLE #Orderlines (OrderlineID INT IDENTITY(1,1), OrderID INT, PartName VARCHAR(20))

    INSERT INTO #Orderlines (OrderID, PartName) VALUES (1, 'Peter01'), (1, 'Peter02')

    Then you write the obvious query:

    -- Query 1

    SELECT c.*, o.*, ol.*

    FROM #Customers c

    LEFT JOIN #Orders o ON o.CustomerID = c.CustomerID

    INNER JOIN #Orderlines ol ON ol.OrderID = o.OrderID

    The result set isn’t what you might expect – there are only two rows, corresponding to Peter’s two items. The result set looks as if SQL Server has changed the outer join to an inner join and the execution plan confirms it. Most TSQL coders know this and will usually begin testing a query where both child tables are outer joined:

    -- Query 2

    SELECT c.*, o.*, ol.*

    FROM #Customers c

    LEFT JOIN #Orders o ON o.CustomerID = c.CustomerID

    LEFT JOIN #Orderlines ol ON ol.OrderID = o.OrderID

    -Which returns too many rows and is tricky to filter. So they switch to this:

    -- Query 3

    SELECT c.*, o.*

    FROM #Customers c

    LEFT JOIN (

    SELECT o.*, ol.OrderlineID, ol.PartName

    FROM #Orders o

    INNER JOIN #Orderlines ol ON ol.OrderID = o.OrderID

    ) o

    ON o.CustomerID = c.CustomerID

    -Which generates the correct result set as you would expect, and the execution plan confirms an inner join and an outer join.

    You could also bracket your joins, but it’s so counterintuitive and tricky to maintain that I’m not even going to provide an example. If you must have a look, use Google – then forget what you’ve seen. There is another way, and that is to change the order of the ON clauses, so that Orders and Orderlines are inner joined before the product is joined to the Customer table:

    -- Query 4

    SELECT c.*, o.*, ol.*

    FROM #Customers c

    LEFT JOIN #Orders o

    INNER JOIN #Orderlines ol

    ON ol.OrderID = o.OrderID

    ON o.CustomerID = c.CustomerID

    This also generates the correct result set, confirmed by the execution plan. It’s easy to see what’s going on and the plan is only trivially different from query 3 – it’s slightly cheaper.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden