MODULE - 4
ASSIGNMENT
Please do not use GenAI (e.g. ChatGPT) for solving assignments and do not copy and paste
from the internet. Understand the solution using GenAI or the internet if you want to but you
have to answer in your own words. Also do not copy solutions from your friends or
batchmates.
Our plagiarism tool on LMS will identify such things and make a record that you are following
unethical means to solve assignments. Without any notice we shall just discard your name
from any internship or job assistance from our side.
Ethics is the number one quality which we all must possess. Thank You
Instructions:
Please share your answers filled in line in the Word document. Submit
code separately wherever applicable.
Please ensure you update all the details:
Name: Avinash Kumar Batch ID: 04/04/2025
Topic: Introduction to Database
1. Create a Supermart_DB with the tables created from the datasets
shared (Customer.csv, Sales.csv and Product.csv files)
a. Create a new database in your database management system,
and name it Supermart_DB.
b. Create a new table called "customers" in the Supermart_DB
database
c. Load the data from the Customer.csv file into the customers
table
d. Create a new table called "products" in the Supermart_DB
database
e. Load the data from the Product.csv file into the products table
f. Create a new table called "sales" in the Supermart_DB database
g. Load the data from the Sales.csv file into the sales table
© 360DigiTMG. All Rights Reserved.
SELECTION OPERATORS:- (FILTERING):- in, like, between
Note: use products, customers and sales table
1. Define the relationship between the tables using constraints/keys.
2. In the database Supermart _DB, find the following:
a. Get the list of all the cities where the region is north or east without
any duplicates using the IN statement.
b. Get the list of all orders where the ‘sales’ value is between 100 and
500 using the BETWEEN operator.
c. Get the list of customers whose last name contains only 4 characters
using LIKE.
SELECTION OPERATORS:- ordering
1. Retrieve all orders where the ‘discount’ value is greater than zero
ordered in descending order basis ‘discount’ value
2. Limit the number of results in the above query to the top 10.
Aggregate operators:-
1. Find the sum of all ‘sales’ values.
2. Find count of the number of customers in the north region with ages
between 20 and 30
3. Find the average age of east region customers
4. Find the minimum and maximum aged customers from Philadelphia
GROUP BY OPERATORS:-
1. Create a display with the information below for each product ID.
a. Total sales (in $) order by this column in descending
b. Total sales quantity
c. The number of orders
d. Max Sales value
e. Min Sales value
f. Average sales value
2. Get the list of product ID’s where the quantity of product sold is greater
than 10
© 360DigiTMG. All Rights Reserved.
Question 1
Create a Supermart_DB with the tables created from the datasets shared
(Customer.csv, Sales.csv and Product.csv files)
Answer:
create database supermart_db;
##customers table
create table customers (
customer_id int primary key,
first_name varchar(50),
last_name varchar(50),
city varchar(50),
region varchar(20),
age int
);
#products table
create table products (
product_id int primary key,
product_name varchar(100),
category varchar(50),
© 360DigiTMG. All Rights Reserved.
price decimal(10,2)
);
#sales table
create table sales (
order_id int primary key,
customer_id int,
product_id int,
order_date date,
sales decimal(10,2),
quantity int,
discount decimal(5,2),
foreign key (customer_id) references customers(customer_id),
foreign key (product_id) references products(product_id)
);
Question 2
Define the relationship between the tables using constraints/keys.
Answer:
customers is linked to sales through customer_id.
products is linked to sales through product_id.
Both are foreign keys in the sales table, ensuring referential integrity.
© 360DigiTMG. All Rights Reserved.
Question 3
In the database Supermart_DB, find the following:
a. Get the list of all the cities where the region is north or east without any
duplicates using the IN statement.
Answer:
select distinct city
from customers
where region in ('north','east');
b. Get the list of all orders where the sales value is between 100 and 500
using the BETWEEN operator.
Answer:
select * from sales
where sales between 100 and 500;
c. Get the list of customers whose last name contains only 4 characters using
LIKE.
Answer:
select * from customers
where last_name like '____';
© 360DigiTMG. All Rights Reserved.
Question 4
Retrieve all orders where the discount value is greater than zero ordered in
descending order basis discount value.
Answer:
select * from sales
where discount > 0
order by discount desc;
Question 5
Limit the number of results in the above query to the top 10.
Answer:
select * from sales
where discount > 0
order by discount desc
limit 10;
Question 6
Aggregate operators:
a) Find the sum of all sales values.
Answer:
select sum(sales) as total_sales
from sales;
© 360DigiTMG. All Rights Reserved.
b) Find count of the number of customers in the north region with ages
between 20 and 30.
Answer:
select count(*)
from customers
where region='north' and age between 20 and 30;
c) Find the average age of east region customers.
Answer:
select avg(age) as avg_age
from customers
where region='east';
d) Find the minimum and maximum aged customers from Philadelphia.
Answer:
select min(age), max(age)
from customers
where city='philadelphia';
Question 7
Group By Operators:
a) Create a display with the following for each product ID:
Total sales (in $), Total sales quantity, Number of orders, Max sales
value, Min sales value, Average sales value. Order by total sales in
descending.
© 360DigiTMG. All Rights Reserved.
Answer:
select product_id,
sum(sales) as total_sales,
sum(quantity) as total_quantity,
count(order_id) as num_orders,
max(sales) as max_sales,
min(sales) as min_sales,
avg(sales) as avg_sales
from sales
group by product_id
order by total_sales desc;
b) Get the list of product IDs where the quantity of product sold is greater
than 10.
Answer:
select product_id, sum(quantity) as total_quantity
from sales
group by product_id
having sum(quantity) > 10;
© 360DigiTMG. All Rights Reserved.
© 360DigiTMG. All Rights Reserved.