SQL Exercises
SQL Exercises
and
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.7
6.8
6.9
List the names and addresses of all guests in London, alphabetically ordered by name.
SELECT guestName, guestAddress FROM Guest WHERE address LIKE %London%
ORDER BY guestName;
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.
SELECT * FROM Room WHERE price < 40 AND type IN (D, F)
ORDER BY price;
(Note, ASC is the default setting).
6.11
6.13
6.14
What is the total revenue per night from all double rooms?
SELECT SUM(price) FROM Room WHERE type = D;
6.15
List the price and type of all rooms at the Grosvenor Hotel.
SELECT price, type FROM Room
WHERE hotelNo =
(SELECT hotelNo FROM Hotel
WHERE hotelName = Grosvenor Hotel);
6.17
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.
SELECT r.* FROM Room r LEFT JOIN
What is the total income from bookings for the Grosvenor Hotel today?
SELECT SUM(price) FROM Booking b, Room r, Hotel h
WHERE (dateFrom <= CURRENT_DATE AND
dateTo >= CURRENT_DATE) AND
r.hotelNo = h.hotelNo AND r.roomNo = b.roomNo AND
hotelName = Grosvenor Hotel;
6.20
List the rooms that are currently unoccupied at the Grosvenor Hotel.
SELECT * FROM Room r
WHERE roomNo NOT IN
(SELECT roomNo FROM Booking b, Hotel h
WHERE (dateFrom <= CURRENT_DATE AND
dateTo >= CURRENT_DATE) AND
b.hotelNo = h.hotelNo AND hotelName = Grosvenor Hotel);
6.21
What is the lost income from unoccupied rooms at the Grosvenor Hotel?
SELECT SUM(price) FROM Room r
WHERE roomNo NOT IN
(SELECT roomNo FROM Booking b, Hotel h
WHERE (dateFrom <= CURRENT_DATE AND
dateTo >= CURRENT_DATE) AND
b.hotelNo = h.hotelNo AND hotelName = Grosvenor Hotel);
Grouping
6.22
6.23
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?
SELECT hotelNo, SUM(price) FROM Room r
WHERE roomNo NOT IN
(SELECT roomNo FROM Booking b, Hotel h
WHERE (dateFrom <= CURRENT_DATE AND
dateTo >= CURRENT_DATE) AND
b.hotelNo = h.hotelNo)
GROUP BY hotelNo;
Populating Tables
6.27
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.
6.31
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.
SELECT * FROM Employee ORDER BY lName, fName;
6.33
6.34
List the names and addresses of all employees who are Managers.
SELECT fName, lName, address FROM Employee WHERE position = Manager;
6.35
Produce a list of the names and addresses of all employees who work for the IT department.
SELECT fName, lName, address from Employee e, Department d
WHERE e.deptNo = d.deptNo
AND d.deptName = IT;
6.36
Produce a complete list of all managers who are due to retire this year, in alphabetical order
of surname.
6.37
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.
SELECT e.lName, e.fName, hoursWorked
FROM WorksOn w, Employee e, Department d
WHERE e.deptNo = d.deptNo
AND e.empNo = w.empNo
ORDER by d.deptNo, e.lName;
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.
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.
SELECT COUNT(empNo) as empCount, deptNo
FROM Employee
GROUP BY deptNo
HAVING COUNT(empNo) > 10;
7.2
Discuss the functionality and importance of the Integrity Enhancement Feature (IEF).
Required data:
Domain constraint:
Entity integrity:
Referential integrity:
General constraints:
7.4
column definition;
PRIMARY KEY
FOREIGN KEY
CHECK constraints
7.5
7.6
DISTINCT is not specified; that is, duplicate rows must not be eliminated from the query
results.
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.
Advantages
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).
SQL security mechanism is based on discretionary access control.
7.9
Exercises
Answer the following questions using the relational schema from the Exercises at the end of Chapter 4.
Create the Hotel table using the integrity enhancement features of SQL.
CREATE DOMAIN HotelNumber AS CHAR(4);
CREATE TABLE Hotel(
hotelNo
HotelNumber
hotelName
VARCHAR(20)
city
VARCHAR(50)
PRIMARY KEY (hotelNo));
7.11
NOT NULL,
NOT NULL,
NOT NULL,
Now create the Room, Booking, and Guest tables using the integrity enhancement features of SQL
with the following constraints:
(a)
(b)
(c)
(d)
(e)
(f)
NOT NULL,
NOT NULL,
NOT NULL);
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.
CREATE TABLE BookingOld(
hotelNo
guestNo
dateFrom
dateTo
roomNo
10
CHAR(4)
CHAR(4)
DATETIME
DATETIME
VARCHAR(4)
NOT NULL,
NOT NULL,
NOT NULL,
NULL,
NOT NULL);
Create a view containing the hotel name and the names of the guests staying at the hotel.
CREATE VIEW HotelData(hotelName, guestName)
AS
SELECT h.hotelName, g.guestName
FROM Hotel h, Guest g, Booking b
WHERE h.hotelNo = b.hotelNo AND g.guestNo = b.guestNo AND
b.dateFrom <= CURRENT_DATE AND
b.dateTo >= CURRENT_DATE;
7.14
Create a view containing the account for each guest at the Grosvenor Hotel.
CREATE VIEW BookingOutToday
AS
SELECT g.guestNo,g.guestName,g.guestAddress,r.price*(b.dateTo-b.dateFrom)
FROM Guest g, Booking b, Hotel h, Room r
WHERE g.guestNo = b.guestNo AND r.roomNo = b.roomNo AND
b.hotelNo = h.hotelNo AND h.hotelName = Grosvenor Hotel AND
b.dateTo = CURRENT_DATE;
7.15
Give the users Manager and Deputy full access to these views, with the privilege to pass the
access on to other users.
GRANT ALL PRIVILEGES ON HotelData
TO Manager, Director WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON BookingOutToday
TO Manager, Director WITH GRANT OPTION;
7.16
Give the user Accounts SELECT access to these views. Now revoke the access from this user.
GRANT SELECT ON HotelData TO Accounts;
GRANT SELECT ON BookingOutToday TO Accounts;
REVOKE SELECT ON HotelData FROM Accounts;
REVOKE SELECT ON BookingOutToday FROM Accounts;
7.17
11
SELECT *
FROM HotelBookingCount;
SELECT h.hotelNo, COUNT(*)
FROM Hotel h, Room r, Booking b
WHERE h.hotelNo = r.hotelNo AND r.roomNo b.roomNo
GROUP BY h.hotelNo;
(b)
SELECT hotelNo
FROM HotelBookingCount
WHERE hotelNo = H001;
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;
(c)
SELECT MIN(bookingCount)
FROM HotelBookingCount;
Invalid bookingCount is based on an aggregate function, so cannot be used within
another aggregate function.
(d)
SELECT COUNT(*)
FROM HotelBookingCount;
Invalid for reason given above.
(e)
SELECT hotelNo
FROM HotelBookingCount
WHERE bookingCount > 1000;
Invalid bookingCount is based on an aggregate function, so cannot be used in the
WHERE clause.
(f)
SELECT hotelNo
FROM HotelBookingCount
ORDER BY bookingCount;
SELECT h.hotelNo, COUNT(*) AS bookingCount
FROM Hotel h, Room r, Booking b
WHERE h.hotelNo = r.hotelNo AND r.roomNo b.roomNo
12
7.19
13