67% found this document useful (3 votes)
2K views

This Study Resource Was: SQL Project

The document provides details for a project involving analyzing order data for an online retail store. It outlines 10 questions to analyze data from an SQLite and MySQL database to help the company make data-driven decisions. The questions include displaying product details with adjusted prices based on category, showing inventory status based on available quantity, counting cities in countries with more than one city, and displaying customer and order details for orders shipped to cities without zero in their pincodes.

Uploaded by

vansh gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
67% found this document useful (3 votes)
2K views

This Study Resource Was: SQL Project

The document provides details for a project involving analyzing order data for an online retail store. It outlines 10 questions to analyze data from an SQLite and MySQL database to help the company make data-driven decisions. The questions include displaying product details with adjusted prices based on category, showing inventory status based on available quantity, counting cities in countries with more than one city, and displaying customer and order details for orders shipped to cities without zero in their pincodes.

Uploaded by

vansh gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Project Problem Statement

You are hired by a chain of online retail stores “Reliant retail limited”. They provided you with “orders”
database and seek answers to the following queries as the results from these queries will help the company in
making data-driven decisions that will impact the overall growth of the online retail store.
Objective of the Project

1st part(Q1-Q6) comes under SQLite and queries should be executed in DB Browser.
(Database used- New Orders.db)

2nd part(Q7-Q10) comes under MYSQL and the queries should be executed in MYSQL.
(SQL Script used - new Orders.sql)

ERR Diagram

m
er as
co
eH w
o.
rs e
ou urc
o
aC s
v i y re
ed d
ar stu
sh is
Th

SQL Project
16th May 2021 PG DSBA

This study source was downloaded by 100000822941909 from CourseHero.com on 11-27-2021 03:47:57 GMT -06:00

https://www.coursehero.com/file/97433579/SQL-Worddocx/
Part-1(SQLite)

Question 1

1) Write a query to Display the product details (product_class_code, product_id, product_desc,


product_price,) as per the following criteria and sort them in descending order of category:

a) If the category is 2050, increase the price by 2000


b) If the category is 2051, increase the price by 500
c) If the category is 2052, increase the price by 600.

Hint: Use case statement. no permanent change in table required. (60 ROWS)
[NOTE: PRODUCT TABLE]

SOLUTION - Considered PRODUCT_CLASS_CODE as CATAGORY.

Query

SELECT PRODUCT_CLASS_CODE AS 'Product Catagory',

m
PRODUCT_ID AS 'Product ID',

er as
PRODUCT_DESC AS 'Product Description',

co
PRODUCT_PRICE AS 'Actual Price',

eH w
CASE PRODUCT_CLASS_CODE
WHEN 2050 THEN PRODUCT_PRICE+2000 -- Increase Price for Category 2050

o.
rs e
WHEN 2051 THEN PRODUCT_PRICE+500 -- Increase Price for Category 2051
WHEN 2052 THEN PRODUCT_PRICE+600 -- Increase Price for Category 2052
ou urc
ELSE PRODUCT_PRICE
END AS 'Calculated Price'
FROM PRODUCT
o

-- Decending order by category(Product Class Code)


ORDER BY PRODUCT_CLASS_CODE DESC;
aC s
v i y re

Output
ed d
ar stu
sh is
Th

This study source was downloaded by 100000822941909 from CourseHero.com on 11-27-2021 03:47:57 GMT -06:00

https://www.coursehero.com/file/97433579/SQL-Worddocx/
2. Write a query to display (product_class_desc, product_id, product_desc, product_quantity_avail) and Show
inventory status of products as below as per their available quantity:
1. For Electronics and Computer categories, if available quantity is <= 10, show 'Low stock', 11 <=
qty <= 30, show 'In stock', >= 31, show 'Enough stock'
2. For Stationery and Clothes categories, if qty <= 20, show 'Low stock', 21 <= qty <= 80, show 'In
stock', >= 81, show 'Enough stock'
3. Rest of the categories, if qty <= 15 – 'Low Stock', 16 <= qty <= 50 – 'In Stock', >= 51 – 'Enough
stock'
For all categories, if available quantity is 0, show 'Out of stock'.
Hint: Use case statement.
(60 ROWS) [NOTE: TABLES TO BE USED – product, product_class]
Query

