Chapter - 3 Order Database: ER-Diagram
Chapter - 3 Order Database: ER-Diagram
ORDER DATABASE
2) Consider the following schema for Order Database:
SALESMAN (Salesman_id, Name, City, Commission)
CUSTOMER (Customer_id, Cust_Name, City, Grade, Salesman_id)
ORDERS (Ord_No, Purchase_Amt, Ord_Date, Customer_id, Salesman_id)
Write SQL queries to
1. Count the customers with grades above Bangalore’s average.
2. Find the name and numbers of all salesmen who had more than one customer.
3. List all salesmen and indicate those who have and don’t have customers in their cities
(Use UNION operation.)
4. Create a view that finds the salesman who has the customer with the highest order of a
day.
5. Demonstrate the DELETE operation by removing salesman with id 1000. All his orders
must also be deleted.
ER-Diagram:
SCHEMA:
Table Creation:
SALESMAN
Table created.
CUSTOMER
Table created.
ORDERS
Table created.
SELECT COUNT(CUSTOMER_ID)
FROM CUSTOMER
WHERE GRADE>(SELECT AVG(GRADE)
FROM CUSTOMER
WHERE CITY LIKE '%BENGALURU');
COUNT(CUSTOMER_ID)
------------------
3
2. Find the name and numbers of all salesmen who had more than one customer.
NAME COUNT(CUSTOMER_ID)
---------- ------------------
ASHWIN 2
RAJ 2
3. List all salesmen and indicate those who have and don’t have customers in their cities
(Use UNION operation.)
(SELECT NAME
FROM SALESMAN S, CUSTOMER C
WHERE S.SALESMAN_ID=C.SALESMAN_ID AND
S.CITY=C.CITY)
UNION
(SELECT NAME
FROM SALESMAN
WHERE SALESMAN_ID NOT IN(SELECT S1.SALESMAN_ID
FROM SALESMAN S1, CUSTOMER C1
WHERE S1.SALESMAN_ID=C1.SALESMAN_ID AND
S1.CITY=C1.CITY));
NAME
----------
ASHWIN
BINDU
LAVANYA
RAJ
ROHIT
4. Create a view that finds the salesman who has the customer with the highest order of a
day.
CREATE VIEW SALES_HIGHERODER AS
SELECT SALESMAN_ID, PURCHASE_AMT
FROM ORDERS
WHERE PURCHASE_AMT=(SELECT MAX(O.PURCHASE_AMT)
FROM ORDERS O
WHERE O.ORD_DATE='12-APR-16');
View created.
1 row deleted.