Assignment Week 4 by Sanjib Kumar Shil
Assignment Week 4 by Sanjib Kumar Shil
Answer:
Thus, the above query is to display the number of different patrons who have ever checked out a book.
Ans:
87. Write a query to display the patron ID, book number, 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. Sort the
results by days kept in descending order, then by patron ID, and then by book number (Figure P7.87). (68
rows)
SELECT
CHECKOUT.Pat_ID AS Patron_ID,
CHECKOUT.Book_Num AS Book_Number,
DATEDIFF(day, CHECKOUT.Check_Out_Date, CHECKOUT.Check_In_Date) AS
Days_Kept
FROM
CHECKOUT
ORDER BY
Days_Kept DESC,
Patron_ID,
Book_Number;
89. Write a query to display the book number, title with year, and subject for each book. Sort the resultsby
the book number (Figure P7.89). (20 rows)
Ans:
Complete SQL query for above:
91. Write a query to display the author ID, book number, title, and subject for each book. Sort the resultsby
book number and then author ID (Figure P7.91). (25 rows)
Ans:
93. Write a query to display the patron ID, book number, patron first name and last name, and book titlefor
all currently checked out books. (Remember to use the redundant relationship described in the assignment
instructions for current checkouts.) Sort the output by patron last name and book title (Figure P7.93).
Ans:
95. Write a query to display the book number and the number of times each book has been checked out.
Do not include books that have never been checked out. Sort the results by the number of timeschecked
out in descending order and then by book number in descending order (Figure P7.95).
Ans:
SELECT Book_Num, COUNT(*) AS Checkout_Count
FROM Checkout
WHERE Checkout_In_Date IS NULL
GROUP BY Book_Num
ORDER BY Checkout_Count DESC, Book_Num DESC;
97. Write a query to display the book number, title, author last name, author first name, patron ID, lastname,
and patron type for all books currently checked out to a patron. Sort the results by book title (Figure P7.97).
Ans:
To display the required information for all books currently checked out to patrons, and sort the results by
book title, we'll need to join the tables that hold the book and patron information. Assuming the tables are
named "Books", "Authors", and "Patrons", and they are connected via appropriate foreign keys, the SQL
query would look like this:
SELECT
Books.BookNumber,
Books.Title,
Authors.LastName AS AuthorLastName,
Authors.FirstName AS AuthorFirstName,
Patrons.PatronID,
Patrons.LastName AS PatronLastName,
Patrons.PatronType
FROM
Books
JOIN
Authors ON Books.AuthorID = Authors.AuthorID
JOIN
Patrons ON Books.PatronID = Patrons.PatronID
WHERE
Books.IsCheckedOut = true
ORDER BY
Books.Title;
99. Write a query to display the book number, title, and number of times each book has been checkedout.
Limit the results to books that have been checked out more than five times. Sort the results in
descending order by the number of times checked out and then by title (Figure P7.99).
Ans:
From the given statement the SQL query can be below: -
Ans:
Use CUST_NUM as the primary key.To create the CUSTOMER table structure, you can use the
followingSQL statement:
17. Create the INVOICE table structure illustrated in Figure P8.16. The invoice number should store
integer values. The invoice date should store date values. The invoice amount should support up to 8digits
to the left of the decimal place and two digits to the right of the decimal place.
Ans:
Use INV_NUM as the primary key. Note that the CUST_NUM is the foreign key to CUSTOMER, so be
certain to enforce referential integrity.
Ans:
Insert data into the CUSTOMER table
Ans:
Insert data into the INVOICE table
References: 01) Coronel, C., & Morris, S. (2018). Database systems: design, implementation, and
management
(13th ed.). Cengage Learning.
02) Beaulieu, A. Learning SQL: Master SQL Fundamentals, 2nd Edition. O'Reilly Media, 2009.
03) SQL and relational theory: how to write accurate SQL code Chris J Date" O'Reilly Media, Inc.", 2011