0% found this document useful (0 votes)
281 views16 pages

Sandhya Assignment SQL

The document contains 10 queries related to product, order, customer and other tables. Query 1 displays product details with adjusted prices based on category. Query 2 displays inventory status based on quantity. Query 3 shows count of cities by country excluding USA and Malaysia. Query 4 displays customer and order details for orders shipped to cities without 0 in pincode. Query 5 finds most frequently bought product along with a given product. Query 6 displays all customers with or without orders. The remaining queries require running on MySQL and retrieve additional order details.

Uploaded by

sanisani1020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
281 views16 pages

Sandhya Assignment SQL

The document contains 10 queries related to product, order, customer and other tables. Query 1 displays product details with adjusted prices based on category. Query 2 displays inventory status based on quantity. Query 3 shows count of cities by country excluding USA and Malaysia. Query 4 displays customer and order details for orders shipped to cities without 0 in pincode. Query 5 finds most frequently bought product along with a given product. Query 6 displays all customers with or without orders. The remaining queries require running on MySQL and retrieve additional order details.

Uploaded by

sanisani1020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

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]
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 t1.PRODUCT_DESC, t1.PRODUCT_ID, t1.PRODUCT_QUANTITY_AVAIL,


t2.PRODUCT_CLASS_DESC

FROM PRODUCT AS t1 INNER JOIN PRODUCT_CLASS AS t2

ON t1.PRODUCT_CLASS_CODE = t2.PRODUCT_CLASS_CODE ORDER BY PRODUCT_ID;


SELECT PRODUCT_CLASS_DESC,PRODUCT_ID, PRODUCT_DESC, PRODUCT_QUANTITY_AVAIL,
CASE
WHEN PRODUCT_QUANTITY_AVAIL =0 THEN 'Out of stock'
WHEN PRODUCT_CLASS_DESC ='Electronics and Computer' THEN
CASE
WHEN PRODUCT_QUANTITY_AVAIL <= 10 THEN 'Low stock'
WHEN PRODUCT_QUANTITY_AVAIL >= 11 and PRODUCT_QUANTITY_AVAIL<=30 THEN 'In stock'
WHEN PRODUCT_QUANTITY_AVAIL >= 31 THEN 'Enough stock'

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

WHERE COUNTRY !='USA' and COUNTRY !='Malaysia'

GROUP BY CITY, COUNTRY

HAVING COUNT(CITY)>1

ORDER BY CITY DESC


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 a.CUSTOMER_ID, CONCAT(b.CUSTOMER_FNAME," ",b.CUSTOMER_LNAME)


"CUSTOMER NAME" , b.CITY, b.PINCODE, c.ORDER_ID, c.ORDER_DATE,
f.PRODUCT_CLASS_DESC, e.PRODUCT_DESC, d.PRODUCT_QUANTITY * e.PRODUCT_PRICE
"SUB TOTAL" from ONLINE_CUSTOMER a, ADDRESS b, ORDER_HEADER c, ORDER_ITEMS d,
PRODUCT e, PRODUCT_CLASS f WHERE a.CUSTOMER_ID = b.CUSTOMER_ID AND
a.CUSTOMER_ID = c.CUSTOMER_ID and c.ORDER_ID = d.ORDER_ID and d.PRODUCT_ID =
e.PRODUCT_ID and e.PRODUCT_CLASS_CODE = f.PRODUCT_CLASS_CODE and b.PINCODE
NOT like "%0%" ORDER BY b.CUSTOMER_NAME, c.ORDER_DATE ,d.PRODUCT_QUANTITY *
e.PRODUCT_PRICE ;

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'

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 (Quantity Wise) along with product id 201.
(USE SUB-QUERY) (1 ROW) [NOTE: ORDER_ITEMS TABLE, PRODUCT TABLE]
SELECT PRODUCT.PRODUCT_ID,PRODUCT.PRODUCT_DESC,
SUM(ORDER_ITEMS.PRODUCT_QUANTITY) as TOTALQUANTITY,

MAX(PRODUCT.PRODUCT_DESC) FROM PRODUCT JOIN ORDER_ITEMS ON


ORDER_ITEMS.PRODUCT_ID=PRODUCT.PRODUCT_ID

WHERE PRODUCT.PRODUCT_ID = 201

Group by PRODUCT.PRODUCT_ID,PRODUCT.PRODUCT_DESC

ORDER by SUM(ORDER_ITEMS.PRODUCT_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]

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 (SELECT CARTON.CARTON_ID, (CARTON.LEN * CARTON.WIDTH * CARTON.HEIGHT) as


carton_vol FROM CARTON) Cart

WHERE Cart.carton_vol > (SELECT SUM(LEN*width*HEIGHT*ORDER_ITEMS.PRODUCT_QUANTITY)


AS TotalVolume

FROM ORDER_ITEMS

INNER JOIN PRODUCT

ON ORDER_ITEMS.PRODUCT_ID = PRODUCT.PRODUCT_ID

WHERE order_id = 10006

ORDER BY Cart.carton_vol ASC


LIMIT 1;

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 per shipped
order. (11 ROWS) [NOTE: TABLES TO BE USED - online_customer, order_header,
order_items,]
SELECT online_customer.CUSTOMER_ID, online_customer.CUSTOMER_FNAME,
online_customer.CUSTOMER_LNAME, order_items.ORDER_ID,
order_items.PRODUCT_QUANTITY

FROM (online_customer INNER JOIN order_header ON online_customer.CUSTOMER_ID =


order_header.CUSTOMER_ID) INNER JOIN order_items ON order_header.ORDER_ID =
order_items.ORDER_ID

GROUP BY online_customer.CUSTOMER_FNAME, online_customer.CUSTOMER_LNAME

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.

Lastly, we use a select statement to get the product_class_desc, totalquantity and


totalvalue. To be able to get the columns on our select statement, we join product_class,
product, highestquantity (cte) and totalvalue (cte)

You might also like