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

DBMS Lab Program 2

The document describes an order database schema with tables for salesmen, customers, and orders. It then 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 if they have customers in their city using UNION 4. Create a view to find the top salesman by order amount each day 5. Demonstrate deleting a salesman and their orders

Uploaded by

amazing video
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

DBMS Lab Program 2

The document describes an order database schema with tables for salesmen, customers, and orders. It then 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 if they have customers in their city using UNION 4. Create a view to find the top salesman by order amount each day 5. Demonstrate deleting a salesman and their orders

Uploaded by

amazing video
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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.
Entity-Relationship Diagram

Schema Diagram
Table Creation
create table SALESMAN_dbms (
Salesman_id int, Name varchar(10), City varchar(10), Commission int,
primary key(Salesman_id));

select * from SALESMAN_dbms;

create table CUSTOMER_dbms(


Customer_id int, Cust_name varchar(10), City varchar(10),
Grade int, Salesman_id int,
primary key (Customer_id),
foreign key (Salesman_id) references SALESMAN (Salesman_id) on delete set
NULL);

select * from CUSTOMER_dbms;

create table ORDERS_dbms(


Ord_no int, Purchase_amt int, Ord_date date, Customer_id int,Salesman_id int,
primary key (Ord_no),
foreign key (Customer_id) references CUSTOMER (customer_id) on delete
cascade,
foreign key (Salesman_id) references SALESMAN (salesman_id) on delete
cascade);

select * from ORDERS_dbms;

Insertion Of Values To Tables:

Salesman Details:

insert into SALESMAN_dbms values (1000, 'joseph','mysore','13');


insert into SALESMAN_dbms values (1001, 'girish','bangalore','22');
insert into SALESMAN_dbms values (1002, 'mukund','mumbai','16');
insert into SALESMAN_dbms values (1003, 'saurabh','delhi','19');
insert into SALESMAN_dbms values (1004, 'srinivas','hydrabad','23');
insert into SALESMAN_dbms values (1005, 'mohan','ranchi','23');
Customer Details:

insert into CUSTOMER_dbms values (1, 'sharal','hydrabad',40,1004);


insert into CUSTOMER_dbms values (2,'meenakshi','mangalore',40,1000);
insert into CUSTOMER_dbms values (3,'vikky','mumbai',35,1002);
insert into CUSTOMER_dbms values (4, 'john','mumbai',20,1002);
insert into CUSTOMER_dbms values (5, 'george','bangalore',10,1001);
insert into CUSTOMER_dbms values (6, 'hevin','bangalore',50,1001);
insert into CUSTOMER_dbms values (7, 'roshan','delhi',45,1003);
insert into CUSTOMER_dbms values (8, 'vimala','chennai',35,1001);
insert into CUSTOMER_dbms values (9, 'nakul','ayodhya',15,1005);

Order Details:

insert into ORDERS_dbms values (111, 50000, '04-jan-17', 1, 1004);


insert into ORDERS_dbms values (222, 45000, '04-jan-17', 2, 1000);
insert into ORDERS_dbms values (333, 10000, '05-feb-17', 3, 1002);
insert into ORDERS_dbms values (444, 35000, '13-mar-17', 4, 1003);
insert into ORDERS_dbms values (555, 75000, '14-mar-17', 5, 1001);
insert into ORDERS_dbms values (666, 25000, '14-mar-17', 6, 1004);
insert into ORDERS_dbms values (777, 5000, '27-jun-17', 7, 1003);
insert into ORDERS_dbms values (888, 52000, '25-aug-17', 8, 1001);
insert into ORDERS_dbms values (991, 37000, '25-aug-17', 1, 1004);
insert into ORDERS_dbms values (992, 29000, '09-sep-17', 2, 1000);
insert into ORDERS_dbms values (993, 6000, '09-sep-17', 9, 1005);

Queries:

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

select Grade, COUNT (distinct Customer_id) as Total_Customers


from CUSTOMER_dbms
group by Grade
having Grade > (select AVG(Grade)
from CUSTOMER
where City='bangalore');
2. Find the name and numbers of all salesmen who had more than one customer.

Select s.Salesman_id, s.Name


from SALESMAN_dbms s
where (select COUNT (*)
from CUSTOMER_dbms c
where c.Salesman_id=s.Salesman_id) > 1;

3. List all salesmen and indicate those who have and don’t have customers
in their cities (Use UNION operation.)

(select a.Salesman_id, a.Name, b.Cust_name, a.Commission, a.City


from SALESMAN_dbms a, CUSTOMER_dbms b
where a.City = b.City)
UNION
(select Salesman_id, Name, 'No Match', Commission, City
from SALESMAN_dbms
where NOT City = ANY (select City from CUSTOMER_dbms))
ORDER BY 2 DESC;

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

create view TOPSALESMAN_dbms as


select b.Ord_date,b.Purchase_amt,a.Salesman_id, a.Name
from SALESMAN_dbms a, ORDERS_dbms b
where a.Salesman_id = b.Salesman_id
and b.Purchase_amt=(select MAX(c.Purchase_amt)
from ORDERS_dbms c
where b.Ord_date = c.Ord_date) ;

select * from TOPSALESMAN_dbms;

5. Demonstrate the DELETE operation by removing salesman with id


1000. All his orders must also be deleted.

Delete from SALESMAN_dbms Where Salesman_Id=1000;

You might also like