You are on page 1of 8

Database Systems

Exercise 5:

The assignments in this part are specific to individual databases you created in earlier
exercise. Please execute them only on the specified datasets. The assignments consists of
writing some queries for understanding the AGGREGATION concepts
SQL Training – Assignment 5

Contents
1 Assignment Preparation .....................................................................................................................................3
2 The Datasets .......................................................................................................................................................3
3. Assignment Tasks .................................................................................................................................................3
[STUDENTS dataset] ...............................................................................................................................................3
[BAKERY dataset] ....................................................................................................................................................4
[CARS dataset] ........................................................................................................................................................4
[CSU dataset] ..........................................................................................................................................................5
[INN dataset] ..........................................................................................................................................................5
[MARATHON dataset].............................................................................................................................................6
[AIRLINES dataset] ..................................................................................................................................................6
[WINE dataset] .......................................................................................................................................................7
[KATZENJAMMER dataset] .....................................................................................................................................7
5. Submission Instructions .......................................................................................................................................8
SQL Training – Assignment 5

1 Assignment Preparation

This is an individual assignment. Each person has to complete all work required in the lab, and submit
all required materials exactly as specified in this assignment. For this assignment, you will be using your
MySQL account. The information about your accounts was given to you by one of our team or
Administrator.

You will be using the MySQL server hosted at AWS and the MySQL workbench client.
https://dev.mysql.com/get/Downloads/MySQLGUITools/mysql-workbench-community-6.3.10-
winx64.msi

For the purposes of this, and future assignments, an SQL script is a text file that contains a sequence of
SQL statements and MySQL commands, as well as SQL comments. Typically, these files should receive a
.sql extension. You will be required to prepare and submit a number of SQL scripts for this assignment.

2 The Datasets

You are to write and debug (to ensure correct output) the SQL queries that return information as
requested for each of the information needs outlined below.

The information needs can be addressed with either a simple SELECT statement (i.e., a SELECT statement
without grouping, aggregation and nested subqueries) and/or with the UNION operation, or with a
SELECT statement that uses some aggregation. Please note, that some queries in this lab may require
you to imitate the intersection operation with other available means. Each information need must be
met with a single SQL statement. For this assignment, you will prepare one SQL script for each database.

NOTE: Please provide a comment in front of each SQL statement in each of your files. The simplest
comment can just state the query number (e.g., "-- Q3.") for this particular database. This is very useful
for the situations when for one reason or another you elected not to implement a query.

3. Assignment Tasks

[STUDENTS dataset]
For the STUDENTS dataset, write an SQL script STUDENTS-lab5.sql containing SQL statements
answering the following information requests.
1. Find all pairs of students with the same first name. Report each pair of students exactly once.
Report first and last names of each of the two students, and their grades.
2. Find all first-grade students who are NOT taught by OTHA MOYER. Report their first and last
names in alphabetical order by last name.
3. Report the total number of third- and fourth-graders in the school.
4. Find and report the number of students taught by LORIA ONDERS
SQL Training – Assignment 5

5. For each grade, report the number of classrooms in which it is taught and the total number of
students in the grade. Sort the output by the number of classrooms in descending order, then by
grade in ascending order.
6. For each kindergarden classroom, report the total number of students. Sort output in the
descending order by the number of students.
7. For each fourth grade classroom, report the student (last name) who is the last (alphabetically)
on the class roster. Sort output by classroom.

[BAKERY dataset]
Write a SQL script BAKERY-lab5.sql containing SQL statements answering the following
information requests.

Note: Here, and everywhere else your queries must match exactly the wording of the information
need. For example, if you are asked to find the price of an Apricot Tart, the following query:

SELECT price FROM goods WHERE


CODE = ’90-APR-PF’;

is considered to be incorrect because nowhere in the query was the code ’90-APR-PF’ mentioned.
(This is especially important when you are expected to produce a join of two or more tables, but
instead look up the foreign key value and use it verbatim in the query. Such queries will be
marked as incorrect on the spot).

1. Find all customers who purchased, during the same trip to the bakery, two different Croissants.
Report first and last names of the customers in alphabetical order by their last name.
2. Find all days on which either ALMETA DOMKOWSKI made a purchase, or someone purchased
a Gongolais Cookie. Sort dates in chronological order. Each date shall appear exactly once.
3. Report the total amount of money NATACHA STENZ spent at the bakery during the month of
October, 2007.
4. Report the total amount of money spent by bakery customers in October 2007 on Chocolate-
flavored items.
5. For each purchase made by NATACHA STENZ output the receipt number, the date of purchase,
the total number of items purchased and the amount paid. Sort in descending order by the
amount paid
6. For each day of the week of October 8 (Monday to Sunday) report the total number of
purchases (receipts), the total number of pastries purchased and the overall daily revenue.
Report results in chronological order and include both the day of the week and the date.
7. Report all days on which more than ten tarts were purchased, sorted in chronological order.
8. Find the total amount of money the bakery earned in October 2007 from selling eclairs. Report
just the amount.

