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

Assignment 05

Uploaded by

Ritika Awasthi
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)
553 views

Assignment 05

Uploaded by

Ritika Awasthi
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/ 8

Assignment:- 05

Operators
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: _____________ Batch ID: ___________
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

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

© 360DigiTMG. All Rights Reserved.


Assignment:- 05
Operators
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

1.)
CREATE DATABASE Supermart_DB;

USE Supermart_DB;

CREATE TABLE customers (


CustomerID INT,

© 360DigiTMG. All Rights Reserved.


Assignment:- 05
Operators
CustomerName VARCHAR(255),
City VARCHAR(255),
Country VARCHAR(255)
);

LOAD DATA INFILE 'Customer.csv'


INTO TABLE customers
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

CREATE TABLE products (


ProductID INT,
ProductName VARCHAR(255),
UnitPrice DECIMAL(10, 2),
UnitsInStock INT
);

LOAD DATA INFILE 'Product.csv'


INTO TABLE products
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

CREATE TABLE sales (


OrderID INT,

© 360DigiTMG. All Rights Reserved.


Assignment:- 05
Operators
CustomerID INT,
ProductID INT,
OrderDate DATE,
Quantity INT,
TotalAmount DECIMAL(10, 2)
);

LOAD DATA INFILE 'Sales.csv'


INTO TABLE sales
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

SELECTION OPERATORS:- (FILTERING):- in, like, between

-- Create the products table with primary key


CREATE TABLE products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(255),
UnitPrice DECIMAL(10, 2),
UnitsInStock INT
);

-- Create the customers table with primary key


CREATE TABLE customers (
CustomerID INT PRIMARY KEY,

© 360DigiTMG. All Rights Reserved.


Assignment:- 05
Operators
CustomerName VARCHAR(255),
City VARCHAR(255),
Country VARCHAR(255)
);

-- Create the sales table with foreign keys


CREATE TABLE sales (
OrderID INT PRIMARY KEY,
CustomerID INT,
ProductID INT,
OrderDate DATE,
Quantity INT,
TotalAmount DECIMAL(10, 2),
FOREIGN KEY (CustomerID) REFERENCES customers(CustomerID),
FOREIGN KEY (ProductID) REFERENCES products(ProductID)
);

a)
SELECT DISTINCT City
FROM customers
WHERE Region IN ('North', 'East');

b)
SELECT *
FROM sales
WHERE TotalAmount BETWEEN 100 AND 500;

c)

© 360DigiTMG. All Rights Reserved.


Assignment:- 05
Operators
SELECT *
FROM customers
WHERE CustomerName LIKE '____';

SELECTION OPERATORS:- ordering

1)
SELECT *
FROM orders
WHERE discount > 0
ORDER BY discount DESC;

2)
SELECT *
FROM orders
WHERE discount > 0
ORDER BY discount DESC
LIMIT 10;

Aggregate operators:-

1)
SELECT SUM(sales) AS total_sales
FROM sales;

2)
SELECT COUNT(*) AS customer_count
FROM customers

© 360DigiTMG. All Rights Reserved.


Assignment:- 05
Operators
WHERE region = 'north' AND age BETWEEN 20 AND 30;

3)
SELECT AVG(age) AS average_age
FROM customers
WHERE region = 'east';

4)
SELECT MIN(age) AS min_age, MAX(age) AS max_age
FROM customers
WHERE city = 'Philadelphia';

GROUP BY OPERATORS:-
1)
SELECT
product_id,
SUM(sales) AS total_sales,
SUM(quantity) AS total_quantity,
COUNT(*) 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;

© 360DigiTMG. All Rights Reserved.


Assignment:- 05
Operators
2)
SELECT
DISTINCT product_id
FROM
sales
WHERE
quantity > 10;

© 360DigiTMG. All Rights Reserved.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy