Documente Academic
Documente Profesional
Documente Cultură
Exercise 4: SQL
The exercises marked with * will be discussed in the exercise session. You can solve the other
exercises as practice, ask questions about them in the session, and hand them in for feedback.
All exercises may be relevant for the exam.
Ask Renato (marenato@inf.ethz.ch) for feedback on this week’s exercise sheet or give it
to the TA of your session (preferably stapled and with your e-mail address).
In this exercise you will write SQL queries and execute them on the databases that you have
to set up following the instructions found in the course website. For reference, the database
schema is provided where primary keys are underlined and foreign keys are in italic.
1 Employee Database*
The following queries refer to the employee database. For reference, the schema of this database
is shown in Figure 1.
departments(dept_no:string, dept_name:string)
dept_emp(emp_no:int, dept_no:string, from_date:date, to_date:date)
dept_manager(emp_no:int, dept_no:string, from_date:date, to_date:date)
employees(emp_no:int, birth_date:date, first_name:string, last_name:string,
gender:enum, hire_date:date)
salaries(emp_no:int, salary:int, from_date:date, to_date:date)
title(emp_no:int, title:string, from_date:date, to_date:date)
1.2 Salaries
Consider the following query, which statement describes the best what the query is meant to do.
1.5 1988
Write a query that determines for each department the average salary of anyone starting or
receiving a salary change in 1988 from that department. It should display the department
number, its name and its average.
2 ZVV Database
The following queries refer to the ZVV database. For reference, the schema of this database is
shown in Figure 2.
GROUP BY tram_number
ORDER BY count_trips DESC;
Finds how many tram lines (tram_number) are in a trip and orders its result in de-
scending order.
Finds the number of trips each tram line (tram_number) makes and orders its result
by number of trips in descending order.
Finds and counts the number of tram lines (tram_number) and orders them in descend-
ing order.
Finds the trips each tram line (tram_number) makes and orders its result by number
of trips in ascending order.
3 TPC-H Database
The following queries refer to the TPC-H database. For reference, the schema of this database
is shown in Figure 3.
Data Modelling and Databases (DMDB), Spring Semester 2017, Exercise 4: SQL, March
20/March 27, 2017 Legi-Nr:
SELECT nationname
FROM nation, region
WHERE __________________
________________________
________________________;
Which of the following predicates makes the query perform the task correctly:
regionname = ’AMERICA’ OR
regionname = ’AFRICA’ OR
regionname = ’ASIA’
regionname = ’AMERICA’ AND
regionname = ’AFRICA’ AND
regionname = ’ASIA’
nation.regionid = region.regionid AND
(regionname = ’AMERICA’ OR
regionname = ’AFRICA’ OR regionname = ’ASIA’)
nation.regionid = region.regionid AND
(regionname = ’AMERICA’ AND
regionname = ’AFRICA’ AND regionname = ’ASIA’)
Data Modelling and Databases (DMDB), Spring Semester 2017, Exercise 4: SQL, March
20/March 27, 2017 Legi-Nr:
SELECT orderdate
FROM orders, customer
WHERE orders.customerid = customer.customerid
AND customername = ’Customer#000000001’
ORDER BY orderdate ASC
LIMIT 1;
D. The ORDER BY statement is actually not needed to get the data needed.
E. The ORDER BY and LIMIT statements help us getting the actual first order.