views:

70

answers:

2

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The table Order contains a primary key of OrderID and a foreign key of CustomerID.

+3  A: 

Something like

Select yourcustomerfields, yourorderfields
From Customer
Left join Orders on Customer.OrderID = Orders.OrderID
Andrew
A: 

I came up with this solutions.

Select CustomerName
from Customer
Where pk_CustomerID IN
(
Select fk_CustomerID from Orders
INNER JOIN Customer
on Customer.pk_CustomerID=Orders.fk_CustomerID)

/* NOT IN instead of IN will give the other customers who doesn't have an Order */

jero
@jero: I am sorry but those `pk_` and `fk_` prefixes gotta go.
Sung Meister