0

I am new to stackoverflow, I recently went through an interview as graduate developer, and I was asked a question regarding SQL.

Given two table customers and orders, how to find all cusomers who didn't place an order. Can someone tell me what would be a query to achieve this?

--Forgot to mention that I was asked to use JOIN operator for this.

EDIT: I could not find answer to this question as this was technical as well as logical. Therefore, I think my question is different from the one identified as a duplicate.

Community
  • 1
  • 1
user5011580
  • 21
  • 1
  • 7
  • possible duplicate of [Difference between INNER and OUTER joins](http://stackoverflow.com/questions/38549/difference-between-inner-and-outer-joins) – Jupotter Jun 15 '15 at 14:48
  • This is a basic query which might need to modify according to the table structure. I would recommend you to go to http://www.w3schools.com/ for some tutorials which would be helpful to you. – hpatel Jun 15 '15 at 14:53

2 Answers2

4
SELECT  c.CustomerId
        ,Name
FROM    dbo.Customers c 
            LEFT JOIN dbo.Orders o ON o.CustomerId = c.CustomerId
WHERE   OrderId IS NULL
hpatel
  • 199
  • 8
2

This translates into a Correlated Subquery using EXISTS:

select * 
from customers as c
where not exists
 ( select * from orders as o
   where c.customer_id = o.customer_id)

No order exists for a given customer_id

dnoeth
  • 59,503
  • 4
  • 39
  • 56