0% found this document useful (0 votes)
4 views5 pages

Dbms Exercise 2

The document outlines a series of SQL commands to create a sales database, including tables for customers, products, and sales, along with various insert statements. It also provides SQL queries to retrieve specific information, such as customers who bought products in a certain category, the most expensive product purchased by each customer, and the total sales amount for each customer. Additionally, it includes queries to find customers who purchased more than five products and products that have never been sold.

Uploaded by

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

Dbms Exercise 2

The document outlines a series of SQL commands to create a sales database, including tables for customers, products, and sales, along with various insert statements. It also provides SQL queries to retrieve specific information, such as customers who bought products in a certain category, the most expensive product purchased by each customer, and the total sales amount for each customer. Additionally, it includes queries to find customers who purchased more than five products and products that have never been sold.

Uploaded by

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

DBMS EXERCISE-2

CREATE DATABASE sales;


USE sales;
CREATE TABLE Customers(CustomerId INT PRIMARY KEY,Customername
VARCHAR(15),Customeremail VARCHAR(20),Customercity VARCHAR(20));
INSERT INTO Customers(CustomerId,Customername,Customeremail,Customercity)
VALUES(1,'Ram','ram@123','Delhi'),(2,'Ken','ken@324','Coimbatore'),
(3,'Riya','riya@121','Chennai'),(4,'Anu','anu@376','Kochi'),(5,'Jay','jay@154','Hyderabad'),
(6,'Sneha','sneha@195','Pune');
CREATE TABLE Products(ProductID INT PRIMARY KEY,Productname VARCHAR(15),Category
VARCHAR(15),Price INT);
INSERT INTO Products(ProductID,Productname,Category,Price)
VALUES(101,'Smartphone','Electronics',20000),
(102, 'Laptop', 'Electronics', 50000),
(103, 'Vacuum Cleaner', 'Home Appliances', 19999),
(104, 'Blender', 'Home Appliances', 4999),
(105, 'Tablet', 'Electronics', 3999),
(106, 'Headphones', 'Electronics', 899),
(107,'TV','Electronics',32000);
CREATE TABLE Sales (
SaleID INT PRIMARY KEY,
CustomerID INT,
ProductID INT,
SaleDate DATE,
Quantity INT NOT NULL,
TotalAmount DECIMAL(10, 2) NOT NULL,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID),
FOREIGN KEY (ProductID) REFERENCES Products(ProductID)
);
INSERT INTO Sales (SaleID, CustomerID, ProductID, SaleDate, Quantity, TotalAmount)
VALUES
(1001, 1, 101, '2024-12-01', 1, 20000.00),
(1002, 2, 102, '2024-12-02', 1, 50000.00),
(1003, 3, 103, '2024-12-03', 2, 19999.00),
(1004, 1, 106, '2024-12-04', 1, 899.00),
(1005, 2, 105, '2024-12-05', 1, 3999.00),
(1006, 4, 104, '2024-12-06', 3, 4999.00);
1. Find Customers Who Have Bought Products in a Specific Category
Write a query to display the names of customers who have purchased products in the
'Electronics' category.
SELECT CustomerName FROM Customers WHERE CustomerID IN (
SELECT CustomerID FROM Sales WHERE ProductID IN (
SELECT ProductID FROM Products WHERE Category = 'Electronics')
);

2. Find the Most Expensive Product Purchased by Each Customer


Write a query to find the most expensive product purchased by each customer. Display the
CustomerName, ProductName, and Price.
SELECT c.CustomerName, p.ProductName, p.Price
FROM Customers c
JOIN (
SELECT s.CustomerID, s.ProductID
FROM Sales s
WHERE s.ProductID IN (
SELECT p1.ProductID
FROM Products p1
WHERE p1.Price = (
SELECT MAX(p2.Price)
FROM Sales s2
JOIN Products p2 ON s2.ProductID = p2.ProductID
WHERE s2.CustomerID = s.CustomerID)
)
) maxProducts ON c.CustomerID = maxProducts.CustomerID
JOIN Products p ON maxProducts.ProductID = p.ProductID;

3. Find Customers Who Have Purchased More Than 5 Products in Total

Write a query to display the CustomerName of customers who have purchased more than 5
different products.
SELECT c.CustomerName
FROM Customers c
WHERE c.CustomerID IN (
SELECT s.CustomerID
FROM Sales s
GROUP BY s.CustomerID
HAVING COUNT(DISTINCT s.ProductID) > 5
);
4. Find Products That Have Never Been Purchased
Write a query to display the names of products that have never been sold.

SELECT p.ProductName
FROM Products p
WHERE p.ProductID NOT IN (
SELECT s.ProductID
FROM Sales s
);

5. Find the Total Sales Amount for Each Customer


Write a query to calculate the total amount spent by each customer and display the
CustomerName and their total expenditure.

SELECT c.CustomerName, (
SELECT SUM(s.TotalAmount)
FROM Sales s
WHERE s.CustomerID = c.CustomerID
) AS TotalExpenditure
FROM Customers c;
6. Find the Customer Who Has Spent the Most
Write a query to find the CustomerName of the customer who has spent the most amount
of money in the shopping mart.
SELECT c.CustomerName
FROM Customers c
WHERE c.CustomerID = (
SELECT s.CustomerID
FROM Sales s
GROUP BY s.CustomerID
ORDER BY SUM(s.TotalAmount) DESC
LIMIT 1);

You might also like