Bloomberg LP Interview Question
Software Engineer / DevelopersTeam: Supply Chain
Country: United States
Interview Type: In-Person
Best way to find this information is using left join
Select Cust_Name from
Customers C
LEft Join Orders O On C.Cust_id = O.Cust_id
Where O.Cust_id is null
this doesn't make sense to me, your foreign key O.Cust_id should never be null, that's violating referential integrity, furthermore, C.Cust_id should also never be null if it's the primary key of the customer table
Left join returns null on the joined table where there are no matches
SELECT Cust_Name, Order_Name
FROM Customers C
LEFT JOIN Orders O ON C.Cust_id = O.Cust_id
WHERE Order_Name is null
Please remember O.Cust_id is foreign key in Orders table and foreign key can be nulls too. In this relation with left outer join it we are not checking null primary keys of customer table rather we are checking in customers who don't have records in orders table which is fine so will return null values..
Select cust_name from Customers a where (Select count(*) from Orders b
where a.cust_id=b.cust_id)=0;
The inner query will calculate the number of orders for a particular cust_id....
An output of 0 will indicate that the customer never ordered anything..
SELECT Cust_name from Customers
- Hank Readen December 07, 2014where
Cust_id NOT in (Select Cust_id from Orders)
--Assuming cust_id is primary key in customers and foreign key in Orders