The final part of the project is to pull together all the different aspects of SQL that we have learned so far and apply them to a made-up database for Juicd. These will primarily be marked on correctness - whether they do the right thing or not - but I may check some of the code if it is particularly slow or similar.
The database schema is available in the file juicd.sql and a sample dataset is in data.txt. You should load the schema and the data into your own server, and then use MySQL Workbench to get the structure diagram for the schema.
A faster way to insert the data is to download juicdump3.sql and upload this file to your server. NOTE: This is the new dataset with data for about six months.
Post to help1402 for guidance if there is any ambiguity in how to interpret the questions (but please do not post actual code unless you have okayed it with me first).
The remaining questions will be added to this file in the next few days.
Place all 10 queries into a single text file with the name basic.sql
so that they can be automatically run by a script.
These queries are all single SQL queries that require nothing more than joins and elementary use of the aggregate functions with GROUP BY
. All students should be able to write queries at this level for the exam.
manages
contains information about managers.)worksAt
table).lineMgr
table)?What SQL query will list the address of each outlet, together with the total number of orders that they have served?
Each cup of juice is represented by a row in the JuiceCup
table which stores the cupId
and the size of the cup. The actual juices that make up each cup of juice are stored in the table comprises
which lists the juices and their percentages. For example, SELECT * FROM comprises WHERE cupid = 1000
tells us that this cup of juice is 40% Juice #7, 40% of Juice #8 and 20 % of Juice #20;
What SQL query will list the actual juices by name and their percentages for this particular juice cup (i.e. the juice with cupId
1000)?
Juice
contains the price in cents-per-ml of each juice).These queries are more complex than the basic SQL
queries and may involve more exotic join conditions, such as outer joins, subqueries and simple stored procedures or functions.
Place all queries and function definitions into a single text file with the name complex.sql
so that they can be automatically run by a script.
comprises
for a given JuiceCup
is actually a different ingredient.)orderId
) that consist only of juices (no non-juice items)?A stored function is similar to a stored procedure, except that it returns a value that can be used in a SQL
statement just like the other SQL
functions. See this Stored Function Tutorial for some more details on how to declare the function and return the values (ignore the DETERMINISTIC
keyword).
juiceCupCost(id INT) RETURNS DOUBLE
that will be called with the id of a JuiceCup
and then return the cost (in cents) of that particular JuiceCup
. For example, Juice Cup 10 is a 400ml juice that is a 50-50 mixture of raspberry-pear, and it costs $3.40.mysql> select juiceCupCost(10);
+------------------+
| juiceCupCost(10) |
+------------------+
| 340 |
+------------------+
juiceOrderCost(id INT) RETURNS DOUBLE
that returns the total price of the juice-component of an order. Obviously this function should use your previous function for the cost of each individual JuiceCup
.Place all queries and view/procedure definitions into a single text file with the name additional.sql
so that they can be automatically run by a script.
Write a stored function totalOrderCost(id INT) RETURNS DOUBLE
that
returns the total cost (in cents) of the order with ordernumber id
. (You
should use your previous functions, so make sure you test them well!)
Create a view CustomerPricedOrder
with
columns date DATE
,customerId INT
, orderId INT
and
orderCost DOUBLE
that provides a more accessible way for the DB user to run
queries regarding customer orders. (The columns have their natural meanings)
Juicd runs a Customer Of The Month promotion, and sends coupons and other rewards
to the highest spending customer each month. Write a stored
procedure listCofM()
that creates a table
customerOfMonth(year INT, month TEXT, COfM INT, cOfMemail TEXT)
that lists
for each year and month (given as "January", "February" etc) the customer number and
email of the biggest spender for that month (you may ignore the possibility that there may
be joint winners).
Written with StackEdit.