Solved

Joins: Finding records with no matches in another table

Posted on 2007-11-30
5
181 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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