Sunteți pe pagina 1din 3

Type 10 Which employees are from Massachusetts?

Looks like a type 1 question to me SELECT emp_id, emp_fname, emp_lname FROM employee WHERE state = 'MA' Results Emp_id 102 105 148 160 184 emp_fname emp_lname Fran Whitney Matthew Cobb Julie Jordan Robert Breault Melissa Espinoza

Type 11 Which orders were placed on 1994-04-02 or 1993-03-20 but not both? This question is nonsense! An order can only have one order-date! Even if the question made sense, the subquery makes no sense at all. It should have eliminated all rows from the answer! The only reason you got an answer is that you messed up the AND/OR clause. See the answer to the next question for further explanation Select id from Sales_order where (order_date= '1994-04-02') or (order_date='1993-03-20') and Not id IN ( select s.id from sales_order s, sales_order S1 where (s.id=s1.id) ); Results 2008 2017 2079 Type 11 Show all orders shipped on either 1993-03-16 or 1994-04-08 but not both. This question at least makes sense. An order can have more than one ship date.

Select id from sales_order_items where(ship_date= '1993-03-16')or (ship_date = '1994-04-08') and Not id IN ( select s.id from sales_order_items s, sales_order_items S1 where (s.id=s1.id) ); Your subquery however is still ridiculous. When you cross a table with itself you will always have the same id in both tables (s and s1). It should have eliminated all rows from the answer. Again, the only reason you got an answer is that you messed up the OR/AND part of the WHERE clause. Your WHERE clause asks for orders where (the ship date was 1993-03-16) OR (the ship date was 1994-04-08 AND the id is not in a list of all the ids). Since every id is in the list of all ids, your query is equivalent to: SELECT id FROM sales_order_items WHERE (ship_date = '1993-03-16') The correct query is (with test dates for the Sybase Version 8.0.2. Sample Database) SELECT id, ship_date FROM sales_order_items WHERE (ship_date = '2000-03-16' OR ship_date ='2000-04-08') AND NOT id IN ( SELECT S.id FROM sales_order_items S, sales_order_items S1 WHERE S.id = S1.id AND S.ship_date = '2000-03-16' AND S1.ship_date = '2000-04-08' ) Results 2001 2001 2115 2115 The correct results are below. Your results failed to show any orders shipped on 4/8. You should have easily detected this mistake with a simple query to show all the orders on 4/8 when checking your answers.

2001, 2001, 2109, 2115, 2115, 2199, 2199, 2199,

'2000-03-16' '2000-03-16' '2000-04-08' '2000-03-16' '2000-03-16' '2000-04-08' '2000-04-08' '2000-04-08'

Type 12 What is the newest order date? SELECT order_date FROM sales_order WHERE order_date IN (SELECT MAX(order_date) FROM sales_order) This query does report the most recent order date. However it is needlessly complex. You would lose points. You can get the most recent (largest) order date simply from the subquery. SELECT MAX(order_date) FROM sales_order Result 1994-07-03 You only need the main query if you want to know something, such as the order id and the customer id, for the order(s) on that date. Then you will need a query like: SELECT id, cust_id FROM sales_order WHERE order_date IN (SELECT MAX(order_date) FROM sales_order) id 2105 cust_id 202

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