Chapter 6 SQL: Data Manipulation Review Questions: Application
Chapter 6 SQL: Data Manipulation Review Questions: Application
Review Questions
6.1 Briefly describe the four basic SQL DML statements and explain their use.
Section 6.3 describes the usage of basic data manipulation SQL statements; SELECT INSERT, UPDATE and
DELETE.
6.2 Explain the importance and application of the WHERE clause in the UPDATE and DELETE statements.
Application
Importance
6.3 Explain the function of each of the clauses in the SELECT statement. What restrictions are imposed on these
clauses?
If the SELECT list includes an aggregate function and no GROUP BY clause is being used to group data
together, then no item in the SELECT list can include any reference to a column unless that column is the
argument to an aggregate function.
When GROUP BY is used, each item in the SELECT list must be single-valued per group. Further, the
SELECT clause may only contain:
Column names.
Aggregate functions.
Constants.
An expression involving combinations of the above.
All column names in the SELECT list must appear in the GROUP BY clause unless the name is used only in an
aggregate function.
6.4 What restrictions apply to the use of the aggregate functions within the SELECT statement? How do nulls affect
the aggregate functions?
An aggregate function can be used only in the SELECT list and in the HAVING clause.
Apart from COUNT(*), each function eliminates nulls first and operates only on the remaining non-null values.
COUNT(*) counts all the rows of a table, regardless of whether nulls or duplicate values occur.
6.5 How can results from two SQL queries be combined? Differentiate how the INTERSECT and EXCEPT
commands work.
There are number of relational operators that can be used to combine results of two queries. Some of them are
UNION, INTERSECT, and EXCEPT. For the two results to be semanticaly combined, order and number of
columns from both results should be identical, and the combining columns should be of the same domain.
The difference between the two is well explained in section 6.3.9 where the intersect returns the rows that are
common to both result sets, and except returns rows that are available in the first result set and not in the second
result set.
6.6 Differentiate between the three types of subqueries. Why is it important to understand the nature of a subquery
result before you write an SQL statement?
The subquery types are scalar, row, and table subquery. The main difference is on the nature of results they
return; scalar subquery returns a single column and single row that is a single value, row subquery returns
multiple columns and single row, while table subquery returns multiple columns and multiple rows. It is so
import to understand the nature of subquery result so that you can make the best choice about the operator to be
used
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.
SELECT * FROM Room WHERE price < 40 AND type IN (‘D’, ‘F’)
ORDER BY price;
6.11 List the bookings for which no dateTo has been specified.
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.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.
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
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 <= DATE’2004-08-01’ AND
dateTo >= DATE’2004-08-01’) OR
(dateFrom >= DATE’2004-08-01’ AND
dateFrom <= DATE’2004-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
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 Demonstrate that queries written using the UNION operator can be rewritten using the OR operator to
produce the same result.
Hint: students should explain a query that can be written in two ways by using UNION and OR operators.
Description of the data insert can be found in section 6.3.10. Studenst should simulate data entry by using the
provided syntax
Case Study 2
For Exercises 6.32–6.40, use the Projects schema defined in the Exercises at the end of Chapter 5.
6.32 List all employees from BRICS countries in alphabetical order of surname.
SELECT * FROM Employee WHERE address LIKE ‘%Brazil%’ OR address LIKE ‘%Rusia%’ OR
address LIKE ‘%India%’ OR address LIKE ‘%China%’ OR address LIKE ‘%South Africa%’
ORDER BY lName, fName;
6.34 List all managers who are female in alphabetical order of surname, and then first name.
SELECT * FROM Employee where position=‘Manager’ and sex=‘F’ ORDER BY lName, fName;
6.35 Remove all projects that are managed by the planning department.
6.36 Assume the planning department is going to be merged with the IT department. Update employee records
to reflect the proposed change.
UPDATE employee
SET deptNO=”IT”
WHERE deptNo=”PL”;
6.37 Using the UNION command, list all projects that are managed by the IT and the HR department.
6.38 Produce a report of the total hours worked by each female employee, arranged by department number and
alphabetically by employee surname within each department.
SELECT e.lName, e.fName, hoursWorked
FROM WorksOn w, Employee e, Department d
WHERE e.deptNo = d.deptNo
AND e.empNo = w.empNo
AND e.sex=’F’
ORDER by d.deptNo, e.lName;
6.39 Remove all project from the database on which no employees worked.
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.
Case Study 3
For Exercises 6.41–6.54, use the Library schema defined in the Exercises at the end of Chapter 5.
6.43 List all books titles published between 2010 and 2014.
SELECT *
FROM book
WHERE year BETWEEN ‘2010’ AND ‘2014’;
6.44 Remove all books published before 1950 from the database.
DELETE FROM book
WHERE year < ‘1950’;
6.45 List all book titles that have never been borrowed by any borrower.
SELECT *
FROM BOOK
WHERE ISBN NOT IN (SELECT ISBN
FROM BOOKCOPY C, BOOKLOAN L
WHERE L.copyno=C.copyno);
6.46 List all book titles that contain the word ‘database’ and are available for loan.
SELECT borrowerName
From Borrower bw, BookLoan bl
WHERE bw.borrowerNo = bl.borrowerNo and dateDue > today’s date;
SELECT COUNT(copyNo)
FROM BookCopy
WHERE available = ‘Y’ AND ISBN = ‘0-321-52306-7’;
6.50 How many times has the book with ISBN “0-321-52306-7” been borrowed?
SELECT COUNT(*)
FROM BookCopy bc, BookLoan bl
WHERE bc.copyNo = bl.copyNo AND ISBN = ‘0-321-52306-7’;
6.51 Produce a report of book titles that have been borrowed by “Peter Bloomfield”.
6.52 For each book title with more than 3 copies, list the names of library members who have borrowed them.
6.53 Produce a report with the details of borrowers who currently have books overdue.
6.54 Produce a report detailing how many times each book title has been borrowed.