Solved

Joins: Finding records with no matches in another table

Posted on 2007-11-30
5
159 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
Comment Utility
select * from Clients c where not exists(select 1 from Orders where Orders.ClientID = c.ClientID )
0
 
LVL 16

Expert Comment

by:Rick_Rickards
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Interesting. Thanks for the info.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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