For each question below (most anyway), you will write an SQL query. Additionally, for each question with a star (*), provide the SQLite output (the table that is printed). The output can be cut-and-pasted from your SQLite browser.
Each of these questions should be answered with a single SQL query that would hypothetically work for any logical database instance. In other words, you shouldn't "hard-code" anything in a query that will make the query return incorrect information if any of the information in the database changes.
The Customer table maintains the personal information for people who order pizzas, such as a unique ID, name, phone number, credit card number (ccn), and neighborhood (e.g., Midtown, East Memphis, Evergreen). It is possible that two different people have the same name.
In the Pizza table, every pizza has an ID, name (e.g., "the works"), particular size (e.g., 7 inches) and price. Note that different pizzas may have the same name, but different sizes.
The Order table includes the records about which customer ordered which pizza, quantity of pizzas, orderyear (e.g., 2014), ordermonth (e.g., 12), orderday (e.g., 27), and the order time (e.g., "6:13pm"). Note that an order can contain more than one pizza; there will just be multiple tuples with the same cust_id, date, and time.
The Supplies table includes the information of the various groceries used by the store: the name, unit price, and the amount left in the store (e.g. the store might have 3 lbs of mozzarella left with a unit price of $5 per lb).
The Ingredient table keeps the records about the amount of ingredients used by each pizza (so "The works 7 inch" might use only 0.1 lbs of mozzarella, while "Four cheese 12 inch" might use 0.4 lbs).
Write SQL queries to answer parts b through f. Use only the operators and SQL statements we have learned in class. In addition, avoid the operators IN, ALL, ANY, and EXISTS.
Suggestion: It is very tempting to create a database and throw some sample data into it to do this problem on a computer. I encourage you not to do this, or at least only do it to check your work. I purposefully did not give you a database for this question so you could practice writing SQL queries without being able to test them out, because the midterm will have questions like this.
Note that in general, there is no guarantee that there will be only one flight that has this number of passengers on it! Your query should be able to return them all if there is more than one.
[Sanity check: For this particular database instance, there is only one flight with the largest number of passengers on it and it involves a New York City airport.]
Note that there are multiple airlines in the database that fly the same route, and possibly multiple flights per day for each airline on that route.
Note that "one-way route" means we are not combining flights from Memphis to Atlanta with flights from Atlanta to Memphis. Those are two separate one-way routes.
Hint: this is hard. You may want to use SQL VIEWs to save parts of your query as you develop them. An SQL view is similar to the relational algebra shorthand notation: a view allows you to give a name to a query that you plan to use later on (for instance, as a subquery). If you use views, you should write down your CREATE VIEW statements as well as your final SELECT statement.
Hint 2: You may find it useful as well to read the book section on correlated subqueries. This may be helpful as well.
Hint 3: You should end up with a 12-row table. [Sanity check: You should end up with the most popular Delta (DL) route being from Minneapolis/St. Paul (MSP) to Salt Lake City (SLC), carrying 572 passengers in total.
The second reason why LIMIT 1 is often deceptive is that while it can help you find the maximum or minimum something in SQL (albeit inefficiently), it can create problems when you try to expand your query to find other attributes related to the maximum or minimum something, if the maximum or minimum appears multiple times. For instance, pretend you have a database table that stores items sold in a store and their prices. You want to find the price of the most expensive item. You can do this with an ORDER BY/LIMIT 1 query (again, it's horribly inefficient and you shouldn't do it!) but it works. What doesn't work, however, is where you try to then get the name of the item that has the most expensive price --- what if there are multiple items with this price? Using LIMIT 1 will only retrieve one of them, and you don't know which one. This kind of query should be done with a subquery (see the link in the last question above for an example of this exact situation).