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

Order Database

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

Order Database

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

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 salesman who had
more than one customer.
3. List all the salesman 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
create table salesman
(
salesman_id varchar(5),
name varchar(15),
city varchar(15),
commission int,
primary key(salesman_id)
);
create table customer
(
customer_id varchar(5),
cust_name varchar(15),
city varchar(15),
grade int,
salesman_id varchar(5),
primary key(customer_id),
foreign key(salesman_id) references
salesman(salesman_id) on delete cascade
);
create table orders
(
ord_no varchar(5),
purchase_amt int,
ord_date date,
customer_id varchar(5),
salesman_id varchar(5),
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
);
1. Count the customers with grades above
Bangalore’s average.

select count(*) as count


from customer where grade >
(select avg(grade)
from customer
where city ='Bangalore');
2. Find the name and numbers of all salesman
who had more than one customer.

select s.salesman_id,s.name,count(customer_id)
from salesman s, customer c
where s.salesman_id = c.salesman_id
group by (s.salesman_id,s.name)
having count(customer_id)>1;
3. List all the salesman and indicate those who have and don’t
have customers in their cities (Use UNION operation.)

select name,'exists' as same_city


from salesman s
where city in (select city
from customer
where s.salesman_id = salesman_id)
union
select name,'not exists' as same_city
from salesman s
where city not in (select city
from customer
where s.salesman_id = salesman_id);
4. Create a view that finds the salesman who has the
customer with the highest order of a day.
create view highest_order as
select s.salesman_id,s.name,o.purchase_amt,o.ord_date
from salesman s, orders o
where s. salesman_id = o.salesman_id;

select name,ord_date
from highest_order h
where purchase_amt =
(select max(purchase_amt)
from highest_order
where h.ord_date = ord_date);
• Demonstrate the DELETE operation by
removing salesman with id 1000. All his orders
must also be deleted.
• delete from salesman where salesman_id =3;

You might also like