Assignment 6
Assignment 6
Tables:
1. Creation of Database
Database changed
2. Create Tables
name VARCHAR(100),
city VARCHAR(50)
);
customer_id INT,
order_date DATE,
total_amount DECIMAL(10,2),
product_name VARCHAR(100),
category VARCHAR(50),
price DECIMAL(10,2),
seller_id INT
);
order_id INT,
product_id INT,
quantity INT,
subtotal DECIMAL(10,2),
);
seller_name VARCHAR(100),
city VARCHAR(50)
);
('Flipkart', 'Mumbai'),
('Myntra', 'Delhi'),
('Snapdeal', 'Chennai'),
(1, 1, 1, 15000.00),
(2, 2, 1, 55000.00),
(3, 3, 1, 2500.00),
(4, 4, 1, 18000.00),
(5, 5, 1, 3000.00);
1. Retrieve all customers along with their corresponding orders (including customers who
haven't ordered).
Ans :-
2. List all orders along with the product names and their quantities.
Ans :-
Ans :-
Ans :-
5. Retrieve the order details, including customer name and total amount, for all orders placed in
the last 30 days.
Ans :-
6. Retrieve the names of sellers who sell a specific product (e.g., "Laptop").
Ans :-
Ans :-
mysql> SELECT c.customer_id, c.name, c.email, c.city FROM Customers c LEFT JOIN Orders o ON
c.customer_id = o.customer_id WHERE o.order_id IS NULL;
8. Retrieve details of orders where the total amount is greater than the average order total. (Using
subquery)
Ans :-
SELECT order_id, customer_id, total_amount FROM Orders WHERE total_amount > (SELECT
AVG(total_amount) FROM Orders);
9. Find customers who have placed at least one orders.
Ans :-
10. Find the top 3 most ordered products based on quantity sold.
Ans :-
11. Display the product names that are sold by single or multiple sellers.
Ans :-
Ans :-
mysql> SELECT s.seller_id, s.seller_name, s.city FROM Sellers s LEFT JOIN Products p ON s.seller_id =
p.seller_id WHERE p.product_id IS NULL;
Ans :-
mysql> SELECT p.product_id, p.product_name, p.category, p.price FROM Products p LEFT JOIN
Order_Items oi ON p.product_id = oi.product_id WHERE oi.order_item_id IS NULL;
14. Retrieve the names of customers who have placed the highest number of orders.
Ans :-
);
15. Find all customers who have ordered more than 1 different products.
Ans :-
16. Find products that are sold by at least two different sellers but have never been ordered.
Ans :-
mysql> SELECT p.product_id, p.product_name FROM Products p JOIN (SELECT product_id FROM
Products GROUP BY product_id HAVING COUNT(DISTINCT seller_id) > 1) AS multi_seller ON
p.product_id = multi_seller.product_id WHERE NOT EXISTS (SELECT 1 FROM Order_Items oi WHERE
oi.product_id = p.product_id);
17. Find the customer who has spent the most money overall.
Ans :-
18. Find all customers who have either placed an order or live in the same city as a seller.
Ans :-
SELECT c.customer_id, c.name, c.city FROM Customers c WHERE c.customer_id IN (SELECT DISTINCT
customer_id FROM Orders) UNION SELECT c.customer_id, c.name, c.city FROM Customers c JOIN
Sellers s ON c.city = s.city;
19. Retrieve all products that are either in stock with at least one seller or have been ordered at
least once.
Ans :-
20. Retrieve products that have been both ordered and are currently in stock.
Ans :-
Ans :-
SELECT customer_id, name, city FROM Customers WHERE customer_id IN (SELECT DISTINCT
customer_id FROM Orders) AND city IN (SELECT DISTINCT city FROM Sellers);
22. Retrieve all customers who have placed at least one order in each year available in the
database.
Ans :-
SELECT c.customer_id, c.name FROM Customers c WHERE NOT EXISTS ( SELECT DISTINCT
YEAR(o1.order_date) FROM Orders o1 WHERE NOT EXISTS (SELECT 1 FROM Orders o2 WHERE
o2.customer_id = c.customer_id AND YEAR(o2.order_date) = YEAR(o1.order_date) ));