Sandhya Assignment SQL
Sandhya Assignment SQL
END
WHEN PRODUCT_CLASS_DESC ='Stationery and Clothes' THEN
CASE
WHEN PRODUCT_QUANTITY_AVAIL <= 20 THEN 'Low stock'
WHEN PRODUCT_QUANTITY_AVAIL >= 21 and PRODUCT_QUANTITY_AVAIL<=80 THEN 'In stock'
WHEN PRODUCT_QUANTITY_AVAIL >= 81 THEN 'Enough stock'
END
ELSE
CASE
WHEN PRODUCT_QUANTITY_AVAIL <= 15 THEN 'Low stock'
WHEN PRODUCT_QUANTITY_AVAIL >= 16 and PRODUCT_QUANTITY_AVAIL<=50 THEN 'In stock'
WHEN PRODUCT_QUANTITY_AVAIL >= 31 THEN 'Enough stock'
END
END AS Stock
FROM PRODUCT,PRODUCT_CLASS
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 COUNTRY, COUNT(CITY)AS CITY_COUNT
FROM ADDRESS
HAVING COUNT(CITY)>1
SELECT ONLINE_CUSTOMER.CUSTOMER_ID,
ONLINE_CUSTOMER.CUSTOMER_FNAME || ' ' || CUSTOMER_LNAME AS
CUSTOMER_NAME,
ADDRESS.CITY,
ADDRESS.PINCODE,
ORDER_HEADER.ORDER_ID,
ORDER_HEADER.ORDER_DATE,
PRODUCT.PRODUCT_DESC,
ORDER_ITEMS.PRODUCT_QUANTITY*PRODUCT.PRODUCT_PRICE AS 'Subtotal'
FROM ONLINE_CUSTOMER
JOIN ADDRESS
ON ONLINE_CUSTOMER.CUSTOMER_ID = ADDRESS.CUSTOMER_ID
JOIN ORDER_HEADER
ON ONLINE_CUSTOMER.CUSTOMER_ID = ORDER_HEADER.CUSTOMER_ID
JOIN ORDER_ITEMS
ON ORDER_HEADER.ORDER_ID = ORDER_ITEMS.ORDER_ID
JOIN PRODUCT
ON ORDER_ITEMS.PRODUCT_ID = PRODUCT.PRODUCT_ID
WHERE ADDRESS.(PINCODE, CITY) NOT LIKE '%0%'
-- AND ORDER_HEADER.ORDER_STATUS = 'Shipped'
Group by PRODUCT.PRODUCT_ID,PRODUCT.PRODUCT_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]
SELECT
CUSTOMER_ID AS "Customer ID",
CONCAT(CUSTOMER_FNAME, CUSTOMER_LNAME) AS Name,
CUSTOMER_EMAIL AS Email,
ORDER_ID AS "Order ID",
PRODUCT_DESC AS Description,
PRODUCT_QUANTITY AS Quantity
PRODUCT_PRICE * PRODUCT_QUANTITY) AS Subtotal
FROM ONLINE_CUSTOMER
NATURAL LEFT JOIN ORDER_HEADER
NATURAL JOIN ORDER_ITEMS
NATURAL JOIN PRODUCT;
2nd part(Q7-Q10) comes under MYSQL and the queries should be executed in MYSQL.
(SQL Script used - new Orders.sql)
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]
SELECT Cart.CARTON_ID, Cart.carton_vol
FROM ORDER_ITEMS
ON ORDER_ITEMS.PRODUCT_ID = PRODUCT.PRODUCT_ID
HAVING (((Count(order_items.PRODUCT_ID))>10));
9. Write a query to display the order_id, customer id and cutomer full name of customers
along with (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 ]
SELECT oh.order_id, oc.customer_id, oc.customer_fname, oc.customer_lname
FROM online_customer oc, order_header oh, order_items oi
WHERE oh.order_id=oi.order_id AND
oc.customer_id=oh.customer_id and
oh.order_id>10060;
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]
Highestquantity: First, we create a common table expression (cte) to get the highestquantity.
We joined 4 tables, order_items, order_header, online_customer, address, using their
foreign keys. Then, we use a WHERE clause to exclude India and USA countries. Next, we use
a GROUP BY claus to be able to use an aggregate function: SUM(). Then, we ORDER
totalquantity DESC to sort quantity from highest to lowest. Finally, we use a LIMIT 1 clause
to be able to get the the highest quantity.
Totalvalue: To get the totalvalue, we joined product and order items table using the foreign
key PRODUCT_ID. Then we use a group by clause to use the SUM() function for us to be able
to get totalvalue per PRODUCT_ID.