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

Assignment 6

The document outlines the creation of a database schema for an E-commerce system, including tables for Customers, Orders, Products, Order_Items, and Sellers. It provides SQL commands for creating the database, inserting data into the tables, and executing various SQL queries to retrieve and analyze data. The queries cover customer orders, product availability, seller information, and order statistics.

Uploaded by

Rohan
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)
76 views

Assignment 6

The document outlines the creation of a database schema for an E-commerce system, including tables for Customers, Orders, Products, Order_Items, and Sellers. It provides SQL commands for creating the database, inserting data into the tables, and executing various SQL queries to retrieve and analyze data. The queries cover customer orders, product availability, seller information, and order statistics.

Uploaded by

Rohan
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/ 11

Assignment 6

Consider the following database schema for an E-commerce system:

Tables:

Customers (customer_id, name, email, city)

Orders (order_id, customer_id, order_date, total_amount)

Products (product_id, product_name, category, price)

Order_Items (order_item_id, order_id, product_id, quantity, subtotal)

Sellers (seller_id, seller_name, city)

1. Creation of Database

mysql> create database Assignment_6;

Query OK, 1 row affected (0.01 sec)

mysql> use Assignment_6;

Database changed

2. Create Tables

mysql> CREATE TABLE Customers (

customer_id INT PRIMARY KEY AUTO_INCREMENT,

name VARCHAR(100),

email VARCHAR(100) UNIQUE,

city VARCHAR(50)

);

Query OK, 0 rows affected (0.06 sec)

mysql> CREATE TABLE Orders (

order_id INT PRIMARY KEY AUTO_INCREMENT,

customer_id INT,

order_date DATE,

total_amount DECIMAL(10,2),

FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)


);

Query OK, 0 rows affected (0.08 sec)

mysql> CREATE TABLE Products (

product_id INT PRIMARY KEY AUTO_INCREMENT,

product_name VARCHAR(100),

category VARCHAR(50),

price DECIMAL(10,2),

seller_id INT

);

Query OK, 0 rows affected (0.04 sec)

mysql> CREATE TABLE Order_Items (

order_item_id INT PRIMARY KEY AUTO_INCREMENT,

order_id INT,

product_id INT,

quantity INT,

subtotal DECIMAL(10,2),

FOREIGN KEY (order_id) REFERENCES Orders(order_id),

FOREIGN KEY (product_id) REFERENCES Products(product_id)

);

Query OK, 0 rows affected (0.07 sec)

mysql> CREATE TABLE Sellers (

seller_id INT PRIMARY KEY AUTO_INCREMENT,

seller_name VARCHAR(100),

city VARCHAR(50)

);

Query OK, 0 rows affected (0.02 sec)


3. Insert Into Tables

mysql> INSERT INTO Customers (name, email, city) VALUES

('Rohan Adkine', '[email protected]', 'Pune'),

('Aditya Gavali', '[email protected]', 'Mumbai'),

('Kunal Biradar', '[email protected]', 'Nagpur'),

('Adwait Deshpande', '[email protected]', 'Pune'),

('Rudrapratap Ghatge', '[email protected]', 'Delhi');

Query OK, 5 rows affected (0.03 sec)

Records: 5 Duplicates: 0 Warnings: 0

mysql> INSERT INTO Sellers (seller_name, city) VALUES

('Amazon India', 'Bangalore'),

('Flipkart', 'Mumbai'),

('Myntra', 'Delhi'),

('Snapdeal', 'Chennai'),

('Reliance Digital', 'Pune');

Query OK, 5 rows affected (0.02 sec)

Records: 5 Duplicates: 0 Warnings: 0

mysql> INSERT INTO Products (product_name, category, price, seller_id) VALUES

('Smartphone', 'Electronics', 15000.00, 1),

('Laptop', 'Electronics', 55000.00, 2),

('Shoes', 'Fashion', 2500.00, 3),

('Washing Machine', 'Appliances', 18000.00, 4),

('Headphones', 'Electronics', 3000.00, 5);

Query OK, 5 rows affected (0.02 sec)

Records: 5 Duplicates: 0 Warnings: 0

mysql> INSERT INTO Orders (customer_id, order_date, total_amount) VALUES

(1, '2025-03-01', 15000.00),

(2, '2025-03-02', 55000.00),


(3, '2025-03-03', 2500.00),

(4, '2025-03-04', 18000.00),

(5, '2025-03-05', 3000.00);

Query OK, 5 rows affected (0.02 sec)

Records: 5 Duplicates: 0 Warnings: 0

