You are on page 1of 2

CSC 321/621 – Fall 2009

SQL Practice Exercises

Due 10/2/2009 by Noon

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
a. List the titles of all books published by a publisher located in New York state
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
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
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
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

/* drop table if exists branch;*/

create table branch (branch_number char(1) PRIMARY KEY,
branch_name varchar(20) default NULL,
branch_location varchar(20) default NULL,
number_employees decimal(2,0) default NULL);

/* drop table if exists publisher; */

create table publisher (publisher_code char(2) PRIMARY KEY,
publisher_name varchar(20) default NULL,
publisher_city varchar(20) default NULL,
publisher_state char(2) default NULL);

/* drop table if exists author; */

/* author_number is like an author ID */
create table author (author_number char(2) PRIMARY KEY,
author_last varchar(20) default NULL,
author_first varchar(20) default NULL);

/* drop table if exists book; */

/* paperback can be ‘Y’, ‘N’, or NULL */
/* Think of the book_code as being like an ISBN. It may differ between printings
or editions of the same title. */
create table book (book_code char(5) PRIMARY KEY,
book_title varchar(30) default NULL,
publisher_code char(2) default NULL REFERENCES publisher,
book_type char(3) default NULL,
book_price decimal(5,2) default NULL,
paperback char(1) default NULL);

/* drop table if exists wrote; */

/* sequence_number relates to first author, second author, etc. */
create table wrote (book_code char(5) NOT NULL REFERENCES book,
author_number char(2) REFERENCES author,
sequence_number decimal(1,0) DEFAULT 1,
PRIMARY KEY(book_code, author_number));

/* drop table if exists invent; */

/* units_on_hand is the number of copies of a book in inventory */
create table invent (book_code char(5) NOT NULL REFERENCES book,
branch_number char(1) NOT NULL REFERENCES branch,
units_on_hand decimal(2,0),
PRIMARY KEY(book_code, branch_number));