50% found this document useful (2 votes)
3K views

City, Grade, Salesman - Id) ORDERS (Ord - No, Purchase - Amt, Ord - Date, Customer - Id, Salesman - Id) Write SQL Queries To

The document describes an Order Database schema with tables for Salesman, Customer, and Orders. It provides SQL queries to: 1. Count customers with grades above Bangalore's average 2. Find salesmen with more than one customer 3. List salesmen and indicate those with/without customers in their city 4. Create a view for the salesman with the highest order each day 5. Demonstrate deleting a salesman and cascading the deletion to related orders

Uploaded by

VIDYA P
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
3K views

City, Grade, Salesman - Id) ORDERS (Ord - No, Purchase - Amt, Ord - Date, Customer - Id, Salesman - Id) Write SQL Queries To

The document describes an Order Database schema with tables for Salesman, Customer, and Orders. It provides SQL queries to: 1. Count customers with grades above Bangalore's average 2. Find salesmen with more than one customer 3. List salesmen and indicate those with/without customers in their city 4. Create a view for the salesman with the highest order each day 5. Demonstrate deleting a salesman and cascading the deletion to related orders

Uploaded by

VIDYA P
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

B.

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.

Solution:

Entity-Relationship Diagram
CREATE TABLE SALESMAN
(SALESMAN_ID NUMBER (4),
NAME VARCHAR2 (20),
CITY VARCHAR2 (20),
COMMISSION VARCHAR2 (20),
PRIMARY KEY (SALESMAN_ID));

CREATE TABLE CUSTOMER1


(CUSTOMER_ID NUMBER (4),
CUST_NAME VARCHAR2 (20),
CITY VARCHAR2 (20),
GRADE NUMBER (3),
PRIMARY KEY (CUSTOMER_ID),
SALESMAN_ID REFERENCES SALESMAN (SALESMAN_ID) ON DELETE SET NULL);

CREATE TABLE ORDERS


(ORD_NO NUMBER (5),
PURCHASE_AMT NUMBER (10, 2),
ORD_DATE DATE,
PRIMARY KEY (ORD_NO),
CUSTOMER_ID REFERENCES CUSTOMER1 (CUSTOMER_ID) ON DELETE CASCADE,
SALESMAN_ID REFERENCES SALESMAN (SALESMAN_ID) ON DELETE CASCADE);

DESC SALESMAN;
DESC CUSTOMER1;
DESC ORDERS;

INSERT INTO SALESMAN VALUES (1000, ‘JOHN’,’BANGALORE’,’25 %’);


INSERT INTO SALESMAN VALUES (2000, ‘RAVI’,’BANGALORE’,’20 %’);
INSERT INTO SALESMAN VALUES (3000, ‘KUMAR’,’MYSORE’,’15 %’);
INSERT INTO SALESMAN VALUES (4000, ‘SMITH’,’DELHI’,’30 %’);
INSERT INTO SALESMAN VALUES (5000, ‘HARSHA’,’HYDRABAD’,’15 %’);

INSERT INTO CUSTOMER1 VALUES (10, ‘PREETHI’,’BANGALORE’, 1, 1000);


INSERT INTO CUSTOMER1 VALUES (11, ‘VIVEK’,’MANGALORE’, 3, 1000);
INSERT INTO CUSTOMER1 VALUES (12, ‘BHASKAR’,’CHENNAI’, 4, 2000);
INSERT INTO CUSTOMER1 VALUES (13, ‘CHETHAN’,’BANGALORE’, 2, 2000);
INSERT INTO CUSTOMER1 VALUES (14, ‘MAMATHA’,’BANGALORE’, 4, 3000);

INSERT INTO ORDERS VALUES (50, 5000, ‘04-MAY-17’, 10, 1000);


INSERT INTO ORDERS VALUES (51, 450, ‘20-JAN-17’, 10, 2000);
INSERT INTO ORDERS VALUES (52, 1000, ‘24-FEB-17’, 13, 2000);
INSERT INTO ORDERS VALUES (53, 3500, ‘13-APR-17’, 14, 3000);
INSERT INTO ORDERS VALUES (54, 550, ‘09-MAR-17’, 12, 2000);

SELECT * FROM SALESMAN;


SELECT * FROM CUSTOMER1;
SELECT * FROM ORDERS;

Queries:

1. 1. Count the customers with grades above Bangalore’s average.


SELECT GRADE, COUNT (DISTINCT CUSTOMER_ID) FROM CUSTOMER1
GROUP BY GRADE
HAVING GRADE > (SELECT AVG(GRADE) FROM CUSTOMER1
WHERE CITY='BANGALORE');
2. 2. Find the name and numbers of all salesmen who had more than one customer.

SELECT SALESMAN_ID, NAME FROM SALESMAN A


WHERE 1 < (SELECT COUNT (*)
FROM CUSTOMER1
WHERE SALESMAN_ID=A.SALESMAN_ID);
2. 2. List all salesmen and indicate those who have and don’t have customers in their cities
3. (Use UNION operation.)
SELECT SALESMAN.SALESMAN_ID, NAME, CUST_NAME, COMMISSION
FROM SALESMAN, CUSTOMER1
WHERE SALESMAN.CITY = CUSTOMER1.CITY UNION
SELECT SALESMAN_ID, NAME, 'NO MATCH', COMMISSION FROM SALESMAN
WHERE CITY != ANY (SELECT CITY
FROM CUSTOMER1) ORDER BY 2 DESC;

4. 4. Create a view that finds the salesman who has the customer with the highest order of a day.

CREATE VIEW ELITESALESMAN AS


SELECT B.ORD_DATE, A.SALESMAN_ID, A.NAME FROM SALESMAN A, ORDERS B
WHERE A.SALESMAN_ID = B.SALESMAN_ID
AND B.PURCHASE_AMT=(SELECT MAX (PURCHASE_AMT)
FROM ORDERS C
WHERE C.ORD_DATE = B.ORD_DATE);

5. 5. Demonstrate the DELETE operation by removing salesman with id 1000.


6. All his orders must also be deleted.

Use ON DELETE CASCADE at the end of foreign key definitions while creating child table orders and
then execute the following:

Use ON DELETE SET NULL at the end of foreign key definitions while creating child table customers
and then executes the following:

DELETE FROM SALESMAN


WHERE SALESMAN_ID=1000;
SELECT * FROM SALESMAN;

You might also like