Friday, August 1, 2014

SQL JOINS

              You have following tables 



SalespersonCustomer
IDNameAgeSalary
1Abe61140000
2Bob3444000
5Chris3440000
7Dan4152000
8Ken57115000
11Joe3838000
IDNameCityIndustry Type
4SamsonicpleasantJ
6PanasungoaktownJ
7SamonyjacksonB
9OrangeJacksonB
Orders
Numberorder_datecust_idsalesperson_idAmount
108/2/9642540
201/30/99481800
307/14/9591460
401/29/98722400
502/3/9867600
603/2/9867720
705/6/9897150
Given the tables above, find the following:

a. The names of all salespeople that have an order with Samsonic. 
solution:
SELECT distinct name FROM `salesperson` WHERE id in(select salesperson_id from customer,orders where id=cust_id AND name='Samsonic');
if you write  above query like:

SELECT distinct name FROM `salesperson` WHERE id=(select salesperson_id from customer,orders where id=cust_id AND name='Samsonic');
It is invalid query because subquery returning multiple records so it can't be compared. 



No comments:

Post a Comment