SELECT PC.PRODUCT_CLASS_DESC AS 'Product Category',


P.PRODUCT_ID AS 'Product ID',
P.PRODUCT_DESC AS 'Product Description',
P.PRODUCT_QUANTITY_AVAIL AS 'Product Availability',
CASE
-- Electronics(2050) and Computer (2053)
WHEN PC.PRODUCT_CLASS_CODE IN (2050,2053) THEN

m
CASE

er as
WHEN P.PRODUCT_QUANTITY_AVAIL =0 THEN 'Out of stock' -- Out of stock criteria
WHEN P.PRODUCT_QUANTITY_AVAIL <=10 THEN 'Low stock'

co
WHEN (P.PRODUCT_QUANTITY_AVAIL >=11 AND P.PRODUCT_QUANTITY_AVAIL <=30) THEN 'In stock'

eH w
WHEN (PRODUCT_QUANTITY_AVAIL >=31) THEN 'Enough stock'
END

o.
-- Stationery(2052) and Clothes(2056)
rs e
WHEN PC.PRODUCT_CLASS_CODE IN (2052,2056) THEN
CASE
ou urc
WHEN P.PRODUCT_QUANTITY_AVAIL =0 THEN 'Out of stock' -- Out of stock criteria
WHEN P.PRODUCT_QUANTITY_AVAIL <=20 THEN 'Low stock'
WHEN (P.PRODUCT_QUANTITY_AVAIL >=21 AND P.PRODUCT_QUANTITY_AVAIL <=80) THEN 'In stock'
WHEN (PRODUCT_QUANTITY_AVAIL >=81) THEN 'Enough stock'
o

END
-- Rest of the categories
aC s

ELSE
v i y re

CASE
WHEN P.PRODUCT_QUANTITY_AVAIL =0 THEN 'Out of stock' -- Out of stock criteria
WHEN P.PRODUCT_QUANTITY_AVAIL <=15 THEN 'Low stock'
WHEN (P.PRODUCT_QUANTITY_AVAIL >=16 AND P.PRODUCT_QUANTITY_AVAIL <=50) THEN 'In stock'
WHEN (PRODUCT_QUANTITY_AVAIL >=51) THEN 'Enough stock'
ed d

END
END AS 'Inventory Status'
ar stu

FROM PRODUCT P
-- Join the Product and Product Class TABLE based on the Product Class Code
INNER JOIN PRODUCT_CLASS PC ON P.PRODUCT_CLASS_CODE = PC.PRODUCT_CLASS_CODE
-- Let’s do order by Product Class Code and available quantity by descending
ORDER BY P.PRODUCT_CLASS_CODE,P.PRODUCT_QUANTITY_AVAIL DESC;
sh is

Output
Th

This study source was downloaded by 100000822941909 from CourseHero.com on 11-27-2021 03:47:57 GMT -06:00

https://www.coursehero.com/file/97433579/SQL-Worddocx/
3. Write a query to Show the count of cities in all countries other than USA & MALAYSIA, with more than 1 city, in
the descending order of CITIES. (2 rows)

[NOTE: ADDRESS TABLE, Do not use Distinct]

Query Output
SELECT COUNT(CITY) AS Count_of_Cites, -- Count Of The Cities
COUNTRY AS Country
FROM ADDRESS
GROUP BY COUNTRY
-- Count of cities more than 1 and exclude the USA and Malaysia
HAVING COUNTRY NOT IN ('USA','Malaysia') AND COUNT(CITY) > 1
-- Descending order of count of cities
ORDER BY Count_of_Cites DESC;

m
er as
4. Write a query to display the customer_id,customer full name ,city,pincode,and order details (order id,order
date, product class desc, product desc, subtotal(product_quantity * product_price)) for orders shipped to cities

co
eH w
whose pin codes do not have any 0s in them. Sort the output on customer name, order date and subtotal. (52
ROWS)

