SQL Exercises
SQL Exercises
SQL Exercises
where Hotel contains hotel details and hotelNo is the primary key;
Room contains room details for each hotel and (roomNo, hotelNo) forms the primary key;
Booking contains details of the bookings and (hotelNo, guestNo, dateFrom) forms the primary
key;
and Guest contains guest details and guestNo is the primary key.
Exercises
For the Exercises 6.7 6.28, use the Hotel schema defined at the start of the Exercises at the end of
Chapter 3.
Simple Queries
6.9 List the names and addresses of all guests in London, alphabetically ordered by name.
Strictly speaking, this would also find rows with an address like: 10 London Avenue, New York.
6.10 List all double or family rooms with a price below 40.00 per night, in ascending order of price.
6.11 List the bookings for which no dateTo has been specified.
Database Systems: Instructors Guide - Part III
Aggregate Functions
6.14 What is the total revenue per night from all double rooms?
6.15 How many different guests have made bookings for August?
6.16 List the price and type of all rooms at the Grosvenor Hotel.
6.18 List the details of all rooms at the Grosvenor Hotel, including the name of the guest staying in the
room, if the room is occupied.
2
Database Systems: Instructors Guide - Part III
6.19 What is the total income from bookings for the Grosvenor Hotel today?
6.20 List the rooms that are currently unoccupied at the Grosvenor Hotel.
6.21 What is the lost income from unoccupied rooms at the Grosvenor Hotel?
Grouping
3
Database Systems: Instructors Guide - Part III
6.24 What is the average number of bookings for each hotel in August?
SELECT AVG(X)
FROM ( SELECT hotelNo, COUNT(hotelNo) AS X
FROM Booking b
WHERE (dateFrom <= DATE2004-08-01 AND
dateTo >= DATE2004-08-01) OR
(dateFrom >= DATE2004-08-01 AND
dateFrom <= DATE2004-08-31)
GROUP BY hotelNo);
6.25 What is the most commonly booked room type for each hotel in London?
SELECT MAX(X)
FROM ( SELECT type, COUNT(type) AS X
FROM Booking b, Hotel h, Room r
WHERE r.roomNo = b.roomNo AND b.hotelNo = h.hotelNo AND
city = London
GROUP BY type);
6.26 What is the lost income from unoccupied rooms at each hotel today?
Populating Tables
4
Database Systems: Instructors Guide - Part III
General
6.29 Investigate the SQL dialect on any DBMS that you are currently using. Determine the compliance
of the DBMS with the ISO standard. Investigate the functionality of any extensions the DBMS
supports. Are there any functions not supported?
This is a small student project, the result of which is dependent on the dialect of SQL being used.
6.30 Show that a query using the HAVING clause has an equivalent formulation without a HAVING
clause.
Hint: Allow the students to show that the restricted groups could have been restricted earlier with
a WHERE clause.
Hint: Allow the students to show that each of the relational algebra operations can be expressed in
SQL.
Case Study 2
For Exercises 6.326.40, use the Projects schema defined in the Exercises at the end of Chapter 5.
6.32 List all employees in alphabetical order of surname, and then first name.
6.34 List the names and addresses of all employees who are Managers.
6.35 Produce a list of the names and addresses of all employees who work for the IT department.
6.36 Produce a complete list of all managers who are due to retire this year, in alphabetical order
of surname.
6.37 Find out how many employees are managed by James Adams.
5
Database Systems: Instructors Guide - Part III
6.38 Produce a report of the total hours worked by each employee, arranged in order of
department number and within department, alphabetically by employee surname.
6.39 For each project on which more than two employees worked, list the project
number, project name, and the number of employees who work on that project.
6.40 List the total number of employees in each department for those departments with more than
10 employees. Create an appropriate heading for the columns of the results table.
6
Database Systems: Instructors Guide - Part III
Review Questions
The eight base types are: Boolean, character, bit (removed from SQL:2003), exact numeric,
approximate numeric, datetime, interval, large object. See Section 7.1.2.
7.2 Discuss the functionality and importance of the Integrity Enhancement Feature (IEF).
column definition;
PRIMARY KEY
FOREIGN KEY
CHECK constraints
ISO standard specifies the views that must be updatable in a system that conforms to the standard.
Definition given in SQL standard is that a view is updatable if and only if:
DISTINCT is not specified; that is, duplicate rows must not be eliminated from the query
results.
7
Database Systems: Instructors Guide - Part III
Every element in the SELECT list of the defining query is a column name (rather than a
constant, expression, or aggregate function) and no column appears more than once.
The FROM clause specifies only one table; that is, the view must have a single source table
for which the user has the required privileges. If the source table is itself a view, then that
view must satisfy these conditions. This, therefore, excludes any views based on a join, union
(UNION), intersection (INTERSECT), or difference (EXCEPT).
The WHERE clause does not include any nested SELECTs that reference the table in the
FROM clause.
There is no GROUP BY or HAVING clause in the defining query.
In addition, every row that is added through the view must not violate the integrity constraints of
the base table (Section 7.4.5).
7.7 What is a materialized view and what are the advantages of a maintaining a materialized view
rather than using the view resolution process?
Materialized view is a temporary table that is stored in the database to represent a view, which
is maintained as the base table(s) are updated.
7.8 Describe the difference between discretionary and mandatory access control. What type of
control mechanism does SQL support.
Discretionary each user is given appropriate access rights (or privileges) on specific database
objects.
Mandatory each database object is assigned a certain classification level (e.g. Top Secret, Secret,
Confidential, Unclassified) and each subject (e.g. user, application) is given a designated
clearance level (Top Secret > Secret > Confidential > Unclassified).
Exercises
Answer the following questions using the relational schema from the Exercises at the end of Chapter 4.
8
Database Systems: Instructors Guide - Part III
7.10 Create the Hotel table using the integrity enhancement features of SQL.
7.11 Now create the Room, Booking, and Guest tables using the integrity enhancement features of SQL
with the following constraints:
9
Database Systems: Instructors Guide - Part III
7.12 Create a separate table with the same structure as the Booking table to hold archive records.
Using the INSERT statement, copy the records from the Booking table to the archive table relating
to bookings before 1st January 2003. Delete all bookings before 1st January 2003 from the
Booking table.
10
Database Systems: Instructors Guide - Part III
7.13 Create a view containing the hotel name and the names of the guests staying at the hotel.
7.14 Create a view containing the account for each guest at the Grosvenor Hotel.
7.15 Give the users Manager and Deputy full access to these views, with the privilege to pass the
access on to other users.
7.16 Give the user Accounts SELECT access to these views. Now revoke the access from this user.
11
Database Systems: Instructors Guide - Part III
For each of the following queries, state whether the query is valid and for the valid ones
should how each of the queries would be mapped onto a query on the underling base tables.
(a) SELECT *
FROM HotelBookingCount;
SELECT h.hotelNo
FROM Hotel h, Room r, Booking b
WHERE h.hotelNo = r.hotelNo AND r.roomNo b.roomNo AND
h.hotelNo = H001
GROUP BY h.hotelNo;
12
Database Systems: Instructors Guide - Part III
GROUP BY h.hotelNo
ORDER BY bookingCount;
and a view SupplierParts, which contains the distinct part numbers that are supplied by at
least one supplier:
13