Program 6:
Consider the following tables,
Sales (Sale_id, PrdID, Qty_Sold, Sale_date)
Products( PrdID, Pname, Category, U_price)
Create table Products (
PrdID int primary Key,
Pname varchar(20) not null,
Category varchar(20) not null,
U_price decimal (10,2) not null);
Create table Sales (
Sale_ID int,
PrdID int,
Qty_Sold int not null,
Sale_date date not null,
Primary Key(Sale_id, PrdID),
Foreign key (PrdID) references Products(PrdID);
Write the queries for the following:
a. Count Sales Per Day from the Sales table in ascending order as per date.
SELECT sale_date, SUM (Qty_Sold) AS sales_count
FROM Sales
GROUP BY sale_date
ORDER BY sale_date;
b. Calculate the total revenue generated from sales of products in the 'Electronics'
category.
SELECT SUM(Sales.Q_Sold * P.Uprice) AS total_revenue
FROM Sales S, Products P
Where Sales.product_id = Products.product_id and
Products.category = 'Electronics';
c. Retrieve the product_name and total_price from the Sales table, calculating the
total_price as quantity_sold multiplied by unit_price.
SELECT product_name, SUM(quantity_sold * unit_price) AS total_price
FROM Sales
JOIN Products ON Sales.product_id = Products.product_id
Group by Products.Pname;
d. Find the Products Not Sold from Products table.
SELECT product_id, product_name
FROM Products
WHERE product_id NOT IN (SELECT DISTINCT product_id FROM Sales);
e. Identify products with total sales exceeding 30.
SELECT p.product_name, Sum(Qty_Sold) as “Total Sales”
FROM Sales s, Products P
Where s.product_id = P.product_id
GROUP BY p.product_name
HAVING SUM(s.Qty_sold) > 30;
Program 8:
Consider the following tables,
Books( BookID, Title, Author, Genre, Year, Price)
Borrowers (BorrowerID, Name, BookID,BorrowDate, ReturnDate)
Create table Books (
bookID int Primary Key,
Title varchar (20) not null,
Author varchar(20) not null,
Genre varchar(20) not null,
Year year not null,
Price decimal(10,L2) not null);
Create table Borrowers (
BrID int ,
Name char(20) not null,
bookID int,
Brwdate date not null,
Returndate date not null,
Primary Key (BrID, bookID),
Foreign Key(bookID) references Books(bookID),
Check(Brwdate< Returndate));
Write the queries for the following:
a. List all borrowers and the books they have borrowed:
SELECT br.BorrowerID, br.Name, bk.Title, bk.Author, br.BorrowDate, br.ReturnDate
FROM Borrowers br
JOIN Books bk ON br.BookID = bk.BookID;
b. Retrieve the details of books borrowed after January 15, 2025.
Select b.bookID, b.title, b.author, b.genre, b.year, b.price
From books b, borrowers br
Where b.bookid = br.bookid and
Br.borrowdate> ‘2025-01-15’;
c. Retrieve all overdue books
SELECT br.BorrowerID, br.Name, bk.Title, bk.Author, bk.Genre, br.BorrowDate, br.ReturnDate
FROM Borrowers br
JOIN Books bk ON br.BookID = bk.BookID
WHERE br.ReturnDate < CURDATE();
d. List the most popular books (most borrowed):
SELECT bk.BookID, bk.Title, bk.Author, COUNT(br.BookID) AS BorrowCount
FROM Borrowers br
JOIN Books bk ON br.BookID = bk.BookID
GROUP BY bk.BookID
ORDER BY BorrowCount DESC;