Friday, June 25, 2010

SQL Query

sql tables : 
SHOP(shop_number,shop_name,shop_address,shop_owner)
CUSTOMER(cust_num,cust_name,cust_addr)
SUPPLIED(item_num,cust_num,shop_num,date,price)

How to : 
(i) find the names of customers who have been supplied items of max total price?
(ii) list the shop owners who supplied some item to the address "addr1"?

Ans : 
SELECT c.cust_num,c.cust_name,c.cust_addr,sum( S.price ) AS "total"
FROM SUPPLIED S, CUSTOMER c
WHERE C.cust_num = S.cust_num
GROUP BY c.cust_num,c.cust_name,c.cust_addr 
ORDER BY sum( S.price ) desc
LIMIT 1

No comments:

Blog Archive