0% found this document useful (0 votes)
20 views7 pages

Assignment Week 4 by Sanjib Kumar Shil

Uploaded by

sanjib.job.bd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views7 pages

Assignment Week 4 by Sanjib Kumar Shil

Uploaded by

sanjib.job.bd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Assignment: Problem Set-Week 4

International American University


MIS 560 Database Management Systems
Professor: Prasanth Kalakota
Sanjib Kumar Shil
Date: 26th January 2024
CHAPTER 7
PROBLEM

Answer:

SELECT MAX(cost) AS "Most Expensive"


FROM books;

Ans: SELECT COUNT(DISTINCT PatronID) AS "DIFFERENT PATRONS"


FROM YourCheckoutTable;

Thus, the above query is to display the number of different patrons who have ever checked out a book.
Ans:

Select AUTHOR.AU_ID , Count(BOOK.BOOK_NUM) as "Books Written" from AUTHOR inner join


WRITES on AUTHOR.AU_ID = WRITES.AU_ID inner join BOOK on WRITES.BOOK_NUM =
BOOK.BOOK_NUM group
by AUTHOR.AU_ID order by Count(BOOK.BOOK_NUM) desc, AUTHOR.AU_ID;

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)

The query would be as follows:

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:

SELECT BOOK_NUM, CONCAT(BOOK, ' ', BOOK_YEAR) AS BOOK, BOOK_SUBJECT


FROM books
ORDER BY BOOK_NUM;

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:

SELECT AU_ID, BOOK.BOOK_NUM, BOOK_TITLE, BOOK_SUBJECT


FROM BOOK JOIN WRITES ON BOOK.BOOK_NUM = WRITES.BOOK_NUM
ORDER BY BOOK.BOOK_NUM, AU_ID;

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:

SELECT PatronID, Book_Num, Pat_FName, Pat_LName, Book_Title


FROM Patron
INNER JOIN Checkout ON Patron.PatronID = Checkout.PatronID
INNER JOIN Book ON Checkout.Book_Num = Book.Book_Num
WHERE Checkout_In_Date IS NULL
ORDER BY Pat_LName, Book_Title;

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: -

SELECT BOOK.BOOK_NUM, BOOK_TITLE, Count(CHECK_NUM) AS


"Times_Checked_Out"FROM BOOK JOIN CHECKOUT ON BOOK.BOOK_NUM =
CHECKOUT.BOOK_NUMGROUP BY BOOK.BOOK_NUM, BOOK_TITLEHAVING
Count(CHECK_NUM) > 5 ORDER BY Count(CHECK_NUM) DESC, BOOK_TITLE;
CHAPTER 8
PROBLEM
16. Create the CUSTOMER table structure illustrated in Figure P8.16. The customer number should store
integer values. The name attributes should support variable length character data up to 30 characters
each. The customer balance should support up to six digits on the left of the decimal place and twodigits
to the right of the decimal place.

Ans:
Use CUST_NUM as the primary key.To create the CUSTOMER table structure, you can use the
followingSQL statement:

CREATE TABLE CUSTOMER


( CUST_NUM INT PRIMARY KEY,
CUST_LNAME VARCHAR(30),
CUST_FNAME VARCHAR(30),
CUST_BALANCE DECIMAL(10, 2)
);

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.

Create the INVOICE table with a foreign key to CUSTOMER

CREATE TABLE INVOICE


( INV_NUM INT PRIMARY
KEY,CUST_NUM INT,
INV_DATE DATE,
INV_AMOUNT DECIMAL(10, 2),
FOREIGN KEY (CUST_NUM) REFERENCES CUSTOMER(CUST_NUM)
);
18. Write the set of SQL commands necessary to insert the data into the CUSTOMER table you created in
Problem 16, as illustrated in Figure P8.16.

Ans:
Insert data into the CUSTOMER table

-- Sample data for CUSTOMER table


INSERT INTO CUSTOMER (CUST_NUM, CUST_LNAME, CUST_FNAME, CUST_BALANCE)
VALUES
(1, 'Smith', 'John', 1000.00),
(2, 'Johnson', 'Mary', 1500.50),
(3, 'Doe', 'Jane', 800.75),
-- Add more records as needed
;
19. Write the set of SQL commands necessary to insert the data into the INVOICE table you created in
Problem 17, as illustrated in Figure P8.16.

Ans:
Insert data into the INVOICE table

-- Sample data for INVOICE table


INSERT INTO INVOICE (INV_NUM, CUST_NUM, INV_DATE, INV_AMOUNT)
VALUES
(101, 1, '2023-09-21', 500.25),
(102, 2, '2023-09-22', 750.75),
(103, 3, '2023-09-23', 200.50),
-- Add more records as needed
;

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

You might also like