Solved

Joins: Finding records with no matches in another table

Posted on 2007-11-30
5
195 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
[X]
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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

696 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