1. [10pts] Return the end- to- end (i.e., start to finish) processing time of the last order added (the one with id:21). SELECT TIMEDIFF(O2.order_status_datetime, O1.order_status_datetime) AS duration FROM Orders_track_status O1, Orders_track_status O2 WHERE O1.oid=21 AND O2.oid=21 AND O1.order_status_code=1 AND O2.order_status_code=4; duration NULL 2. [10pts] Return the restaurant id and the name of each restaurant along with the number of dishes that it serves. SELECT r.id, r.name, COUNT(*) FROM Restaurants r, Dishes d WHERE r.rid = d.rid GROUP BY d.rid; rid name COUNT(*) 1 Pat Urban Seoul 3 2 Dan Urban Plates 3 3 Ken Fish Grill 3 4 Ric Miyabi Shabu Shabu Grill 3 5 Sar Tang 190 3 6 Ann Falasophy 3 7 Nat Sandwich Plus 3 8 Rom Del Sushi 3 9 Kri Dhaba 3 10 Nor Kingchops 3 3. [10pts] For those orders where a customer ordered at least three different dishes in a single order transaction, return the order id, the name of each dish, and the cost associated with that dish (i.e., price * quantity) in the order. SELECT o.oid, oc.name, SUM(d.price * quantity) AS dishcost FROM Orders_Contain_Dishes OC, Orders O, Dishes D WHERE d.rid = oc.rid AND d.name = oc.name AND oc.oid = o.oid GROUP BY oid, name HAVING oid IN (SELECT oid FROM Orders_Contain_Dishes GROUP BY oid HAVING COUNT(*) > 2); oid name dishcost 4 fresh lemonade 25.98 4 sandwich 33.98 1
4 the thai wrap 26 8 hummus 139.9 8 the burger combo 164.95 8 the karma burger 54 12 salmon 260 12 skewered shrimp 219.8 12 swordfish 230 4. [10pts] Find the ids and ratings of restaurants whose average rating is greater than the overall average rating (i.e., for above average restaurants). SELECT rid, AVG(rating) FROM Customers_review GROUP BY rid HAVING AVG(rating) >= (SELECT AVG(rating) FROM Customers_review); Rid avgrating 2 3.1667 4 5.0000 5 5.0000 7 5.0000 8 4.0000 10 4.0000 5. [15pts] Return the customer nicknames along with order ids and total amounts for all orders placed by customers. Include all customers in the result, even customers that have placed no orders. Sort the final result by customer nickname. SELECT c.nickname, o.oid, o.total_amount FROM Customers c LEFT JOIN Orders o ON c.cid = o.cid ORDER BY c.nickname; nickname oid total_amount Alien Leader 3 96.5 Alien Leader 6 62.5 Blimey Billy Blackstroker NULL NULL Bloody Lena Stoker NULL NULL Brutus Pale Klek 19 32.5 Dana Hawkins 9 59.95 El Agent NULL NULL Elastic Mustard NULL NULL Forsaken Captain 1 22.5 Forsaken Captain 4 85.96 Forsaken Captain 11 173.94 Forsaken Captain 12 709.8 2
Forsaken Captain 13 1199.5 Forsaken Captain 14 569.88 Forsaken Captain 15 129.9 Forsaken Captain 16 41.97 Forsaken Captain 17 315 Forsaken Captain 18 2913 Forsaken Captain 20 210 Gargoyle Bad 2 115 Gargoyle Bad 5 153.97 Gold Jacob Silverbeard NULL NULL Gold Kala Fargloom 10 1379.7 Heavy Major 7 22.5 Joe Harker NULL NULL Mars Lieutenant NULL NULL Nana Deadwood 8 358.85 Poseidon Arrow NULL NULL Stony Prince NULL NULL The Brave Boy NULL NULL The Gladiator NULL NULL The Oyster NULL NULL 6. [15pts] Return the restaurant ids, restaurant names, reviewer names, and ratings that each restaurant has received. The id and the name of each restaurant should be returned even if no customer has rated that restaurant yet. Sort the results by restaurant name. SELECT r.rid, r.name AS restaurant_name, u.name AS customer_name, cr.rating FROM Restaurants r LEFT JOIN Customers_review cr ON r.rid = cr.rid LEFT JOIN Users u ON cr.cid = u.id ORDER BY r.name; rid restaurant_name customer_name rating 6 Ann Falasophy Joyce J. Rayo 1 6 Ann Falasophy Timothy A. German 2 2 Dan Urban Plates Patrick M. Tyler 1 2 Dan Urban Plates Kenneth B. Knight 4 2 Dan Urban Plates Joyce J. Rayo 2 2 Dan Urban Plates Sophia D. Williams 3 2 Dan Urban Plates Michael R. Deane 4 2 Dan Urban Plates Raul A. Dicarlo 5 3 Ken Fish Grill Patrick M. Tyler 3 3 Ken Fish Grill Danny C. Malveaux 1 3 Ken Fish Grill Kenneth B. Knight 1 3
9 Kri Dhaba NULL NULL 7 Nat Sandwich Plus Sophia D. Williams 5 10 Nor Kingchops Raul A. Dicarlo 4 1 Pat Urban Seoul Patrick M. Tyler 4 1 Pat Urban Seoul Anne M. Gurley 1 4 Ric Miyabi Shabu Shabu Grill James K. McGaha 5 8 Rom Del Sushi Michael R. Deane 4 5 Sar Tang 190 Patrick M. Tyler 5 5 Sar Tang 190 Raul A. Dicarlo 5 7. [15pts] Return the restaurant ids, restaurant names, number of ratings, lowest rating, highest rating, and average rating that each restaurant has received. The id and the name of each restaurant should be returned even if there are no ratings yet from a customer for that restaurant. Sort the results by the restaurant name. SELECT r.rid, r.name, COUNT(cr.rating), min(cr.rating), max(cr.rating), AVG(cr.rating) FROM Restaurants r LEFT JOIN Customers_review cr ON r.rid = cr.rid GROUP BY r.rid ORDER BY r.name; rid name COUNT(cr.rating) min(cr.rating) max(cr.rating) AVG(cr.rating) 6 Ann Falasophy 2 1 2 1.5 2 Dan Urban Plates 6 1 5 3.1667 3 Ken Fish Grill 3 1 3 1.6667 9 Kri Dhaba 0 NULL NULL NULL 7 Nat Sandwich Plus 1 5 5 5 10 Nor Kingchops 1 4 4 4 1 Pat Urban Seoul 2 1 4 2.5 Ric Miyabi Shabu Shabu 4 Grill 1 5 5 5 8 Rom Del Sushi 1 4 4 4 5 Sar Tang 190 2 5 5 5 8. [15pts] Show whether each user is a driver, a customer, or both by presenting their id, name, and two additional labeled fields. In the first labeled field, if a given user is a customer, put Customer. If not, leave it NULL. In the second labeled field, if the user is a driver, put Driver. If not, leave it NULL. (E.g., an output row might look like this: 1, Patrick M. Tyler, Customer, Driver.) Hint: In order to do this last query, you may want to have a look at the CASE expression in MySQL s online documentation. SELECT id, name, (CASE WHEN cid IS NOT NULL THEN "Customer" ELSE NULL END) AS ISCustomer, (CASE WHEN did IS NOT NULL THEN "Driver" ELSE NULL END) AS ISDriver FROM Users U LEFT JOIN Customers C ON U.id=C.cid LEFT JOIN Drivers D ON U.id=D.did; 4
id name ISCustomer ISDriver 1 Patrick M. Tyler Customer Driver 2 Danny C. Malveaux Customer Driver 3 Kenneth B. Knight Customer Driver 4 Richard A. Delvalle Customer Driver 5 Gladys B. Hopper Customer Driver 6 Sara T. Wilson Customer Driver 7 Charles M. Gunter Customer Driver 8 Michelle L. Nye Customer Driver 9 Joseph A. Obrien Customer Driver 10 James M. Schwan Customer Driver 11 Kristy H. Hilliard NULL Driver 12 Joyce J. Rayo Customer NULL 13 Roy T. Herbert NULL Driver 14 Sophia D. Williams Customer NULL 15 Timothy M. Wilhelm NULL Driver 16 Michael R. Deane Customer NULL 17 Guadalupe C. Necaise NULL Driver 18 Raul A. Dicarlo Customer NULL 19 Kendrick S. Craig NULL Driver 20 Timothy A. German Customer NULL 21 Kathleen T. Shelly NULL Driver 22 Joel A. Lozano Customer NULL 23 Jeffrey C. Shields NULL Driver 24 Nora W. Hardy Customer NULL 25 Rolando K. Smith NULL Driver 26 James K. McGaha Customer NULL 27 Elizabeth S. Bongiorno NULL Driver 28 Anne M. Gurley Customer NULL 29 Janet A. Ricker NULL Driver 30 Nathan M. Schneider Customer NULL 5