The SQL expressions for the following queries on the LIBRARY database:
(a)
SQL
SELECT COUNT(Copies)
FROM Book
JOIN BRANCH ON Book.BranchID = BRANCH.BranchID
WHERE Title = 'The Lost Tribe' AND BRANCH.BranchName = 'Sharpstown';
(b)
SQL
SELECT Title, BranchName, COUNT(Copies) AS NumCopies
FROM Book
JOIN BRANCH ON Book.BranchID = BRANCH.BranchID
WHERE Title = 'The Lost Tribe'
GROUP BY Title, BranchName;
(c)
SQL
SELECT BorrowerName
FROM Borrower
WHERE BorrowerID NOT IN (
SELECT BorrowerID
FROM BookLoan
);
(d)
SQL
SELECT b.Title, bl.BorrowerName, bl.BorrowerAddress
FROM Book b
JOIN BookLoan bl ON b.BookID = bl.BookID
WHERE bl.BranchID = 'Sharpstown' AND bl.DueDate = CURRENT_DATE();
(e)
SQL
SELECT BranchName, SUM(NumCopies) AS NumLoans
FROM BookLoan
JOIN BRANCH ON BookLoan.BranchID = BRANCH.BranchID
GROUP BY BranchName;
(f)
SQL
SELECT BorrowerName, BorrowerAddress, COUNT(BookID) AS NumCheckouts
FROM Borrower
JOIN BookLoan ON Borrower.BorrowerID = BookLoan.BorrowerID
GROUP BY BorrowerName, BorrowerAddress
HAVING NumCheckouts > 5;
(g)
SQL
SELECT b.Title, b.NumCopies
FROM Book b
JOIN Author a ON b.AuthorID = a.AuthorID
WHERE a.AuthorName = 'Stephen King' AND b.BranchID = 'Central';
See text below
QL EXERCISE Practice and practice we can improve our skills. Retrieve the required information using SQL language.Part I. Give a database schema for a library management system as the following picture.
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".