mysql> INSERT INTO Order_Items (order_id, product_id, quantity, subtotal) VALUES

(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);

Query OK, 5 rows affected (0.02 sec)

Records: 5 Duplicates: 0 Warnings: 0

Solve following SQL Queries:

1. Retrieve all customers along with their corresponding orders (including customers who

haven't ordered).

Ans :-

select c.customer_id, c.name, c.email, c.city, o.order_id, o.order_date, o.total_amount

from Customers c left join Orders o ON c.customer_id = o.customer_id;

2. List all orders along with the product names and their quantities.

Ans :-

select o.order_id, p.product_name, oi.quantity from Orders o join Order_Items oi on o.order_id =


oi.order_id join Products p ON oi.product_id = p.product_id;
3. Find the total number of orders placed by each customer.

Ans :-

select c.customer_id, c.name, count(o.order_id) AS total_orders from Customers c

left join Orders o ON c.customer_id = o.customer_id group by c.customer_id, c.name;

4. Find the total number of products available in each category.

Ans :-

select category, count(product_id) AS total_products from Products group by category;

5. Retrieve the order details, including customer name and total amount, for all orders placed in
the last 30 days.
Ans :-

SELECT o.order_id, c.name, o.total_amount, o.order_date FROM Orders o JOIN Customers c ON


o.customer_id = c.customer_id WHERE o.order_date >= CURDATE() - INTERVAL 30 DAY;

6. Retrieve the names of sellers who sell a specific product (e.g., "Laptop").

Ans :-

SELECT s.seller_name FROM Sellers s JOIN Products p ON s.seller_id = p.seller_id WHERE


p.product_name = 'Laptop';

7. Show all customers who have never placed an order.

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;

Empty set (0.00 sec)

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

SELECT c.customer_id, c.name, COUNT(o.order_id) AS total_orders FROM Customers c JOIN Orders o


ON c.customer_id = o.customer_id GROUP BY c.customer_id, c.name HAVING COUNT(o.order_id) >=
1;

10. Find the top 3 most ordered products based on quantity sold.

Ans :-

SELECT p.product_name, SUM(oi.quantity) AS total_quantity FROM Order_Items oi JOIN Products p


ON oi.product_id = p.product_id GROUP BY p.product_name ORDER BY total_quantity DESC LIMIT 3;

11. Display the product names that are sold by single or multiple sellers.

Ans :-

SELECT product_name FROM Products GROUP BY product_name HAVING COUNT(DISTINCT seller_id)


> 0;
12. Retrieve sellers who have never sold any product.

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;

Empty set (0.00 sec)

13. Find all products that have never been ordered.

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;

Empty set (0.00 sec)

14. Retrieve the names of customers who have placed the highest number of orders.

Ans :-

SELECT c.customer_id, c.name, COUNT(o.order_id) AS total_orders FROM Customers c JOIN Orders o


ON c.customer_id = o.customer_id GROUP BY c.customer_id, c.name HAVING COUNT(o.order_id) = (

SELECT MAX(order_count) FROM (SELECT COUNT(order_id) AS order_count FROM Orders GROUP BY


customer_id) AS order_counts

);
15. Find all customers who have ordered more than 1 different products.

Ans :-

mysql> SELECT c.customer_id, c.name, COUNT(DISTINCT oi.product_id) AS unique_products FROM


Customers c JOIN Orders o ON c.customer_id = o.customer_id JOIN Order_Items oi ON o.order_id =
oi.order_id GROUP BY c.customer_id, c.name HAVING COUNT(DISTINCT oi.product_id) > 1;

Empty set (0.00 sec)

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);

Empty set (0.00 sec)

17. Find the customer who has spent the most money overall.

Ans :-

SELECT c.customer_id, c.name, SUM(o.total_amount) AS total_spent FROM Customers c

JOIN Orders o ON c.customer_id = o.customer_id GROUP BY c.customer_id, c.name

ORDER BY total_spent DESC LIMIT 1;

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

SELECT DISTINCT p.product_id, p.product_name FROM Products p WHERE p.product_id IN (SELECT


product_id FROM Products) UNION SELECT DISTINCT oi.product_id, p.product_name FROM
Order_Items oi JOIN Products p ON oi.product_id = p.product_id;

20. Retrieve products that have been both ordered and are currently in stock.

Ans :-

SELECT p.product_id, p.product_name FROM Products p WHERE p.product_id IN (SELECT DISTINCT


product_id FROM Order_Items) AND p.product_id IN (SELECT DISTINCT product_id FROM Products);
21. Find customers who have both placed an order and live in a city where a seller is located.

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) ));

You might also like