[CARS dataset]

Here are the queries for the CARS dataset. Name the SQL scrips CARS-lab5.sql
SQL Training – Assignment 5

1. Find all cars made after 1980 with gas mileage better than the 1982 Honda civic. Report full
name of the car, year it was made and the name of the manufacturer. Sort output in descending
order by gas mileage.
2. Find the average, maximum and minimum horsepower for 4-cylinder vehicles manufactured
by renault between 1971 and 1976 inclusively.
3. Find how many cars produced in 1971 had better acceleration than a 1972 volvo 145e (sw).
Report just the number.
4. For each year when US-manufactured cars averaged less than 100 horsepowers, report the
highest and the lowest engine displacement number. Sort in chronological order.
5. For each year in which honda produced more than 2 models, report the best, the worst and
the average gas milage of a toyota vehicle. Report results in chronological order.
6. For each Japanese car maker (reported by their short name) report the best mileage per gallon
of a car produced by it and the average acceleration. Sort output in ascending order by the best
mileage.

[CSU dataset]

Here are the queries for the CSU dataset. Name the SQL scrips CSU-lab5.sql

1. Find the largest, the smallest and the average fee on a CSU campus in 2005.
2. Report the average student to faculty (use student FTE to faculty FTE ratio) ratio in 2004 among
the campuses where 2004 enrollment (FTE numbers) was greater than 15000.
3. For each campus for which data exists for more than 60 years, report the average, the
maximum and the minimum enrollment (for all years).
4. For each campus in LA and Orange counties report the total number of degrees granted
between 1998 and 2002 (inclusively). Sort the output in descending order by the number of
degrees.
5. For each campus that had more than 20000 enrolled students in 2004 report the number of
disciplines for which the campus had non-zero graduate enrollment. Sort the output in
alphabetical order by the name of the campus. (This query should exclude campuses that had no
graduate enrollment at all).

[INN dataset]

For the INN dataset, create a SQL script file INN-info5.sql with SQL queries for the following information
needs. (When no year is supplied in the query descriptions below, assume 2010).
Note: If the full name of a room is provided in the question, you cannot replace with a three-letter code
in the text of the query.
1. Find all rooms that were occupied on all three of the following dates: May 15, 2010, August
18, 2010 and December 12, 2010. Report just the full name of the room and the room code. Sort
output in alphabetical order by room name
2. Find the number of August reservations (both check-in and checkout dates are in August)
where two adults are staying with two children.
SQL Training – Assignment 5

3. Find how many different durations of stay for trips that commenced and ended in July of 2010
were in Interim but salutary room.
4. Report the total number of reservations that commenced on Fridays and the total revenue
they brought in. (Hint: look up the date of the first Friday on the calendar).
5. For each day of the week, report the total number of reservations commenced on it and the
total revenue these reservations brought. Report days of week as Monday, Tuesday, etc.
6. For each room report the highest markup against the base price and the smallest markup (i.e.,
largest markdown). Report markups and markdowns in absolute terms (absolute difference
between the base price and the rate). Sort output in descending order by the absolute value of
the largest markup. Report full names of the rooms.
7. For each room report how many nights in 2010 the room was occupied. Report the room code,
the full name of the room and the number of occupied nights. Sort in descending order by
occupied nights. (Note: it has to be number of nights in 2010 - the last reservation in each
room may and will can go beyond December 31, 2010, so the”extra” nights in 2011 need to be
deducted).

[MARATHON dataset]

For this dataset, all times must be shown in the output in the same format as in the original dataset (in
the file marathon.csv). Note: please remember that the best, i.e., the fastest time is the smallest one!
1. For each gender/age group, report total number of runners in the group, the overall place of
the best runner in the group and the overall place of the worst runner in the group. Output result
sorted by age group and sorted by gender (F followed by M) within each age group.
2. Report the total number of gender/age groups for which both the first and the second place
runners (within the group) hail from the same state.
3. For each full minute, report the total number of runners whose pace was between that number
of minutes and the next. (That is, how many runners ran the marathon at a pace between 5 and
6 mins, how many - at a pace between 6 and 7 mins, and so on).
4. For each CT town with 3 or more participants in the race, report the average time of its resident
runners in the race computed in seconds. Output the results sorted by the average time (best
average time first).
5. For each state, whose representatives participated in the marathon report the number of
runners from it who finished in top 10 in their gender-age group (if a state did not have runners
in top 10s, do not output information about the state). Output in descending order by the
computed number.

[AIRLINES dataset]

