0% found this document useful (0 votes)
276 views

Lab Chapter # 11

The document provides instructions for a lab assignment on querying a book database using SQL. Students are asked to complete 10 queries to retrieve information from the database such as the number of books in different categories, the most recent publication date, and profits from customer orders. An additional challenge asks students to identify books that are less profitable by generating a report of books purchased that return less than a 55% profit.

Uploaded by

Kovid Behl
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
276 views

Lab Chapter # 11

The document provides instructions for a lab assignment on querying a book database using SQL. Students are asked to complete 10 queries to retrieve information from the database such as the number of books in different categories, the most recent publication date, and profits from customer orders. An additional challenge asks students to identify books that are less profitable by generating a report of books purchased that return less than a 55% profit.

Uploaded by

Kovid Behl
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Lab Chapter # 11

Total Marks: 10

Student Name : KOVID BEHL____________________________________________________

Student Id #: N01579154______________________________________________________

Lab exercises You need to be present in the class for your lab to be graded and are to be
submitted within 3 Hours after the class, failing to do the will result in ZERO grade.

Hands-On Assignments
To perform these assignments, refer to the tables in the JustLee Books database.

1. Determine how many books are in the Cooking category.


SELECT COUNT(*)
FROM books
WHERE category = 'COOKING';

2. Display the number of books with a retail price of more than $30.00.
SELECT COUNT(*)
FROM books
WHERE retail > 30;
3. Display the most recent publication date of all books sold by JustLee Books.
SELECT MAX(pubdate)
FROM books;

4. Determine the total profit generated by sales to customer 1017. Note:


Quantity should be reflected in the total profit calculation.
SELECT SUM((retail-cost)*quantity)
FROM orders JOIN orderitems USING(order#) JOIN books USING(isbn)
WHERE customer# = 1017;

5. List the retail price of the least expensive book in the Computer category.
SELECT MIN(retail)
FROM books
WHERE category = 'COMPUTER';
6. Determine the average profit generated by orders in the ORDERS table.
Note: The total profit by order must be calculated before finding the average
profit.

SELECT AVG(SUM((retail-cost)*quantity))
FROM orders JOIN orderitems USING(order#) JOIN books USING(isbn)
GROUP BY order#;

7. Determine how many orders have been placed by each customer. Do not
include in the results any customer who hasn’t recently placed an order with
JustLee Books.
SELECT customer#, COUNT(*)
FROM orders
GROUP BY customer#;
8. Determine the average retail price of books by publisher name and category.
Include only the categories Children and Computer and the groups with an
average retail price greater than $50.
SELECT name, category, AVG(retail)
FROM books JOIN publisher USING(pubid)
WHERE category IN('COMPUTER', 'CHILDREN')
GROUP BY name, category;
9. List the customers living in Georgia or Florida who have recently placed an
order totaling more than $80.
SELECT DISTINCT firstname, lastname
FROM customers JOIN orders USING(customer#) JOIN orderitems
USING(order#) JOIN books USING(isbn)
WHERE (state = 'FL' or state = 'GA')
GROUP BY order#, firstname, lastname
HAVING SUM(retail*quantity)>80;
10.What’s the retail price of the most expensive book written by Lisa White?
SELECT MAX(retail)
FROM books JOIN bookauthor USING(isbn) JOIN author USING(authorid)
WHERE lname = 'WHITE'
AND fname = 'LISA';

Advanced Challenge
To perform this activity, refer to the tables in the JustLee Book database.

JustLee Books has a problem: Book storage space is filling up. As a solution,
management is considering limiting the inventory to only those books returning at
least a 55% profit. Any book returning less than a 55% profit would be dropped
from inventory and not reordered.

This plan could, however, have a negative impact on overall sales. Management
fears that if JustLee stops carrying the less profitable books, the company might
lose repeat business from its customers. As part of management’s decision-making
process, it wants to know whether current customers purchase less profitable books
frequently. Therefore, management wants to know how many times these less
profitable books have been purchased recently.
select b.isbn, b.title, b.retail, b.cost, oi.quantity, ((b.retail-b.cost)/b.cost)*100 as
Profit from books b
join orderitems oi on b.isbn = oi.isbn
where ((b.retail-b.cost)/b.cost)*100 < 55
order by Profit;

You might also like