0% found this document useful (0 votes)
279 views

Order Database PDF

1. Count the customers with grades above Bangalore's average grade. 2. Find the names and number of customers for salesmen with more than one customer. 3. Use a UNION to list salesmen who do and do not have customers in their city. 4. Create a view to find the salesman with the highest customer order on a given date. 5. Demonstrate deleting a salesman and their associated orders using their salesman ID.

Uploaded by

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

Order Database PDF

1. Count the customers with grades above Bangalore's average grade. 2. Find the names and number of customers for salesmen with more than one customer. 3. Use a UNION to list salesmen who do and do not have customers in their city. 4. Create a view to find the salesman with the highest customer order on a given date. 5. Demonstrate deleting a salesman and their associated orders using their salesman ID.

Uploaded by

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

ORDER DATABASE

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
CREATE TABLE SALESMAN(
SALESMAN_ID INTEGER PRIMARY KEY,
NAME VARCHAR(10) NOT NULL,
CITY VARCHAR(15) NOT NULL,
COMMISSION NUMERIC(5,2)
);
CUSTOMER
CREATE TABLE CUSTOMER(
CUSTOMER_ID INTEGER PRIMARY KEY,
CUST_NAME VARCHAR(10) NOT NULL,
CITY VARCHAR(10) NOT NULL,
GRADE INTEGER NOT NULL,
SALESMAN_ID INTEGER,
FOREIGN KEY (SALESMAN_ID) REFERENCES SALESMAN(SALESMAN_ID) ON DELETE SET NULL
);
ORDERS
CREATE TABLE ORDERS(
ORD_NO INTEGER PRIMARY KEY,
PURCHASE_AMT INTEGER NOT NULL,
ORD_DATE DATE NOT NULL,
CUSTOMER_ID INTEGER REFERENCES CUSTOMER(CUSTOMER_ID),
SALESMAN_ID INTEGER,
FOREIGN KEY (SALESMAN_ID) REFERENCES SALESMAN(SALESMAN_ID) ON DELETE CASCADE
);
Values for tables

INSERT INTO SALESMAN VALUES(1000,'RAJ','BENGALURU',50);


INSERT INTO SALESMAN VALUES(2000,'ASHWIN','TUMKUR',30);
INSERT INTO SALESMAN VALUES(3000,'BINDU','MUMBAI',40);
INSERT INTO SALESMAN VALUES(4000,'LAVANYA','BENGALURU',40);
INSERT INTO SALESMAN VALUES(5000,'ROHIT','MYSORE',60);

SELECT * FROM SALESMAN;

SALESMAN_ID NAME CITY COMMISSION

1000 RAJ BENGALURU 50

2000 ASHWIN TUMKUR 30

3000 BINDU MUMBAI 40

4000 LAVANYA BENGALURU 40

5000 ROHIT MYSORE 60

INSERT INTO CUSTOMER VALUES(11, 'INFOSYS','BENGALURU',5,1000);


INSERT INTO CUSTOMER VALUES(22, 'TCS','BENGALURU',4,2000);
INSERT INTO CUSTOMER VALUES(33, 'WIPRO','MYSORE',7,1000);
INSERT INTO CUSTOMER VALUES(44, 'TCS','MYSORE',6,2000);
INSERT INTO CUSTOMER VALUES(55, 'ORACLE','TUMKUR',3,3000);

SELECT * FROM CUSTOMER;

CUSTOMER_ID CUST_NAME CITY GRADE SALESMAN_ID

11 INFOSYS BENGALURU 5 1000

22 TCS BENGALURU 4 2000

33 WIPRO MYSORE 7 1000

44 TCS MYSORE 6 2000

55 ORACLE TUMKUR 3 3000


INSERT INTO ORDERS VALUES(1,200000,'2020-04-16',11,1000);
INSERT INTO ORDERS VALUES(2,300000,'2020-05-20',11,2000);
INSERT INTO ORDERS VALUES(3,400000,'2019-04-19',22,1000);

SELECT * FROM ORDERS;

ORD_NO PURCHASE_AMT ORD_DATE CUSTOMER_ID SALESMAN_ID

1 200000 2020-04-16 11 1000

2 300000 2020-05-20 11 2000

3 400000 2019-04-19 22 1000

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

SELECT COUNT(CUSTOMER_ID)
FROM CUSTOMER
WHERE GRADE > (SELECT AVG (GRADE)
FROM CUSTOMER
WHERE CITY = 'BENGALURU');

COUNT(CUSTOMER_ID)

2. Find the name and numbers of all salesmen who had more than one customer.

SELECT NAME, COUNT(CUSTOMER_ID)


FROM SALESMAN S, CUSTOMER C
WHERE S.SALESMAN_ID=C.SALESMAN_ID
GROUP BY NAME
HAVING COUNT(CUSTOMER_ID)>1;

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

RAJ

ASHWIN

BINDU

LAVANYA

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='2020-04-16');

SELECT * FROM SALES_HIGHERODER;

SALESMAN_ID PURCHASE_AMT

1000 200000
5. Demonstrate the DELETE operation by removing salesman with id 1000. All his
orders must also be deleted.

BEFORE DELETION

SELECT * FROM SALESMAN;

SALESMAN_ID NAME CITY COMMISSION

1000 RAJ BENGALURU 50

2000 ASHWIN TUMKUR 30

3000 BINDU MUMBAI 40

4000 LAVANYA BENGALURU 40

5000 ROHIT MYSORE 60

SELECT * FROM CUSTOMER;

CUSTOMER_ID CUST_NAME CITY GRADE SALESMAN_ID

11 INFOSYS BENGALURU 5 1000

22 TCS BENGALURU 4 2000

33 WIPRO MYSORE 7 1000

44 TCS MYSORE 6 2000

55 ORACLE TUMKUR 3 3000

SELECT * FROM ORDERS;

ORD_NO PURCHASE_AMT ORD_DATE CUSTOMER_ID SALESMAN_ID

1 200000 2020-04-16 11 1000

2 300000 2020-05-20 11 2000

3 400000 2019-04-19 22 1000


DELETE from SALESMAN
WHERE SALESMAN_ID = 1000;

AFTER DELETION

SELECT * FROM SALESMAN;

SALESMAN_ID NAME CITY COMMISSION

2000 ASHWIN TUMKUR 30

3000 BINDU MUMBAI 40

4000 LAVANYA BENGALURU 40

5000 ROHIT MYSORE 60

SELECT * FROM CUSTOMER;

CUSTOMER_ID CUST_NAME CITY GRADE SALESMAN_ID

11 INFOSYS BENGALURU 5 (null)

22 TCS BENGALURU 4 2000

33 WIPRO MYSORE 7 (null)

44 TCS MYSORE 6 2000

55 ORACLE TUMKUR 3 3000

SELECT * FROM ORDERS;

ORD_NO PURCHASE_AMT ORD_DATE CUSTOMER_ID SALESMAN_ID

2 300000 2020-05-20 11 2000

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy