Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DBMS AP21110011442 Final lab

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

DBMS FINAL LAB

G.Nikhila AP21110011442 CSE U


CODE :
use job;
create table Salesman_table(
Salesman_id int primary key,
name varchar(50),
city varchar(50),
commission decimal(10,2));
CREATE TABLE Order_Table (
ord_no INT PRIMARY KEY,
purch_amt INT,
ord_date DATE,
cust_id INT,
salesman_id INT,
FOREIGN KEY (salesman_id) REFERENCES Salesman(Salesman_id)
);
CREATE TABLE Customer_table (
cust_id INT PRIMARY KEY,
cust_name VARCHAR(50),
city varchar(50),
grade INT,
salesman_id INT,
foreign key(salesman_id) references Salesman(Salesman_id)
);

INSERT INTO Salesman_table VALUES(5001,'Sachin','Chennai',0.15);


INSERT INTO Salesman_table VALUES(5002,'Danis','Kolkata',0.13);
INSERT INTO Salesman_table VALUES(5005,'Hari','Mumbai',0.11);
INSERT INTO Salesman_table VALUES(5006,'Ambar','Kolkata',0.14);
INSERT INTO Salesman_table VALUES(5007,'Paul','Mysuru',0.13);
INSERT INTO Salesman_table VALUES(5003,'Goutham','Hyderabad',0.12);

insert into Order_Table values(70001,150,'2021-10-05',3005,5002);


insert into Order_Table values(70009,270,'2021-09-10',3001,5005);
insert into Order_Table values(70002,65,'2021-10-05',3002,5001);
insert into Order_Table values(70004,110,'2021-08-17',3009,5003);
insert into Order_Table values(70007,948,'2021-09-10',3005,5002);
insert into Order_Table values(70005,2400,'2021-07-27',3007,5001);
insert into Order_Table values(70010,1983,'2021-10-10',3004,5006);
insert into Order_Table values(70003,2480,'2021-10-10',3009,5003);
insert into Order_Table values(70008,5760,'2021-09-10',3002,5001);
insert into Order_Table values(70012,250,'2021-06-27',3009,5003);
insert into Order_Table values(70013,3045,'2021-04-25',3002,5001);
insert into Order_Table values(70011,75,'2021-08-17',3003,5007);

INSERT INTO Customer_table (cust_id, cust_name,city, grade, salesman_id)


VALUES
(3002, 'Nandan','chennai', 100, 5001),
(3007, 'sunil','chennai', 200, 5001),
(3005, 'rick', 'hyderabad',200, 5002),
(3008, 'akshay','mumbai', 300, 5002),
(3004, 'jyothi', 'kolkatta',300, 5006),
(3009, 'murali', 'vizaq',100, 5003),
(3003, 'xavier', 'delhi',200, 5007),
(3001, 'rihan','mumbai' ,null , 5005);
SELECT ot.ord_no,
c.cust_name,
ot.cust_id,
ot.salesman_id
FROM Order_Table ot
JOIN Customer_table c ON ot.cust_id = c.cust_id
JOIN Salesman_table s ON ot.salesman_id = s.Salesman_id
WHERE c.city <> s.city;

SELECT ot.ord_no,
ot.purch_amt,
ot.ord_date,
ot.cust_id AS customer_id,
ot.salesman_id
FROM Order_Table ot
WHERE ot.ord_date = '2021-10-10'
AND ot.purch_amt > (
SELECT AVG(purch_amt)
FROM Order_Table
WHERE ord_date = '2021-10-10'
);

SELECT DISTINCT s.Salesman_id, s.name


FROM Salesman_table s
JOIN Customer_table c ON s.Salesman_id = c.salesman_id
JOIN Order_Table o ON c.cust_id = o.cust_id
WHERE (o.purch_amt >= 2000 OR o.purch_amt IS NULL)
AND c.grade IS NOT NULL;
SELECT
ct.cust_name,
ct.city AS customer_city,
ct.grade,
st.name AS salesman,
st.city AS salesman_city
FROM
Customer_table ct
JOIN
Salesman_table st ON ct.salesman_id = st.Salesman_id
WHERE
ct.grade < 300
ORDER BY
ct.cust_id ASC;

SELECT
ct.cust_name AS "customer",
ct.grade AS "Grade",
ot.ord_no AS "OrderNo"
FROM
Customer_table ct
JOIN
Order_Table ot ON ct.cust_id = ot.cust_id
JOIN
Salesman_table st ON ct.salesman_id = st.Salesman_id
WHERE
ct.grade IS NOT NULL
ORDER BY
ct.cust_id, ot.ord_no;
SELECT ot.ord_no,
ot.purch_amt,
ot.ord_date,
ot.cust_id AS customer_id,
ot.salesman_id
FROM Order_Table ot
JOIN (
SELECT cust_id, AVG(purch_amt) AS avg_amount
FROM Order_Table
GROUP BY cust_id
) AS avg_table ON ot.cust_id = avg_table.cust_id
WHERE ot.purch_amt > avg_table.avg_amount;

Output:

1) find those sales people who generated orders for their customers
but not located in the same city . return
ord_no,cust_name,customer_id(orders table) ,salesman_id
(orderstable)
2) from the orders table find the order values greater than the
average order value of 10th october 2021 . return
ord_no,purch_amt,ord_date,customer_id,saleman_id.
3) make a list of the salesman who either work for one
or more customers. The customer may have placed ,
either one or more orders on or above order
amount 2000 and must have a grade

4) from the customer and the sales man table find


those customers who grade is less than 300 REturn
cust_name,customer_city,grade,Salesman,salesman
city. the result should be ordered by ascending
customer_id
5) find those customers where each customer has a
grade and served by a atleast one salesperson who
belongs to acity. return cust_name as
"customer",grade as "Grade",and order_no as
"OrderNo"
6)from the orders table find those orders , which
amount is higher than the average amount of the
related customer. Return
ord_no,purch_amt,ord_date,customer_id,and
salesman_id.

You might also like