This Study Resource Was: SQL Project
This Study Resource Was: SQL Project
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
Hint: Use case statement. no permanent change in table required. (60 ROWS)
[NOTE: PRODUCT TABLE]
Query
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
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
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)
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
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
-- 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.
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)
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
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
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.
Query
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)