Solved

using old-style left JOINs in SQL

Posted on 2014-11-01
9
223 Views
Last Modified: 2014-11-25
How does sql old syntax works when there is a third table called "Buyers" which I want to left join by OrderID and BuyerID?

SELECT o.OrderID, od.ProductID
  FROM dbo.Orders AS o, dbo.OrderDetails AS od
  WHERE o.OrderDate >= '20091001'
  AND o.OrderID = od.ProductID
left join Buyers on o.OrderID = BuyerID <<< ?
0
Comment
Question by:VBdotnet2005
  • 5
  • 3
9 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 250 total points
ID: 40417884
You probably can't go that far back (i.e. using =* or *= ) as most installations have that option turned off

The safer bet is to change the old fashioned inner join into an explicit inner join
SELECT
        O.OrderID
      , OD.ProductID
FROM dbo.Orders AS O
  INNER JOIN dbo.OrderDetails AS OD ON O.OrderID = OD.ProductID
  LEFT JOIN Buyers AS B ON O.OrderID = B.BuyerID
WHERE O.OrderDate >= '20091001'

Open in new window

0
 
LVL 10

Expert Comment

by:HuaMinChen
ID: 40418087
Try
SELECT o.OrderID, od.ProductID
  FROM dbo.Orders AS o inner join dbo.OrderDetails AS od
  on o.OrderDate >= '20091001'
  AND o.OrderID = od.ProductID
left join Buyers on o.OrderID = BuyerID 

Open in new window

0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 250 total points
ID: 40419843
I don't think you have to convert the existing joins, just put the LEFT JOIN in its proper location, after the FROM clause and before the WHERE:

SELECT o.OrderID, od.ProductID, b.<column_name>
FROM dbo.Orders AS o, dbo.OrderDetails AS od
LEFT JOIN Buyers b on o.OrderID = b.BuyerID
WHERE o.OrderDate >= '20091001'
     AND o.OrderID = od.ProductID
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 48

Expert Comment

by:PortletPaul
ID: 40420746
I do not recommend mixing older and newer join syntax styles.

In a simple query you can get away with it, in more complex queries it can produce faults or errors.

Far better in my view, to adopt both the INNER JOIN and LEFT JOIN so the syntax style is consistent.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40421819
In theory, yes.  But if the existing code has a lot of inner joins that are done in the WHERE, you could easily introduce errors trying to switch to standard "INNER JOIN" syntax, esp. if you can't spend a lot of time on the changes.

New joins should always be done using the new syntax, esp. OUTER joins, as they can be ambiguous under the old syntax.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40422482
Not just in theory. The oft forgotten factor is sequence, which is vital in the newer syntax but very lax in the older syntax.

It is safer to adjust to newer syntax than to maintain awful hybrids.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40422678
All the more reason to rewrite only if you have the time to do it properly and test it fully.  Otherwise I say use the hybrid.  It may seem ugly to some, but it's vastly better than wrong results!
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40422742
There is a change as already, that requires testing. Substantially better to do this together,.

For this small and simple query there simply is no complexity or risk at adopring full new syntax.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40422753
Adopting. Not adopring.

Sorry
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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

679 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