Solved

using old-style left JOINs in SQL

Posted on 2014-11-01
9
207 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
Comment Utility
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
Comment Utility
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:ScottPletcher
ScottPletcher earned 250 total points
Comment Utility
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
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
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
Comment Utility
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:ScottPletcher
Comment Utility
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
Comment Utility
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
Comment Utility
Adopting. Not adopring.

Sorry
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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 insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now