SQL 4
SQL 4
I recommend creating a new user and workspace named after your netid, log in as that user and load the
database script facts.sql (provided in this week's assignment folder). Before you attempt to write any SQL
queries, familiarize yourself with the database structure and data. I have provided a relational diagram
and sample data for this database.
Write queries to address each of the problems below. Submit both the SQL statements and the
screen prints of the outputs from Oracle. (2 points for the screen shots). Be sure the workspace
name is included in your screen shots!!!
Sara Irfan
Sara Irfan
1. Create a new table to track the Library location. Screen shot not required
(6pts)
Sara Irfan
3. There is a 1:M relationship between LIBRARY and BOOK. Alter the BOOK table,
add lib_id as a foreign key. (4 pts) Screen shot not required
Sara Irfan
ADD CONSTRAINT LIB_ID_FK FOREIGN KEY (LIB_ID) REFERENCES LIBRARY
(LIB_ID)
4. Update all the Checkout records by updating the year to 2016. (HINT: Use the
ADD_MONTHS function). (5 pts) Screen shot not required
UPDATE CHECKOUT
SET CHECK_OUT_DATE = ADD_MONTHS(CHECK_OUT_DATE, 12)
UPDATE CHECKOUT
SET CHECK_DUE_DATE = ADD_MONTHS(CHECK_DUE_DATE, 12)
UPDATE CHECKOUT
SET CHECK_IN_DATE = ADD_MONTHS(CHECK_IN_DATE, 12)
5. Due to inflation, the cost of all books published in 2014 will increase by 6%.
Write a single SQL command to increase all book costs by 6%. (4 pts) Screen
shot not required
UPDATE BOOK
SET BOOK_COST = BOOK_COST *1.06
WHERE BOOK_YEAR = '2014'
6. Write a query that will display all the Books that were published in 2016.
Display the book year, book title, book subject, author last name, and author
first. Sort the records by book title. The output should be formatted as follows
(Book Title (year), Book Subject, Author Name) see output below. (5 pts)
Sara Irfan
WHERE A.AU_ID = W.AU_ID AND W.BOOK_NUM = B.BOOK_NUM AND
BOOK_YEAR = 2016
ORDER BY BOOK_TITLE
7. Write a query to display the book title, book year, book subject , and book cost
for all books that are either Database or Programming books and that have a
replacement cost that is greater than $50. Sort the results in ascending order
by Subject then cost. (4 pts)
Sara Irfan
8. Write a query to show the min, max, and average replacement cost for all
books. (4 pts)
Sara Irfan
9. Write a query to display the book subject and the number of books as #
Books in each subject, order by the subject. (5 pts)
10.Write a query to display the patron type and the number of patrons as # of
Patrons in each type, order by the number of patrons in descending order.
What are some of the problems with the way the data is entered? How could
you avoid those problems at the DBMS level? (5 pts)
Sara Irfan
Sara Irfan
12.Which books have been checked out more than 7 times? Show the book_title,
bok_num and the # of times checked out. Show the query and the result set.
(6 pts)
10
Sara Irfan
13.Write a query to display books that have the word database in the title
regardless of how it is capitalized. (4 pts)
Sara Irfan
15.Which patrons have checked out more than 2 books? Display the patron name,
patron type, and number of books. (6 pts)
12
Sara Irfan
16.Write a query to display the patron id, book title, and days kept for each
checkout. Days Kept is the difference from the date on which the book is
returned to the date it was checked out. (6 pts)
13
Sara Irfan
17.Write a query to display all the books that are currently checked out. Display
the book title, patron last name, due date, and # of days remaining until it is
due. (6 pts)
14
Sara Irfan
18.Write a query that will display all the books that are overdue. Display the book
title, patron last name, due date, and days overdue. (6 pts)
B.BOOK_NUM
AND
19.Write a query that will which authors have written more than 2 books.
Display the author first name, last name, and # of Books Written. (6 pts)
15
Sara Irfan
20.Write a query that will list all the books that have never been checked out. List
the book number, book title and subject. (4 pts)
16
Sara Irfan
17