SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Linking two tables to find non orders
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

deanglen
Yak Posting Veteran

64 Posts

Posted - 03/14/2013 :  06:57:18  Show Profile  Reply with Quote
Hi

I have two tables. Customer and Orders.

Both are linked by CustomerID

If I wanted to find out customer details from the customer table to find out who has not placed an order do you know what the SQL Script would be like? Trying to link but no luck so far.

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 03/14/2013 :  07:05:15  Show Profile  Reply with Quote
SELECT custid FROM customers
EXCEPT
SELECT custid FROM Orders

--
Chandu
Go to Top of Page

ahmeds08
Aged Yak Warrior

India
656 Posts

Posted - 03/14/2013 :  07:51:51  Show Profile  Send ahmeds08 a Yahoo! Message  Reply with Quote
and also...........

select c.customername from customer c
inner join orders o
on c.custid=o.custid
where c.custid not in(select custid from orders)
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 03/14/2013 :  07:59:08  Show Profile  Reply with Quote
--One more alternate
By this approach you can select all columns in both tables
SELECT c.*
FROM customers c
LEFT JOIN Orders o ON o.custid = c.custid
WHERE o.custid IS NULL

Note: As per ahmeds08's solution, If you have NULL value for custid in Orders Table, then it won't give any single record (even we have non-order customers)......
For that solution is NOT IN ( SELECT Custid FROM Orders WHERE custid IS NOT NULL)

--
Chandu

Edited by - bandi on 03/14/2013 08:00:10
Go to Top of Page

ahmeds08
Aged Yak Warrior

India
656 Posts

Posted - 03/14/2013 :  08:04:56  Show Profile  Send ahmeds08 a Yahoo! Message  Reply with Quote
quote:
Originally posted by bandi

--One more alternate
By this approach you can select all columns in both tables
SELECT c.*
FROM customers c
LEFT JOIN Orders o ON o.custid = c.custid
WHERE o.custid IS NULL

Note: As per ahmeds08's solution, If you have NULL value for custid in Orders Table, then it won't give any single record (even we have non-order customers)......
For that solution is NOT IN ( SELECT Custid FROM Orders WHERE custid IS NOT NULL)

--
Chandu



Thanks chandu.
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 03/14/2013 :  08:06:53  Show Profile  Reply with Quote
quote:
Originally posted by ahmeds08

quote:
Originally posted by bandi

--One more alternate
By this approach you can select all columns in both tables
SELECT c.*
FROM customers c
LEFT JOIN Orders o ON o.custid = c.custid
WHERE o.custid IS NULL

Note: As per ahmeds08's solution, If you have NULL value for custid in Orders Table, then it won't give any single record (even we have non-order customers)......
For that solution is NOT IN ( SELECT Custid FROM Orders WHERE custid IS NOT NULL)

--
Chandu


Thanks chandu.

Welcome......

--
Chandu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000