For the AIRLINES dataset, create a SQL script file AIRLINES-info5.sql with SQL queries for the following
information needs. You may not substitute numeric codes for airlines in the place of airline names in the
queries below. You may use three-letter airport abbreviations whenever they are used in the questions.
1. Find all airports with exactly 17 outgoing flights. Report airport code and the full name of the
airport sorted in alphabetical order by the code.
2. Find the number of airports from which airport ANP can be reached with exactly one transfer.
(make sure to exclude ANP itself from the count). Report just the number.
SQL Training – Assignment 5

3. Find the number of airports from which airport ATE can be reached with at most one transfer.
(Make sure to exclude ATE itself from the count). Report just the number.
4. For each airline report the total number of airports from which it has at least one outgoing
flight. Report the full name of the airline and the number of airports computed. Report the results
sorted by the number of airports in descending order.

[WINE dataset]

Create a SQL script WINE-lab5.sql containing SQL statements representing the following information
needs.

1. List all 2006 vintage wines from Napa (any appellation within the county) whose total revenue
exceeds that of the 2006 ’Appelation Series’2 Paso Robles Zinfandel from ’Rosenblum’ winery.
For each wine report grape, winery and name, score and revenue. Order by revenue.
2. Find the average score of a Paso Robles Zinfandel.
3. Find the total revenue from all red wines made by Kosta Browne.
4. Find the average number of cases of a Pinor Noir produced from grapes sourced from the
Central Coast.
5. For each appellation inside Central Coast compute the total (known) sales volume that it can
generate for the wines produced in 2008. Sort the output in descending order by the total sales
volume. (Note: recall what a case of wine is).
6. For each county in the database, report the score of the highest ranked 2009 red wine. Exclude
wines that do not have a county of origin (’N/A’). Sort output in descending order by the best
score.
7. For each year, report the total number of red Sonoma County wines whose scores are 90 or
above. Output in chronological order.
8. For each wine score value above 88, report average price, the cheapest price and the most
expensive price for a bottle of wine with that score (for all vintage years combined), the total
number of wines with that score and the total number of cases produced. Sort by the wine score.

[KATZENJAMMER dataset]

Create a SQL script KATZENJAMMER-lab5.sql containing SQL statements representing the


following information needs.
1. Find the number of times Turid played bass balalaika on Katzenjammer songs.
2. Find the number of times Solveig was positioned center stage while Marianne was playing
drums.
3. Find the total number of different instruments Anne-Marit played on Katzenjammer songs.
4. Find how many different performers played guitar.
5. Find how many times the lead vocalist on a song played bass balalaika.
6. Report all instruments (in alphabetical order) that were played by three or more people.
7. Report how many different unique instruments each performer plays on songs from ’Le Pop’.
Sort the output by the first name of the performers.
SQL Training – Assignment 5

8. For each performer, report the number of times they played more than one instrument on the
same song. Sort output in alphabetical order by first name of the performer

5. Submission Instructions

Please, name your files exactly as requested (including capitalization and any typos present in
instructor’s filenames), and submit all files in a single archive. Correct submission helps us review the
developments quickly and ensures its correctness.

Inside it, the archive shall contain nine directories named AIRLINES, CSU, CARS, BAKERY,
KATZENJAMMER, MARATHON, STUDENTS, WINE and INN (same as the dataset names). In addition, the
root of the directory must contain a README file, which should, at a minimum, contain your name, SBP
Email address and any specific comments concerning your submission.

Each directory shall contain all SQL scripts built by you for the specific dataset. The scripts shall be named
as follows.

File naming conventions:


• database creation scripts. The filename shall be <Account ID><dataset>-setup.sql. Here,
<dataset> is the name of the directory in ALL CAPS. E.g., for the CARS dataset, the filename will
be A1CARS-setup.sql.
• table deletion scripts. The filename shall be <Account ID><dataset>-cleanup.sql. E.g., for the
CARS dataset, the filename will be A1CARS-cleanup.sql.
• table population scripts. The filename shall be <Account ID><dataset>-build-<table>.sql. Here
<table> is the name of the .csv file (not the name of the table that you are building). For example,
for the table populating the list of car makers in the CARS database, the filename will be A1CARS-
build-car-makers.sql. Use the same capitalization as the filename in the dataset: e.g., A1CSU-
build-Campuses.sql and A1INN-build-Rooms.sql, A1CARS-build-cars-data.csv
and A1BAKERY-build-items.sql.
• testing scripts. These are scripts described in item 6 of assignment. The filename shall be
<Account ID><dataset>-test.sql. E.g., A1CARS-test.sql

If you are submitting code/scripts, include, at the beginning of the file, a few comment lines with this
information. Please include your name and SBP email address in all files you are submitting for coding
part/scripts as well.

You might also like