Sunteți pe pagina 1din 3

1. Which cust (id's) ordered something in March 1993 or April 1993 but not both?

type 11: select cust_id from sales_order where (order_date like '1993-03%' or order_date like '1993-04%') and not cust_id in ( select s1.cust_id from sales_order s1, sales_order s2 where s1.cust_id = s2.cust_id and s1.order_date like '1993-03%' and s2.order_date like '1993-04%' ) Also (with Sybase 8.0.2 dates): SELECT cust_id, order_date FROM sales_order WHERE (order_date BETWEEN '2000-03-01' AND '2000-03-31' OR order_date BETWEEN '2000-04-01' AND '2000-04-30') AND NOT cust_id IN ( SELECT s1.cust_id FROM sales_order s1, sales_order s2 WHERE s1.cust_id = s2.cust_id AND s1.order_date BETWEEN '2000-03-01' AND '2000-03-31' AND s2.order_date BETWEEN '2000-04-01' AND '2000-04-30' ) result: 102 104 115 138 169 ... 2.) What products (id) were in the first shipment? Type 12 select distinct prod_id from sales_order_items where ship_date IN ( select MIN(ship_date) from sales_order_items ) result: 300 301 302 3.) Who was the last customer (id) to order something?

type 12 SELECT cust_id FROM sales_order WHERE order_date IN (SELECT MAX(order_date) FROM sales_order) result 202 4.) What products (id) comes in only size large and no other? select id from product where size = 'large' and id not in ( select id from product where size != 'large' ) result: 600 601 This query will work, but is more complex than necessary. You would lose points. Since a product with a specific id can only come in one size it doesnt really make sense to ask if it only comes in large. If it is large, it cant possibly be any other size. SELECT id FROM product WHERE size = 'large' will do just as well. It would make sense if we were looking for a product name since a product with a particular name can come in multiple sizes. SELECT name FROM product WHERE size = 'large' AND name not in ( SELECT name FROM product WHERE size != 'large' ) Also: (Using a correlated subquery) SELECT name FROM product p1 WHERE size = 'medium' AND NOT EXISTS ( SELECT name

FROM product p2 WHERE p1.name = p2.name AND size != 'medium' ) Make sure you can answer this type of question with both a correlated and a non-correlated subquery. Note: Size large isnt a very good test given the data in the Sybase dataset. No product comes in both large and another size. It would be better to check your query with medium since teeshirts come on both small and medium but shorts come only in medium. 5.) Which products(names) come in either black or white but not both? type 11 select name from product p0 where (color = 'black' or color = 'white') and not name in ( select p1.name from product p1, product p2 where p1.name = p2.name and p1.color = 'black' and p2.color = 'white' ) result: shorts The p0 alias is not necessary.

S-ar putea să vă placă și