Вы находитесь на странице: 1из 3

1. Which orders were placed on March 5, 1994 for exactly one item?

SELECT id FROM sales_order GROUP by id HAVING COUNT (*) = 1 AND MAX ( order_date) = '1994-03-05' Answers: 2546, 2547, 2548 This is a type 10 query. Instructors Comments: This query gives incorrect answers. SELECT * FROM sales_order_items WHERE id = 2546 Shows that 2546 has two items. The problem is that this query looks only at the Sales_Order table, which contains only one record for each order regardless of the number of items on the order. The Sales_Order_Items table contains on record for each item. We still need the Sales_Order table because it contains the order_date, but we need Sales_order_items as well. So we join the tables and then count the rows. SELECT SO.id FROM sales_order SO, sales_order_items SOI WHERE SO.id = SOI.id GROUP by SO.id HAVING COUNT (*) = 1 AND MAX ( order_date) = '1994-03-05' Which orders are for product 501 and no other product? SELECT id FROM sales_order_items WHERE prod_id = 501 AND id NOT IN ( SELECT id FROM sales_order_items WHERE prod_id != 501 )

Answers: 2012, 2025, 2032, 2035,2045 This is a type 10 query. Looks Good! 3. Which customers have placed an order on March 5,1994 and no other dates? SELECT cust_id FROM sales_order S1 WHERE order_date = '1994-03-05' and NOT EXISTS (SELECT cust_id FROM sales_order S2 WHERE S1.id = S2.id AND order_date != '1994-03-05') Answer: 158, 146, 157 This is a type 10 query. Instructors comments: These answers arent correct! If you check them with SELECT * FROM sales_order S1 WHERE cust_id = 158 Youll see that customer 158 placed 4 orders, only one of which was on the specified date. The problem here is that the query and the subquery both ask the same question. The query finds an order placed on 3/5/94. The NOT EXISTS specifies that there is no order with the same id (the same order since S1.id = S2.id) that wasnt placed on 3/5/94. The double negative arises from NOT EXISTS and !=. Rather than checking for the same order (S1.id = S2.id) we want to check for the same customer. 4. Which orders are for either product 501 or 502 but not both items? SELECT id FROM sales_order WHERE (prod_id = 501 OR prod_id = 502) AND NOT id in

( SELECT S1.id FROM sales_order S1, sales_order S2 WHERE S1.id = S2.id AND S1.prod_id = 501 AND S2.prod_id = 502) Answer: 0 This is a type 11 query. Instructors Comments: I sure hope this is a typo this query doesnt even run. Prod_id isnt in the Sales_order table. Sales_order should be sales_order_items throughout. 5. Which manager(s) have been with the company for the longest period of time? SELECT manager_id FROM employee WHERE start_date IN (SELECT MIN (start_date) FROM employee) Answer: 501 This is a type 12 query. Instructors Comments: Again this answer isnt correct! SELECT * FROM employee WHERE emp_id = 501 Shows that employee 501 started on 1987-08-04 SELECT MIN (start_date) FROM employee Shows that the oldest start date is 1984-08-28 This query asks Who manages the employee who has been with us the longest?

Вам также может понравиться