SQL Project
Submitted to:
Great Learning
Submitted by:
Manoj Kumbhare
PGPDSBA Online July_C 2021
Data Science and Business Analytics.
Contents
Introduction.....................................................................................................................................4
1. SQLite...........................................................................................................................................4
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]....................................................4
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: a. For Electronics and Computer categories, if available quantity is <= 10, show 'Low
stock', 11 <= qty <= 30, show 'In stock', >= 31, show 'Enough stock' b. For Stationery and Clothes
categories, if qty <= 20, show 'Low stock', 21 <= qty <= 80, show 'In stock', >= 81, show 'Enough
stock' c. 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].............................5
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]........................................................................................................................................7
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 whose pin codes do not have any 0s in them. Sort
the output on customer name, order date and subtotal. (52 ROWS) [NOTE: TABLE TO BE USED -
online_customer, address, order_header, order_items, product, product_class].........................8
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]...............................................9
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].........................................................................................10
2. MySQL........................................................................................................................................11
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,PRODUCT TABLE].............................................................................................................11
8. Write a query to display details (customer id,customer fullname,order id,product quantity) of
customers who bought more than ten (i.e. total order qty) products with Credit Card or Net
Banking as the mode of payment per shipped order. (6 ROWS) [NOTE: TABLES TO BE USED -
online_customer, order_header, order_items,]..........................................................................12
9. Write a query to display the order_id, customer id and cutomer full name of customers
starting with the alphabet “A” along with (product_quantity) as total quantity of products
shipped for order ids > 10030. (5 ROWS) [NOTE: TABLES TO BE USED - online_customer,
order_header, order_items].......................................................................................................13
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]..................................................................................................14
LIST OF TABLES
No table of figures entries found.
LIST OF EQUATION
No table of figures entries found.
LIST OF FIGURES
Figure 1: Output 1.................................................................................................................................5
Figure 2: Output 2.................................................................................................................................6
Figure 3: Output 3.................................................................................................................................7
Figure 4: Output 4.................................................................................................................................9
Figure 5: Output 5...............................................................................................................................10
Figure 6: Output 6...............................................................................................................................11
Figure 7: Output 7...............................................................................................................................12
Figure 8: Output 8...............................................................................................................................13
Figure 9: Output 9...............................................................................................................................14
Figure 10: Output 10...........................................................................................................................15
Introduction:
This document captures the scenario of simple order management functionality of an online retail
store. Typical purchase scenario: A customer places an order for N products specifying quantity for
each line item of the order. Every product belongs to a product class (or category). All products
ordered in one order, are shipped to customer’s address (in India or outside) by a shipper in one
shipment. Order can be paid using either Cash, Credit Card or Net Banking. There can be customers
who may not have placed any order. Few customers would have cancelled their orders (As a whole
order, no cancellation of individual item allowed). Few orders may be ‘In process’ status. There can
also be products that were never purchased. Shippers use optimum sized cartons (boxes) to ship an
order, based on the total volume of all products and their quantities. Dimensions of each product (L,
W, H) is also stored in the database. To keep it simple, all products of an order are put in one single
appropriately sized carton for shipping.
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. 1st part- Q1-Q6 comes under SQLite and queries should be executed in DB Browser.
(Database - Orders.db) 2nd part- Q7-Q10 comes under MYSQL and the queries should be executed in
MYSQL. (SQL Script -orders.sql)
1. SQLite
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]
SELECT
PRODUCT_CLASS_CODE,
PRODUCT_ID,
PRODUCT_DESC,
PRODUCT_PRICE,
CASE PRODUCT_CLASS_CODE
WHEN 2050 THEN PRODUCT_PRICE + 2000
WHEN 2051 THEN PRODUCT_PRICE + 500
WHEN 2052 THEN PRODUCT_PRICE + 600
ELSE PRODUCT_PRICE
END AS INCREASE_PRICE
FROM
PRODUCT
ORDER BY PRODUCT_CLASS_CODE DESC;
Figure 1: Output 1
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:
a. For Electronics and Computer categories, if available quantity is <= 10, show 'Low stock', 11
<= qty <= 30, show 'In stock', >= 31, show 'Enough stock'
b. For Stationery and Clothes categories, if qty <= 20, show 'Low stock', 21 <= qty <= 80, show
'In stock', >= 81, show 'Enough stock'
c. 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]
SELECT
pc.PRODUCT_CLASS_DESC,
PRODUCT_ID,
PRODUCT_DESC,
PRODUCT_QUANTITY_AVAIL,
CASE
WHEN
PRODUCT_CLASS_DESC = 'Electronics'
OR PRODUCT_CLASS_DESC = 'Computer'
THEN
CASE
WHEN PRODUCT_QUANTITY_AVAIL = 0 THEN 'Out of Stock'
WHEN PRODUCT_QUANTITY_AVAIL <= 10 THEN 'Low Stock'
WHEN PRODUCT_QUANTITY_AVAIL BETWEEN 11 AND 30 THEN 'In Stock'
WHEN PRODUCT_QUANTITY_AVAIL >= 31 THEN 'Enough Stock'
END
WHEN
PRODUCT_CLASS_DESC = 'Clothes'
OR PRODUCT_CLASS_DESC = 'Stationery'
THEN
CASE
WHEN PRODUCT_QUANTITY_AVAIL = 0 THEN 'Out of Stock'
WHEN PRODUCT_QUANTITY_AVAIL <= 20 THEN 'Low Stock'
WHEN PRODUCT_QUANTITY_AVAIL BETWEEN 21 AND 80 THEN 'In Stock'
WHEN PRODUCT_QUANTITY_AVAIL >= 81 THEN 'Enough Stock'
END
ELSE CASE
WHEN PRODUCT_QUANTITY_AVAIL = 0 THEN 'Out of Stock'
WHEN PRODUCT_QUANTITY_AVAIL <= 15 THEN 'Low Stock'
WHEN PRODUCT_QUANTITY_AVAIL BETWEEN 16 AND 50 THEN 'In Stock'
WHEN PRODUCT_QUANTITY_AVAIL >= 51 THEN 'Enough Stock'
END
END AS inventory_level
FROM
PRODUCT prod
INNER JOIN
PRODUCT_CLASS pc ON prod.PRODUCT_CLASS_CODE = pc.PRODUCT_CLASS_CODE;
Figure 2: Output 2
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]
SELECT * FROM ADDRESS;
SELECT
COUNT(CITY) AS CITY_COUNT, COUNTRY
FROM
ADDRESS
WHERE
COUNTRY NOT IN ('USA' , 'Malaysia')
GROUP BY COUNTRY
HAVING COUNT(CITY) > 1
ORDER BY CITY_COUNT DESC;
Figure 3: Output 3
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 whose pin codes do not have any 0s in them. Sort
the output on customer name, order date and subtotal. (52 ROWS) [NOTE: TABLE TO BE USED -
online_customer, address, order_header, order_items, product, product_class]
SELECT
cust.CUSTOMER_ID,
cust.CUSTOMER_FNAME
|| cust.CUSTOMER_LNAME AS CUSTOMER_FULLNAME,
addr.CITY,
addr.PINCODE,
ord.ORDER_ID,
prod_class.PRODUCT_CLASS_DESC,
prod.PRODUCT_DESC,
prod.PRODUCT_PRICE * prod.PRODUCT_PRICE AS SUBTOTAL
FROM
ONLINE_CUSTOMER AS cust
INNER JOIN
ADDRESS AS addr ON cust.ADDRESS_ID = addr.ADDRESS_ID
INNER JOIN
ORDER_HEADER ord ON cust.CUSTOMER_ID = ord.CUSTOMER_ID
AND ord.ORDER_STATUS = 'Shipped'
INNER JOIN
ORDER_ITEMS OI ON ord.ORDER_ID = OI.ORDER_ID
INNER JOIN
PRODUCT prod ON OI.PRODUCT_ID = prod.PRODUCT_ID
INNER JOIN
PRODUCT_CLASS prod_class ON prod_class.PRODUCT_CLASS_CODE =
prod.PRODUCT_CLASS_CODE
WHERE
addr.PINCODE NOT LIKE '%0%'
ORDER BY CUSTOMER_FULLNAME , ord.ORDER_DATE , SUBTOTAL;
Figure 4: Output 4
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]
SELECT
prod.PRODUCT_ID,
PRODUCT_DESC,
SUM(PRODUCT_QUANTITY) AS TOTAL_QUANTITY
FROM
ORDER_ITEMS oi,
PRODUCT prod
WHERE
ORDER_ID IN (SELECT
ORDER_ID
FROM
ORDER_ITEMS
WHERE
PRODUCT_ID = 201)
AND prod.PRODUCT_ID != 201
AND oi.PRODUCT_ID = prod.PRODUCT_ID
GROUP BY prod.PRODUCT_ID , prod.PRODUCT_DESC
ORDER BY TOTAL_QUANTITY DESC
LIMIT 1;
Figure 5: Output 5
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]
SELECT
oc.CUSTOMER_ID,
oc.CUSTOMER_FNAME || oc.CUSTOMER_LNAME AS CUSTOMER_NAME,
CUSTOMER_EMAIL,
ord.ORDER_ID,
prod.PRODUCT_DESC,
oi.PRODUCT_QUANTITY,
oi.PRODUCT_QUANTITY * prod.PRODUCT_PRICE AS subtotal
FROM
ONLINE_CUSTOMER oc
LEFT JOIN
ORDER_HEADER ord ON oc.CUSTOMER_ID = ord.CUSTOMER_ID
LEFT JOIN
ORDER_ITEMS oi ON ord.ORDER_ID = oi.ORDER_ID
LEFT JOIN
PRODUCT prod ON oi.PRODUCT_ID = prod.PRODUCT_ID
ORDER BY oc.CUSTOMER_ID , ord.ORDER_ID , prod.PRODUCT_DESC;
Figure 6: Output 6
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,PRODUCT TABLE]
SELECT
CARTON_ID, (LEN * WIDTH * HEIGHT) AS CARTON_VOL
FROM
CARTON
WHERE
(LEN * WIDTH * HEIGHT) >= (SELECT
SUM(LEN * WIDTH * HEIGHT * PRODUCT_QUANTITY)
FROM
order_items OI
INNER JOIN
product PROD ON OI.PRODUCT_ID = PROD.PRODUCT_ID
WHERE
ORDER_ID = 10006)
ORDER BY CARTON_VOL
LIMIT 1;
Figure 7: Output 7
8. Write a query to display details (customer id,customer fullname,order id,product quantity) of
customers who bought more than ten (i.e. total order qty) products with Credit Card or Net
Banking as the mode of payment per shipped order. (6 ROWS) [NOTE: TABLES TO BE USED -
online_customer, order_header, order_items,]
SELECT
OC.CUSTOMER_ID,
CONCAT(CUSTOMER_FNAME, ' ', CUSTOMER_LNAME) AS CUSTOMER_FULLNAME,
OH.ORDER_ID,
SUM(OI.PRODUCT_QUANTITY) AS TOTAL_ORDER_QUANTITY
FROM
online_customer AS OC
INNER JOIN
order_header AS OH ON OC.CUSTOMER_ID = OH.CUSTOMER_ID
INNER JOIN
order_items AS OI ON OH.order_id = OI.order_id
WHERE
OH.order_status = 'Shipped'
AND OH.PAYMENT_MODE IN ('credit card' , 'Net Banking')
AND OH.order_id IN (SELECT
ORDER_ID
FROM
order_items
GROUP BY ORDER_ID
HAVING SUM(PRODUCT_QUANTITY) > 10)
GROUP BY ORDER_ID;
Figure 8: Output 8
9. Write a query to display the order_id, customer id and cutomer full name of customers
starting with the alphabet “A” along with (product_quantity) as total quantity of products
shipped for order ids > 10030. (5 ROWS) [NOTE: TABLES TO BE USED - online_customer,
order_header, order_items]
SELECT
OH.ORDER_ID,
CUST.CUSTOMER_ID,
CONCAT(CUSTOMER_FNAME, ' ', CUSTOMER_LNAME) AS CUSTOMER_FULLNAME,
SUM(OI.PRODUCT_QUANTITY) AS TOTAL_QUANTITY_OF_PRODUCTS
FROM
order_header AS OH
JOIN
online_customer CUST ON OH.customer_id = CUST.customer_id
LEFT JOIN
order_items AS OI ON OH.order_id = OI.order_id
WHERE
OH.ORDER_ID > 10030
AND CUSTOMER_FNAME LIKE 'A%'
AND OH.order_status = 'Shipped'
GROUP BY OH.ORDER_ID;
Figure 9: Output 9
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]
SELECT
PRODUCT_CLASS_DESC,
SUM(OI.PRODUCT_QUANTITY) AS TOTAL_QUANTITY,
SUM(OI.product_quantity * PROD.product_price) AS TOTAL_VALUE
FROM
address A
INNER JOIN
online_customer OC ON OC.ADDRESS_ID = A.ADDRESS_ID
INNER JOIN
order_header OH ON OH.CUSTOMER_ID = OC.CUSTOMER_ID
INNER JOIN
order_items OI ON OH.ORDER_ID = OI.ORDER_ID
INNER JOIN
product PROD ON PROD.PRODUCT_ID = OI.PRODUCT_ID
INNER JOIN
product_class PC ON PROD.PRODUCT_CLASS_CODE = PC.PRODUCT_CLASS_CODE
WHERE
A.COUNTRY != 'India'
AND A.COUNTRY != 'USA'
AND ORDER_STATUS = 'Shipped'
GROUP BY PRODUCT_CLASS_DESC
ORDER BY TOTAL_QUANTITY DESC
LIMIT 1;
Figure 10: Output 10