Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need to select all rows from table1 that do not have a matching key in table 2

Posted on 2008-09-30
4
Medium Priority
?
269 Views
Last Modified: 2010-04-21
I have two tables ie Customers and Orders

I need to Select all customers for which there are no orders.

ie. Show the CustomerID for all orders where the CustomerID in the Customers table s not found in the CustomerID of the Orders table.
What is the correct SQL syntax ?
Thanks,
0
Comment
Question by:Dovberman
[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
4 Comments
 
LVL 11

Accepted Solution

by:
aaronakin earned 500 total points
ID: 22605551
SELECT *
  FROM Customers
  WHERE CustomerID NOT IN (SELECT CustomerID FROM Orders)
0
 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 500 total points
ID: 22605560
or like this

select * from customers c where not exists(select 1 from orders o where o.customerid = c.customerid)
0
 
LVL 5

Expert Comment

by:adlink_la
ID: 22605771
If I read that right, you want customers that have no Orders?
SELECT CustomerID
FROM Customers
EXCEPT
SELECT CustomerID 
FROM Orders

Open in new window

0
 

Author Closing Comment

by:Dovberman
ID: 31501551
aaronikin and chapmandew split the points. Both returned 38 rows. adlink la returned 32 rows. Thanks to all.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

618 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