Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

using old-style left JOINs in SQL

Posted on 2014-11-01
9
219 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

837 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