You are on page 1of 5

ADBMS ASSIGNMENT-II

SUBMITTED TO: PROF. YANN CHANG


Q1. State what the following query computes: 𝜋𝐶𝑢𝑠𝑡𝑜𝑚𝑒𝑟 𝑁𝑎𝑚𝑒(𝜋𝐶𝑢𝑠𝑡𝑜𝑚𝑒𝑟 𝐼𝐷(𝜋𝑂𝑟𝑑𝑒𝑟
𝐼𝐷(𝑂𝑟𝑑𝑒𝑟_𝐿𝑖𝑛𝑒_𝑡 ⨝ 𝜎𝑃𝑟𝑜𝑑𝑢𝑐 𝑁𝑎𝑚𝑒= ′𝐶𝑜𝑓𝑓𝑒𝑒 𝑇𝑎𝑏𝑙𝑒 ′𝑃𝑟𝑜𝑑𝑢𝑐𝑡_𝑡) ⨝ Order_t) ⨝ Customer_t)

Response: The above query extracts all those Customer Name who have ordered the product Coffee
Table.

Q2. Compose a relational algebra expression that would find every order that requested (included) both
of these two products: End Table and Coffee Table. The output of this relational algebra expression
should show Order_ID and Order_Date. Hint: use a division operator concept.

Response:

ORDER_ALL<- 𝜋𝑂𝑟𝑑𝑒𝑟 𝐼𝐷, Product_Id(𝑂𝑟𝑑𝑒𝑟_𝐿𝑖𝑛𝑒_𝑡)

PRODUCT_ALL<- 𝜋 Product_id ( 𝜎𝑃𝑟𝑜𝑑𝑢𝑐t 𝑁𝑎𝑚𝑒= ′𝐶𝑜𝑓𝑓𝑒𝑒 𝑇𝑎𝑏𝑙𝑒 ′ or ‘End Table’ 𝑃𝑟𝑜𝑑𝑢𝑐𝑡_𝑡)

RESULT_ORDERS<-ORDER_ALL PRODUCT_ALL

RESULT<-𝜋𝑂𝑟𝑑𝑒𝑟 𝐼𝐷, order_date (𝜋𝑂𝑟𝑑𝑒𝑟 𝐼𝐷, Product_id RESULT_ORDERS ⨝ Order_t)

Q3. Compose a SQL statement that is equivalent to Question 1 above. Note, you might want to try and
execute this SQL statement against the PVF database in SQL Server 2014 to see if it works as intended.

Response:
SELECT CUSTOMER_NAME FROM (SELECT CUSTOMER_ID FROM
(SELECT DISTINCT ORDER_ID FROM ORDER_LINE_T A JOIN
(SELECT * FROM PRODUCT_T WHERE PRODUCT_NAME='COFFEE TABLE')B ON
A.PRODUCT_ID=B.PRODUCT_ID) CUST
JOIN ORDER_T ORD ON CUST.ORDER_ID=ORD.ORDER_ID)CUST_NAME JOIN CUSTOMER_T NAME
ON CUST_NAME.CUSTOMER_ID=NAME.CUSTOMER_ID
Q4. Compose an SQL statement that is equivalent to Question 2 above. Note, you might want to try and
execute this SQL statement against the PVF database in SQL Server 2014 to see if it works as intended.

Response:

SELECT DISTINCT A.ORDER_ID,A.ORDER_DATE FROM ORDER_T A INNER JOIN ORDER_LINE_T B


INNER JOIN PRODUCT_T C ON B.PRODUCT_ID=C.PRODUCT_ID
AND upper(C.PRODUCT_NAME) IN ('COFFEE TABLE','END TABLE')
ON A.ORDER_ID=B.ORDER_ID
Q5. Compose a query that would find customer(s) who submitted at least two orders in 2011. In the
result table, display Customer_ID and Customer_Name.

Response:
SELECT A.CUSTOMER_ID,B.CUSTOMER_NAME FROM (SELECT CUSTOMER_ID,COUNT(ORDER_ID) AS
TOT_ORD FROM ORDER_T
WHERE YEAR(ORDER_DATE)=2011 GROUP BY CUSTOMER_ID
HAVING COUNT(ORDER_ID)>=2) A INNER JOIN CUSTOMER_T B ON A.CUSTOMER_ID=B.CUSTOMER_ID

Q6. For each customer who submitted at least one order, show Order_ID, Customer_ID, and Order_Date
of the recent two orders. Note: if a customer submitted only one order, that customer order should also
be in the result (output) table. Hint: use a correlated nested query to answer this top N (where N>1) per
group question.

Response:

SELECT CUSTOMER_ID,ORDER_ID,ORDER_DATE FROM ORDER_T A WHERE ORDER_DATE IN


(SELECT TOP 2 ORDER_DATE FROM ORDER_T B WHERE A.CUSTOMER_ID=B.CUSTOMER_ID ORDER BY
ORDER_DATE DESC)
ORDER BY CUSTOMER_ID;

You might also like