Solved

Joins: Finding records with no matches in another table

Posted on 2007-11-30
5
187 Views
Last Modified: 2010-03-20
Table 1: clients
Table 2: orders

order to clients is a 1 to 1 relationship
clients to orders is a 1 to many (in some cases Zero)

I'm looking for a SQL statement that will give me a list of customers that have no orders.
0
Comment
Question by:djlurch
  • 2
  • 2
5 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 250 total points
ID: 20383835
select * from Clients c where not exists(select 1 from Orders where Orders.ClientID = c.ClientID )
0
 
LVL 16

Expert Comment

by:Rick_Rickards
ID: 20383866
SELECT Client.ClientID, Client.ClientName
FROM Client LEFT JOIN Orders ON Client.ClientID = Orders.ClientID
WHERE ((Orders.OrdersID) Is Null);
0
 
LVL 1

Author Comment

by:djlurch
ID: 20383954
Wow. I've been looking for that answer for a few years and never thought about the Is Null and didn't know about Not Exists.

Both work. I am giving points to aneesh since he answered with the first correct answer. I did try Rick's answer and it works. It is also a bit more traditional and possibly intuitive.

You guys are awesome. Thanks!
0
 
LVL 16

Expert Comment

by:Rick_Rickards
ID: 20384081
No argument that aneeshattingal's approach is viable (for SQL server).  In the world of Access one should be wary of using Sub Queries, access just doesn't perform well with them and can in fact take hours to do what an outer join can do in seconds.  The main advantage of the outer join approach is that it works equally well in both arenas.
0
 
LVL 1

Author Comment

by:djlurch
ID: 20384413
Interesting. Thanks for the info.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

791 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