233k views
2 votes
(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 prior to "September 1, 2018", retrieve the book title, the borrower’s name, and the borrower’s address. (e) For each book authored (or coauthored) by Stephen King, retrieve the title and the number of copies owned by the library branch whose name is "Central". (f) Retrieve all book titles that are available in "Sharpstown" branch but not in "Morristown" branch. (g) Retrieve book titles that are available in all library branches in the database.

User Gotiasits
by
5.6k points

2 Answers

5 votes

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".

(a) How many copies of the book titled "The Lost Tribe" are owned by the-example-1
(a) How many copies of the book titled "The Lost Tribe" are owned by the-example-2
(a) How many copies of the book titled "The Lost Tribe" are owned by the-example-3
(a) How many copies of the book titled "The Lost Tribe" are owned by the-example-4
User Paul Lalonde
by
6.3k points
5 votes

Answer:

a) SELECT No_of_copies

FROM ((book JOIN book_copies) JOIN library_branch)

WHERE title = 'The Lost Tribe' AND branchname = 'Sharpstown';

b) SELECT branchname, No_of_copies

FROM ((book JOIN book_copies) JOIN library_branch)

WHERE title = 'The Lost Tribe'

GROUP BY branchname;

c) SELECT Name

FROM borrowers B

WHERE cardno NOT IN ( SELECT cardno FROM book_loan)

d) SELECT title, name, B.address

FROM (((book JOIN book_loan) JOIN library_branch) JOIN borrowers B)

WHERE duedate = '09/01/2018' AND branchname = 'Sharpstown';

e) SELECT title, No_of_copies

FROM (((book JOIN book_authors) JOIN book_copies) JOIN library_branch)

WHERE authorname = 'Stephen King' AND branchname = 'Central';

g) SELECT DISTINCT title

FROM ((book JOIN book_copies) JOIN library_branch)

Step-by-step explanation:

The SQL statements query's a book store database of seven relations using joins and subqueries to return results.

User Jesantana
by
6.0k points