o.
rs e
[NOTE: TABLE TO BE USED - online_customer, address, order_header, order_items, product, product_class]
ou urc
Query

SELECT OC.CUSTOMER_ID AS 'Customer ID',


o

OC.CUSTOMER_FNAME || ' ' || OC.CUSTOMER_LNAME AS 'Customer Full Name' ,


A.CITY AS 'City',
aC s

A.PINCODE AS 'Pin Code',


v i y re

OH.ORDER_ID AS 'Order Id',


PC.PRODUCT_CLASS_DESC AS 'Product Class Description',
P.PRODUCT_DESC AS 'Product Description',
P.PRODUCT_PRICE AS 'Product Price',
OI.PRODUCT_QUANTITY AS 'Product Order Quantity',
ed d

(P.PRODUCT_PRICE * OI.PRODUCT_QUANTITY) AS Sub_Total -- Calculated value Total Price


FROM
ar stu

ONLINE_CUSTOMER OC
INNER JOIN ADDRESS A ON OC.ADDRESS_ID = A.ADDRESS_ID -- Join the Address table to fetch the City and Pincode details.
INNER JOIN ORDER_HEADER OH ON OH.CUSTOMER_ID = OC.CUSTOMER_ID
INNER JOIN ORDER_ITEMS OI ON OI.ORDER_ID = OH.ORDER_ID -- For Product Order Quantity
INNER JOIN PRODUCT P ON P.PRODUCT_ID = OI.PRODUCT_ID -- For Product Price
sh is

INNER JOIN PRODUCT_CLASS PC ON PC.PRODUCT_CLASS_CODE = P.PRODUCT_CLASS_CODE -- For Product Class Description


(Category)
Th

-- Filter the data which is shipped and Pin code does not have value 0.
WHERE OH.ORDER_STATUS='Shipped' AND A.PINCODE NOT LIKE '%0%'
-- Order by customer name and subtotal.
ORDER BY OC.CUSTOMER_FNAME, Sub_Total;

Output

This study source was downloaded by 100000822941909 from CourseHero.com on 11-27-2021 03:47:57 GMT -06:00

