Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the LIBRARY relational schema shown in Figure 6.14 on page 189, which i

ID: 3702189 • Letter: C

Question

Consider the LIBRARY relational schema shown in Figure 6.14 on page 189, which is used to keep track of books, borrowers, and book loans. Write down SQL expressions for the following queries on the LIBRARY database:

(a) How many copies of the book titled The Lost Tribe are owned by the library branch whose name is "Sharpstown"?

(b) How many copies of the book titled The Lost Tribe are owned by each library branch?

(c) Retrieve the names of all borrowers who do not have any books checked out.

(d) For each book that is loaned out from the "Sharpstown" branch and whose DueDate is today, retrieve the book title, the borrower's name, and the borrower's address.

(e) For each library branch, retrieve the branch name and the total number of books loaned out from that branch.

(f) Retrieve the names, addresses, and number of books checked out for all borrowers who have more than five books checked out.

(g) For each book authored (or co-authored) by "Stephen King", retrieve the title and the number of copies owned by the library branch whose name is "Central".

BOOK Bookidl Title LPublisher_name | BOOK AUTHORS Book id Author name PUBLISHER Name Address Phone BOOK COPIES Book id Branch id No of copies BOOK LOANS Book d Branch id Card no Date, out Due date LIBRARY BRANCH Branch id Branch name Address BORROWER Card no Name Address Phone

Explanation / Answer

/*(a) How many copies of the book titled The Lost Tribe are owned by the library branch whose name is "Sharpstown"?*/

SELECT COUNT(Book_id) AS CountOfLostTribe FROM Book WHERE Book_id IN (

SELECT Book_id FROM Book_Copies WHERE Brach_ID IN (

(SELECT Brach_ID FROM LIBRARY_Branch WHERE Branch_name='Sharpstown')))

AND Title='The Lost Tribe';

/*(b) How many copies of the book titled The Lost Tribe are owned by each library branch?*/

SELECT l.Branch_name,COUNT(b.Book_id) AS CountByBanch FROM Book b INNER JOIN Book_Copies bc ON b.Book_id=bc.Book_id

INNER JOIN LIBRARY_Branch l ON bc.Brach_ID=l.Brach_ID

WHERE b.Title='The Lost Tribe'

GROUP BY l.Brach_ID;

/*(c) Retrieve the names of all borrowers who do not have any books checked out.*/

SELECT Name FROM Borrower WHERE Card_No NOT IN (SELECT Card_No FROM Book_Loans);

/*(d) For each book that is loaned out from the "Sharpstown" branch and whose DueDate is today,

retrieve the book title, the borrower's name, and the borrower's address.*/

SELECT b.title,bor.Name,bor.Address FROM Book b INNER JOIN Book_Loans bc ON b.Book_id=bc.Book_id

INNER JOIN LIBRARY_Branch l ON bc.Brach_ID=l.Brach_ID

INNER JOIN Borrower bor ON bc.Card_No=bor.Card_No

WHERE l.Branch_name='Sharpstown' AND bc.Due_Date='Today'/*if due date is in format of date please give '07-04-2018'*/

/*(e) For each library branch, retrieve the branch name and the total number of books loaned out from that branch.*/

SELECT l.Branch_name,COUNT(bc.Brach_ID) FROM LIBRARY_Branch l INNER JOIN Book_Loans bc ON l.Brach_ID=bc.Brach_ID

GROUP BY l.Branch_name;

/*(f) Retrieve the names, addresses, and number of books checked out for all borrowers who have more than five books checked out.*/

SELECT B.CardNo, B.Name, B.Address, COUNT(bc.Book_id)

FROM BORROWER B INNER JOIN BOOK_LOANS bc ON B.CardNo = bc.CardNo

GROUP BY B.CardNo HAVING COUNT(bc.Book_id) > 5;

/*(g) For each book authored (or co-authored) by "Stephen King",

retrieve the title and the number of copies owned by the library branch whose name is "Central".*/

SELECT b.title, COUNT(bc.Book_id) AS NumberOfCopies FROM Book_Authors a INNER JOIN Book b ON a.Book_id=b.book_id

INNER JOIN Book_Copies bc ON b.Book_id=bc.Book_id

INNER JOIN LIBRARY_Branch l ON bc.Brach_ID=l.Brach_ID

WHERE a.Author_Name='Stephen King'

GROUP BY bc.Brach_ID;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote