0% found this document useful (0 votes)
25 views7 pages

SQL Questions

The document contains 10 SQL queries related to analyzing product, order, customer and other related tables in a database. The queries retrieve and aggregate data, apply filtering conditions, perform calculations and return sorted results. Key tasks include displaying product details with conditional pricing, showing inventory status, finding countries with most cities, listing customer and order details, and identifying optimum carton size.

Uploaded by

sarathravindhran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
25 views7 pages

SQL Questions

The document contains 10 SQL queries related to analyzing product, order, customer and other related tables in a database. The queries retrieve and aggregate data, apply filtering conditions, perform calculations and return sorted results. Key tasks include displaying product details with conditional pricing, showing inventory status, finding countries with most cities, listing customer and order details, and identifying optimum carton size.

Uploaded by

sarathravindhran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

SQL Questions

1. Write a query to Display the product details (product_class_code, product_id, produ


ct_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 t
he 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 requi
red. (60 ROWS) [NOTE: PRODUCT TABLE]

SELECT PRODUCT_CLASS_CODE,PRODUCT_ID,PRODUCT_DESC,PRODUCT_PRICE,

CASE

WHEN PRODUCT_CLASS_CODE=2050 THEN PRODUCT_PRICE+2000

WHEN PRODUCT_CLASS_CODE=2051 THEN PRODUCT_PRICE+500

WHEN PRODUCT_CLASS_CODE=2052 THEN PRODUCT_PRICE+600

ELSE PRODUCT_PRICE

END AS NEW_PRODUCT_PRICE

FROM PRODUCT

ORDER BY PRODUCT_CLASS_CODE DESC;

2.
Write a query to display (product_class_desc, product_id, product_desc, product_quant
ity_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, prod
uct_class]

SELECT
PR.PRODUCT_DESC,PR.PRODUCT_ID,PR.PRODUCT_QUANTITY_AVAIL,PC.PRODUCT_CLASS_DES
C,

CASE

WHEN (PC.PRODUCT_CLASS_DESC="ELECTRONICS" OR
PC.PRODUCT_CLASS_DESC="COMPUTER")
AND (PR.PRODUCT_QUANTITY_AVAIL <=10)

THEN "LOW STOCK"

WHEN (PC.PRODUCT_CLASS_DESC="ELECTRONICS"OR
PC.PRODUCT_CLASS_DESC="COMPUTER")

AND (PR.PRODUCT_QUANTITY_AVAIL BETWEEN 11 AND 30)

THEN "IN STOCK"

WHEN (PC.PRODUCT_CLASS_DESC="ELECTRONICS" OR
PC.PRODUCT_CLASS_DESC="COMPUTER")

AND (PR.PRODUCT_QUANTITY_AVAIL >=31)

THEN "ENOUGH STOCK"

WHEN (PC.PRODUCT_CLASS_DESC="STATIONERY" OR
PC.PRODUCT_CLASS_DESC="CLOTHES")

AND (PR.PRODUCT_QUANTITY_AVAIL <=20)

THEN "LOW STOCK"

WHEN (PC.PRODUCT_CLASS_DESC="STATIONERY"OR
PC.PRODUCT_CLASS_DESC="CLOTHES") AND

(PR.PRODUCT_QUANTITY_AVAIL BETWEEN 21 AND 80)

THEN "IN STOCK"

WHEN (PC.PRODUCT_CLASS_DESC="STATIONERY" OR
PC.PRODUCT_CLASS_DESC="CLOTHES") AND

(PR.PRODUCT_QUANTITY_AVAIL >=81)

THEN "ENOUGH STOCK"

WHEN (PR.PRODUCT_QUANTITY_AVAIL <=15)

THEN "LOW STOCK"

WHEN (PR.PRODUCT_QUANTITY_AVAIL BETWEEN 16 AND 50)

THEN "IN STOCK"

WHEN (PR.PRODUCT_QUANTITY_AVAIL >=50)

THEN "ENOUGH STOCK"

END AS STOCK_DETAILS

FROM PRODUCT PR

JOIN PRODUCT_CLASS PC ON

PC.PRODUCT_CLASS_CODE=PR.PRODUCT_CLASS_CODE
3.
Write a query to show the number of cities in all countries other than USA & MALAYS
IA, with more than 1 city, in the descending order of CITIES. (2 rows)
[NOTE: ADDRESS TABLE]

SELECT DISTINCT(COUNT(city)) AS COUNT, COUNTRY

FROM ADDRESS

WHERE COUNTRY NOT IN ('USA', 'MALAYSIA')

GROUP BY COUNTRY

HAVING COUNT(CITY) > 1

ORDER BY COUNT DESC;

4.
Write a query to display the customer_id,customer full name ,city,pincode,and order de
tails (order id, product class desc, product desc,
subtotal(product_quantity * product_price)) for orders shipped to cities whose pin code
s 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]

SELECT

C.CUSTOMER_ID,

concat(CUSTOMER_FNAME,' ',CUSTOMER_LNAME) AS FULL_NAME,

A.CITY, A.PINCODE, OI.ORDER_ID,

P.PRODUCT_DESC ,PC.PRODUCT_CLASS_DESC,

(OI.PRODUCT_QUANTITY * P.PRODUCT_PRICE) AS SUBTOTAL

FROM ONLINE_CUSTOMER C

JOIN ADDRESS A

ON C.ADDRESS_ID=A.ADDRESS_ID

JOIN ORDER_HEADER OH

ON C.CUSTOMER_ID=OH.CUSTOMER_ID

JOIN ORDER_ITEMS OI

ON OH.ORDER_ID= OI.ORDER_ID

JOIN PRODUCT P

ON OI.PRODUCT_ID=P.PRODUCT_ID
JOIN PRODUCT_CLASS PC

ON P.PRODUCT_CLASS_CODE=PC.PRODUCT_CLASS_CODE

WHERE OH.ORDER_STATUS='SHIPPED'

AND PINCODE NOT LIKE ('%0%')

ORDER BY SUBTOTAL ASC;

5.
Write a Query to display product id,product description,totalquantity(sum(product qua
ntity) 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 maxi
mum value for total quantity in this scenario. (USE SUB-QUERY)(1
ROW)[NOTE : ORDER_ITEMS TABLE,PRODUCT TABLE]

SELECT P.PRODUCT_ID,P.PRODUCT_DESC,

SUM(OI.PRODUCT_QUANTITY )AS TOT_QTY

FROM PRODUCT P

JOIN ORDER_ITEMS OI

ON P.PRODUCT_ID=OI.PRODUCT_ID

WHERE ORDER_ID IN (

SELECT ORDER_ID FROM ORDER_ITEMS

WHERE PRODUCT_ID=201

GROUP BY P.PRODUCT_ID,P.PRODUCT_DESC

ORDER BY TOT_QTY DESC

LIMIT 1;

6.
Write a query to display the customer_id,customer name, email and order details (orde
r 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 C.CUSTOMER_ID,

concat(CUSTOMER_FNAME,' ',CUSTOMER_LNAME) AS CUSTOMER_NAME,

C.CUSTOMER_EMAIL,OI.ORDER_ID,P.PRODUCT_DESC

,OI.PRODUCT_QUANTITY,
(PRODUCT_QUANTITY * PRODUCT_PRICE) AS SUBTOTAL

FROM ONLINE_CUSTOMER C

LEFT JOIN ORDER_HEADER OH

ON C.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;

7.
Write a query to display carton id, (len*width*height) as carton_vol and identify the op
timum 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 CARTON_ID, MIN(LEN*WIDTH*HEIGHT) AS CAR_VOL, TOT_VOL

FROM CARTON

CROSS JOIN (

SELECT (P.LEN*P.WIDTH*P.HEIGHT*OI.PRODUCT_QUANTITY) AS TOT_VOL

FROM ORDER_ITEMS OI

JOIN PRODUCT P ON P.PRODUCT_ID = OI.PRODUCT_ID

WHERE OI.ORDER_ID=10006

) AS TOTAL_VOL

WHERE (LEN*WIDTH*HEIGHT) >= TOT_VOL

GROUP BY CARTON_ID,TOT_VOL

ORDER BY CAR_VOL

LIMIT 1;

8.
Write a query to display details (customer id,customer fullname,order id,product quant
ity) 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 C.CUSTOMER_ID ,
concat(CUSTOMER_FNAME,' ',CUSTOMER_LNAME) AS FULL_NAME,

OH.ORDER_ID,SUM(OI.PRODUCT_QUANTITY)AS TOTAL

FROM ONLINE_CUSTOMER C

JOIN ORDER_HEADER OH

ON C.CUSTOMER_ID=OH.CUSTOMER_ID

JOIN ORDER_ITEMS OI

ON OH.ORDER_ID=OI.ORDER_ID

WHERE OH.PAYMENT_MODE IN('CREDIT CARD', "NET BANKING") AND


ORDER_STATUS="SHIPPED"

GROUP BY OH.ORDER_ID

HAVING TOTAL>10;

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 B
E USED - online_customer, order_header, order_items]

SELECT OH.ORDER_ID,C.CUSTOMER_ID,

CONCAT(C.CUSTOMER_FNAME,' ',C.CUSTOMER_LNAME) AS FULL_NAME,

SUM(OI.PRODUCT_QUANTITY) AS TOTAL

FROM ONLINE_CUSTOMER C

JOIN ORDER_HEADER OH

ON C.CUSTOMER_ID=OH.CUSTOMER_ID

JOIN ORDER_ITEMS OI

ON OH.ORDER_ID=OI.ORDER_ID

WHERE OH.ORDER_ID>10030 AND ORDER_STATUS="SHIPPED"

GROUP BY OH.ORDER_ID

HAVING FULL_NAME LIKE 'A%'

10.
Write a query to display product class description ,total quantity (sum(product_quantit
y),Total value (product_quantity * product price) and show which
class of products have been shipped highest(Quantity) to countries outside India other t
han USA? Also show the total value of those items. (1
ROWS)[NOTE:PRODUCT TABLE,ADDRESS TABLE,ONLINE_CUSTOMER TABL
E,ORDER_HEADER TABLE,ORDER_ITEMS
TABLE,PRODUCT_CLASS TABLE]

SELECT PC.PRODUCT_CLASS_DESC,

SUM(OI.PRODUCT_QUANTITY) AS TOT_QTY,

(OI.PRODUCT_QUANTITY * P.PRODUCT_PRICE)AS TOT_VAL

FROM PRODUCT P

JOIN PRODUCT_CLASS PC

ON P.PRODUCT_CLASS_CODE=PC.PRODUCT_CLASS_CODE

JOIN ORDER_ITEMS OI

ON P.PRODUCT_ID=OI.PRODUCT_ID

JOIN ORDER_HEADER OA

ON OI.ORDER_ID=OA.ORDER_ID

JOIN ONLINE_CUSTOMER C

ON OA.CUSTOMER_ID=C.CUSTOMER_ID

JOIN ADDRESS A

ON C.ADDRESS_ID=A.ADDRESS_ID

WHERE COUNTRY NOT IN('INDIA', "USA") AND ORDER_STATUS="SHIPPED"

GROUP BY PC.PRODUCT_CLASS_DESC

ORDER BY TOT_QTY DESC

LIMIT 1;

You might also like