https://www.coursehero.com/file/97433579/SQL-Worddocx/
5. Write a Query to display product id,product description,totalquantity (sum(product quantity) for an item which
has been bought maximum no. of times along with product id 201. (USE SUB-QUERY) (1 ROW)
[NOTE: ORDER_ITEMS TABLE, PRODUCT TABLE]

Query

SELECT OI.PRODUCT_ID AS Product_ID, -- 2. Look for other product_id that are brought along with product_id 201
P.PRODUCT_DESC AS Product_Description, -- 4. Get the Product Description from Product Table
SUM(OI.PRODUCT_QUANTITY) AS Total_Quantity-- 3. Total quantity(sum(product quantity) for each product_id that was brought
along with product_id 201
FROM ORDER_ITEMS OI
INNER JOIN PRODUCT P ON P.PRODUCT_ID = OI.PRODUCT_ID -- Join the Product Table to fetch the description
WHERE OI.ORDER_ID IN
( -- 1. Pull out all the orders that have the product_id 201
SELECT DISTINCT
ORDER_ID
FROM
ORDER_ITEMS A
WHERE
PRODUCT_ID = 201
)
AND OI.PRODUCT_ID <> 201

m
GROUP BY OI.PRODUCT_ID

er as
ORDER BY Total_Quantity DESC -- 5. Sort by Total_Quantity on descending

co
LIMIT 1; -- 6. Show the first row

eH w
Output

o.
rs e
ou urc
o
aC s
v i y re
ed d
ar stu
sh is
Th

This study source was downloaded by 100000822941909 from CourseHero.com on 11-27-2021 03:47:57 GMT -06:00

https://www.coursehero.com/file/97433579/SQL-Worddocx/
6. Write a query to display the customer_id,customer name, email and order details (order id, product
desc,product qty, subtotal(product_quantity * product_price)) for all customers even if they have not ordered
any item.

(225 ROWS) [NOTE: TABLE TO BE USED - online_customer, order_header, order_items, product]

Query

SELECT
OC.CUSTOMER_ID AS Customer_ID,
(OC.CUSTOMER_FNAME ||' '|| OC.CUSTOMER_LNAME) AS Customer_Full_Name,
OC.CUSTOMER_EMAIL AS Customer_Email,
O.ORDER_ID AS Order_ID,
P.PRODUCT_DESC AS Product_Description,
OI.PRODUCT_QUANTITY AS Purchase_Quantity,
P.PRODUCT_PRICE AS Product_Price,
(OI.PRODUCT_QUANTITY*P.PRODUCT_PRICE) AS Subtotal -- Calulated value Total Price
FROM
ONLINE_CUSTOMER OC
LEFT JOIN ORDER_HEADER O ON OC.CUSTOMER_ID = O.CUSTOMER_ID -- Join the Order header to fetch the order id and connect
product and customer
LEFT JOIN ORDER_ITEMS OI ON O.ORDER_ID = OI.ORDER_ID -- For Prodcut Quantity

m
er as
LEFT JOIN PRODUCT P ON OI.PRODUCT_ID = P.PRODUCT_ID -- For Product Price
ORDER BY Customer_ID,Purchase_Quantity DESC; -- Lets Order by Customer_ID and Purchase_Quantitity

co
eH w
Output

o.
rs e
ou urc
o
aC s
v i y re
ed d
ar stu
sh is
Th

This study source was downloaded by 100000822941909 from CourseHero.com on 11-27-2021 03:47:57 GMT -06:00

https://www.coursehero.com/file/97433579/SQL-Worddocx/
Part-2(MySQL)

7. Write a query to display carton id, (len*width*height) as carton_vol and identify the optimum carton (carton
with the least volume whose volume is greater than the total volume of all items (len * width * height *
product_quantity)) for a given order whose order id is 10006, Assume all items of an order are packed into one
single carton (box). (1 ROW)

[NOTE: CARTON TABLE]

Query
SELECT C.CARTON_ID AS Carton_ID,
(C.LEN*C.WIDTH*C.HEIGHT) as Carton_Volume
FROM ORDERS.CARTON C
WHERE (C.LEN*C.WIDTH*C.HEIGHT) >= (
-- Subquery to take volume details from both Order_items and Product tables.
SELECT SUM(P.LEN*P.WIDTH*P.HEIGHT*OI.PRODUCT_QUANTITY) AS VOL -- Optimum carton value
FROM
ORDERS.ORDER_ITEMS OI
INNER JOIN ORDERS.PRODUCT P ON OI.PRODUCT_ID = P.PRODUCT_ID -- Join to get the LEN, WIDTH and HEIGHT
WHERE OI.ORDER_ID =10006 ## Filtered the only Order ID 10006)
ORDER BY (C.LEN*C.WIDTH*C.HEIGHT) ASC

m
LIMIT 1;

er as
# Order by descending will arrange the outcome in decreasing order of Product of Len*Wodth*Height, and Limit 1 will display only 1 record

co
eH w
Output

o.
rs e
ou urc
o
aC s
v i y re
ed d
ar stu
sh is
Th

This study source was downloaded by 100000822941909 from CourseHero.com on 11-27-2021 03:47:57 GMT -06:00

https://www.coursehero.com/file/97433579/SQL-Worddocx/
8. Write a query to display details (customer id,customer fullname,order id,product jquantity) of customers who
bought more than ten (i.e. total order qty) products per shipped order. (11 ROWS) [NOTE: TABLES TO BE USED
- online_customer, order_header, order_items,]

Query

SELECT OC.CUSTOMER_ID AS Customer_ID,


CONCAT(CUSTOMER_FNAME,' ',CUSTOMER_LNAME) AS Customer_FullName,
OH.ORDER_ID AS Order_ID,
SUM(OI.PRODUCT_QUANTITY) AS Total_Order_Quantity
FROM ONLINE_CUSTOMER OC
INNER JOIN ORDER_HEADER OH ON OH.CUSTOMER_ID = OC.CUSTOMER_ID -- To connect the Order and Customer details.
INNER JOIN ORDER_ITEMS OI ON OI.ORDER_ID = OH.ORDER_ID -- To fetch the Product Quantity.
WHERE OH.ORDER_STATUS = 'Shipped' -- To check for order_status whether it is shipped.
GROUP BY OH.ORDER_ID
HAVING Total_Order_Quantity > 10 -- To check the Total Order Quality is greater than 10.
ORDER BY CUSTOMER_ID;

Output

m
er as
co
eH w
o.
rs e
ou urc
o
aC s

9. Write a query to display the order_id, customer id and cutomer full name of customers along with
v i y re

(product_quantity) as total quantity of products shipped for order ids > 10060. (6 ROWS)
[NOTE: TABLES TO BE USED - online_customer, order_header, order_items]

Query
SELECT
ed d

OC.CUSTOMER_ID AS Customer_ID,
ar stu

CONCAT(CUSTOMER_FNAME,' ',CUSTOMER_LNAME) AS Customer_FullName,


OH.ORDER_ID AS Order_ID,
SUM(OI.PRODUCT_QUANTITY) AS Total_Order_Quantity
FROM ONLINE_CUSTOMER OC
INNER JOIN ORDER_HEADER OH ON OH.CUSTOMER_ID = OC.CUSTOMER_ID -- To connect the Order and Customer details.
sh is

INNER JOIN ORDER_ITEMS OI ON OI.ORDER_ID = OH.ORDER_ID -- To fetch the Product Quantity.


WHERE OH.ORDER_STATUS = 'Shipped' AND OH.ORDER_ID > 10060 -- To check for order_status whether it is shipped.
GROUP BY OH.ORDER_ID
Th

ORDER BY Customer_FullName;

Output

This study source was downloaded by 100000822941909 from CourseHero.com on 11-27-2021 03:47:57 GMT -06:00

https://www.coursehero.com/file/97433579/SQL-Worddocx/
10. Write a query to display product class description ,total quantity (sum(product_quantity),Total value
(product_quantity * product price) and show which class of products have been shipped highest(Quantity) to
countries outside India other than USA? Also show the total value of those items.

(1 ROWS)[NOTE:PRODUCT TABLE,ADDRESS TABLE,ONLINE_CUSTOMER TABLE,ORDER_HEADER


TABLE,ORDER_ITEMS TABLE,PRODUCT_CLASS TABLE]

Query

SELECT PC.PRODUCT_CLASS_CODE AS Product_Class_Code,


PC.PRODUCT_CLASS_DESC AS Product_Class_Description,
SUM(OI.PRODUCT_QUANTITY) AS Total_Quantity,
SUM(OI.PRODUCT_QUANTITY*P.PRODUCT_PRICE) AS Total_Value
FROM ORDER_ITEMS OI
INNER JOIN ORDER_HEADER OH ON OH.ORDER_ID = OI.ORDER_ID -- Join to connect Online Customer
INNER JOIN ONLINE_CUSTOMER OC ON OC.CUSTOMER_ID = OH.CUSTOMER_ID
INNER JOIN PRODUCT P ON P.PRODUCT_ID = OI.PRODUCT_ID
INNER JOIN PRODUCT_CLASS PC ON PC.PRODUCT_CLASS_CODE = P.PRODUCT_CLASS_CODE
INNER JOIN ADDRESS A ON A.ADDRESS_ID = OC.ADDRESS_ID -- To retrive the country details.
WHERE OH.ORDER_STATUS ='Shipped' AND A.COUNTRY NOT IN('India','USA') # Order status as Shipped & country without India and USA.

m
GROUP BY PC.PRODUCT_CLASS_CODE,PC.PRODUCT_CLASS_DESC

er as
ORDER BY Total_Quantity DESC -- Order by Total_Quality
LIMIT 1;

co
eH w
Output

o.
rs e
ou urc
o
aC s
v i y re
ed d
ar stu
sh is
Th

This study source was downloaded by 100000822941909 from CourseHero.com on 11-27-2021 03:47:57 GMT -06:00

https://www.coursehero.com/file/97433579/SQL-Worddocx/
Powered by TCPDF (www.tcpdf.org)

You might also like