Solved

using old-style left JOINs in SQL

Posted on 2014-11-01
9
217 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Return 0 on SQL count 24 30
SQL view 2 27
ms sql + get number in list out of total 7 29
Correct an issue with a where clause with calculation 2 31
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

777 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