Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

project

Submitted by
ANIMESH HALDER
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.

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)
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]

Execution finished without errors.


Result: 60 rows returned in 19ms
------------------------------------------------------------------------------------------------
At line 1:
SELECT P.PRODUCT_CLASS_CODE, P.PRODUCT_ID,
P.PRODUCT_DESC,P.product_price,
CASE P.PRODUCT_CLASS_CODE
WHEN 2050 THEN P.product_price + 2000 -- to increase price of 2050 product
WHEN 2051 THEN P.product_price + 500 -- to increase price of 2051 product
WHEN 2052 THEN P.product_price + 600 -- to increase price of 2052 product
ELSE P.PRODUCT_PRICE
END 'CALCULATED_PRICE'
FROM PRODUCT P INNER JOIN PRODUCT_CLASS PC
ON P.PRODUCT_CLASS_CODE = PC.PRODUCT_CLASS_CODE
ORDER BY P.PRODUCT_CLASS_CODE DESC;
2. Write a query to display (product_class_desc, product_id, Execution finished without errors.
product_desc, product_quantity_avail ) and Show inventory status of Result: 60 rows returned in 10ms
----------------------------------------------------------------------------------------------------------------------------
products as below as per their available quantity: a. For Electronics
At line 1:
and Computer categories, if available quantity is <= 10, show 'Low SELECT PC.PRODUCT_CLASS_DESC, P.PRODUCT_ID, P.PRODUCT_DESC, P.PRODUCT_QUANTITY_AVAIL,
stock', 11 <= qty <= 30, show 'In stock', >= 31, show 'Enough stock' --For Electronics and Computer categories
b. For Stationery and Clothes categories, if qty <= 20, show 'Low CASE
stock', 21 <= qty <= 80, show 'In stock', >= 81, show 'Enough stock' WHEN P.PRODUCT_CLASS_CODE in (2050,2053) THEN
c. Rest of the categories, if qty <= 15 – 'Low Stock', 16 <= qty <= 50 CASE
WHEN P.PRODUCT_QUANTITY_AVAIL =0 THEN 'Out of stock'
– 'In Stock', >= 51 – 'Enough stock' For all categories, if available
WHEN P.PRODUCT_QUANTITY_AVAIL <=10 THEN 'Low stock'
quantity is 0, show 'Out of stock'. Hint: Use case statement. (60 WHEN (P.PRODUCT_QUANTITY_AVAIL >=11 and P.PRODUCT_QUANTITY_AVAIL <=30) THEN 'In stock'
ROWS) [NOTE: TABLES TO BE USED – product, product_class] WHEN P.PRODUCT_QUANTITY_AVAIL >=31 THEN 'Enough stock'
END
--For Stationery and Clothes categories
WHEN P.PRODUCT_CLASS_CODE in (2052,2056) THEN
CASE
WHEN P.PRODUCT_QUANTITY_AVAIL =0 THEN 'Out of stock'
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 P.PRODUCT_QUANTITY_AVAIL >=81 THEN 'Enough stock'
END
--Rest of the products
ELSE
CASE
WHEN P.PRODUCT_QUANTITY_AVAIL =0 THEN 'Out of stock'
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 P.PRODUCT_QUANTITY_AVAIL >=51 THEN 'Enough stock'
END
END INVENTORY_STATUS
FROM PRODUCT P INNER JOIN PRODUCT_CLASS PC
ON P.PRODUCT_CLASS_CODE = PC.PRODUCT_CLASS_CODE
ORDER BY P.PRODUCT_CLASS_CODE, P.PRODUCT_QUANTITY_AVAIL ASC;
3. Write a query to show the number 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]

Execution finished without errors.


Result: 2 rows returned in 7ms
----------------------------------------------------------------------------------------------------
At line 1:
SELECT A.COUNTRY, COUNT(CITY) COUNT_OF_CITIES
FROM ADDRESS A
GROUP BY A.COUNTRY
HAVING A.COUNTRY NOT IN ('USA','Malaysia') AND COUNT(CITY) > 1
ORDER BY COUNT(CITY) DESC;
4. Write a query to display the customer_id,customer full name ,city,pincode,and order details (order id, 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 and
subtotal. (52 ROWS) [NOTE: TABLE TO BE USED - online_customer, address, order_header, order_items, product, product_class]

Execution finished without errors.


Result: 52 rows returned in 24ms
-----------------------------------------------------------------------------------------------------------------------------------------------------------
At line 1:
SELECT OC.CUSTOMER_ID,(OC.CUSTOMER_FNAME||' '||OC.CUSTOMER_LNAME) AS CUSTOMER_FULL_NAME,A.CITY,
A.PINCODE,OI.ORDER_ID,
PC.PRODUCT_CLASS_DESC,P.PRODUCT_DESC,(OI.PRODUCT_QUANTITY*P.PRODUCT_PRICE) AS SUBTOTAL
FROM
ONLINE_CUSTOMER OC
INNER JOIN ADDRESS A 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 OI.ORDER_ID = OH.ORDER_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
WHERE OH.ORDER_STATUS='Shipped' AND A.PINCODE NOT LIKE '%0%'
ORDER BY CUSTOMER_FULL_NAME, SUBTOTAL;
5. Write a Query to display product id,product description,totalquantity(sum(product quantity) for a given item whose product id is 201 and which item has
been bought along with it maximum no. of times. Display only one record which has the maximum value for total quantity in this scenario. (USE SUB-
QUERY)(1 ROW)[NOTE : ORDER_ITEMS TABLE,PRODUCT TABLE]

Execution finished without errors.


Result: 1 rows returned in 7ms
------------------------------------------------------------------------------------------------------------------------------------------------
At line 1:
SELECT OI.PRODUCT_ID,
P.PRODUCT_DESC,
SUM(OI.PRODUCT_QUANTITY) AS TOTAL_QUANTITY
FROM ORDER_ITEMS OI
INNER JOIN PRODUCT P ON P.PRODUCT_ID = OI.PRODUCT_ID
WHERE OI.ORDER_ID IN ( SELECT DISTINCT ORDER_ID FROM ORDER_ITEMS OI_S WHERE PRODUCT_ID = 201)
AND OI.PRODUCT_ID != 201
GROUP BY OI.PRODUCT_ID
ORDER BY TOTAL_QUANTITY DESC
LIMIT 1;
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]
Execution finished without errors.
Result: 225 rows returned in 54ms
--------------------------------------------------------------------------------------------------------------------------------------------------------------
At line 1:
SELECT OC.CUSTOMER_ID,
(OC.CUSTOMER_FNAME ||' '|| OC.CUSTOMER_LNAME) AS CUSTOMER_FULL_NAME,
OC.CUSTOMER_EMAIL,
OH.ORDER_ID,P.PRODUCT_DESC,
OI.PRODUCT_QUANTITY,
(OI.PRODUCT_QUANTITY*P.PRODUCT_PRICE) AS SUBTOTAL
FROM ONLINE_CUSTOMER OC
INNER JOIN ADDRESS A ON OC.ADDRESS_ID = A.ADDRESS_ID
LEFT JOIN ORDER_HEADER OH ON OC.CUSTOMER_ID = OH.CUSTOMER_ID
LEFT JOIN ORDER_ITEMS OI ON OH.ORDER_ID = OI.ORDER_ID
LEFT JOIN PRODUCT P ON OI.PRODUCT_ID = P.PRODUCT_ID
LEFT JOIN PRODUCT_CLASS PC ON P.PRODUCT_CLASS_CODE = PC.PRODUCT_CLASS_CODE;
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]
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,]
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]
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]

You might also like