SQL Exercises
SQL Exercises
For each of the queries below, write an SQL query to retrieve the data from the “YADKIN”
database using a query that will work in at least one of MySQL or ORACLE (but not
necessarily both). You should submit a single printed paper with your SQL queries clearly
numbered. Each query should be prefixed with a very brief statement of what you believe
your query does. For example: “This query uses a natural join to relate books and authors.
It then selects those authors who have written more than 10 books.”
The schema for the “YADKIN” database can be found on the attached page. For each of
the following questions, write an SQL query that will execute correctly with either MySQL
or ORACLE:
a. List the titles of all books published by a publisher located in New York state
(‘NY’).
b. List the titles of the books with exactly one author.
c. Find the average price of all the paperback books that are included in the book
table.
d. List the titles of the books in the database that are available in both paperback and
hardback form. (The value of PAPERBACK can be ‘Y’ or ‘N’ or NULL. If
PAPERBACK is ‘Y’ then the book is a paperback. If the value is ‘N’ it’s a
hardback. If it’s NULL then we don’t know.)
e. List the names of the books that are in inventory (UNITS_ON_HAND > 0) at all
branches.
f. List the name(s) of the branch with the smallest number of employees (might not
be unique).
g. List the name of every branch along with the total value of the inventory at that
branch.
h. List the name of the branch with the highest inventory value (do not use a
constant in your query).
i. In a single table, list the name of every branch along with a list of all the
publishers (from publisher) and the number of books from that publisher that the
branch has in inventory, even if it is zero. HINT: If there are 5 branches and
10 publishers in the database, the resulting table should contain 5 x 10 = 50 rows.
j. List the name of every branch along with the name of the most expensive book
currently in inventory at that branch.
--script for creating yadkin database in MySQL
/*
* Note that the database name is all caps but all table names are lower case
*/
use YADKIN;