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

Additional DBMS Lab

Uploaded by

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

Additional DBMS Lab

Uploaded by

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

1.

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 deleted.

Entity-Relationship Diagram
Schema Diagram

Table Creation

create table SALESMAN (


Salesman_id int,
Name varchar(10),
City varchar(10),
Commission int,
primary key(Salesman_id)
);

create table CUSTOMER (


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
);

create table ORDERS (


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
);
Contents Of Tables

In MySql data format used for date is “yyyy-mm-dd”


Queries:

1. Count the customers with grades above Bangalore’saverage.

select Grade, COUNT (distinct Customer_id) as Total_Customers from CUSTOMER


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 s


where (select COUNT (*) from CUSTOMER 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, a.City, b.Cust_name from SALESMAN a, CUSTOMER


b where a.Salesman_id=b.Salesman_id
and a.City = b.City)
union
(select a.Salesman_id, a.Name, a.City, b.Cust_name from SALESMAN a, CUSTOMER
b where a.Salesman_id=b.Salesman_id
and a.City <> b.City));
4. Create a view that finds the salesman who has the customer with the highest order
of a day.
(NOTE: Execute Query-1 to create a view. Then execute Query-2 to display
that on SQL Command Line console.)

Query-1: Creating view


create view TOPSALESMAN as
select b.Ord_date, b.Purchase_amt,a.Salesman_id, a.Name from SALESMAN a, ORDERS b
where a.Salesman_id = b.Salesman_id
and b.Purchase_amt=(select MAX(c.Purchase_amt) from ORDERS c
where b.Ord_date = c.Ord_date) ;

Query-2: Display the virtual table

select * from TOPSALESMAN;


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

Delete from SALESMAN Where Salesman_Id=1000;

(Note: Before executing this for the exam first display all the table then executes the query
and show the changes it has made in all the table to the customer. Remember only salesmen and
all his order must get deleted not the customer)
2. Consider the schema for Company Database:

EMPLOYEE(SSN, Name, Address, Sex, Salary, SuperSSN, DNo)


DEPARTMENT(DNo, DName, MgrSSN, MgrStartDate)
DLOCATION(DNo,DLoc)
PROJECT(PNo, PName, PLocation, DNo)
WORKS_ON(SSN, PNo, Hours)
Write SQL queries to
1. Make a list of all project numbers for projects that involve an employee whose last name
is ‘Scott’, either as a worker or as a manager of the department that controls the project.
2. Show the resulting salaries if every employee working on the ‘IoT’ project is given a 10
percent raise.
3. Find the sum of the salaries of all employees of the ‘Accounts’ department, as well as the
maximum salary, the minimum salary, and the average salary in this department.
4. Retrieve the name of each employee who works on all the projects controlled by
department number 5 (use NOT EXISTS operator).
5. number and the number of its employees who are making more than Rs. 6, 00,000.

NOTE: Once DEPARTMENT and EMPLOYEE tables are created we must alter
EMPLOYEE table to add foreign key constraint to EMPLOYEE. Dno using sql
command.

TABLE CREATION

create table EMPLOYEE (


Ssn varchar(8),
Fname varchar(10),
Lname varchar(10),
Address varchar(10),
Sex varchar(1),
Salary int,
Sup_Ssn varchar(8),
Dno int,
primary key(Ssn),
foreign key(Sup_Ssn) references EMPLOYEE(Ssn)
);

create table DEPARTMENT (


Dno int,
Dname varchar(20),
MgrSsn varchar(8),
Mgr_sdate date,
primary key(Dno),
foreign key(MgrSsn) references EMPLOYEE (Ssn)
);

NOTE: Once DEPARTMENT and EMPLOYEE tables are created we must alter
EMPLOYEE table to add foreign key constraint to EMPLOYEE.Dno using sql
command.
// FOR MYSQL:

alter table EMPLOYEE add constraint foreign key (Dno) references


DEPARTMENT(Dno);

//ORACLE:

ALTER TABLE EMPLOYEE ADD Dno INT REFERENCES DEPARTMENT(Dno);

create table DLOCATION (


Dno int,
Dloc varchar(20),
primary key (Dno,Dloc),
foreign key(Dno) references DEPARTMENT(Dno)
);

create table PROJECT


( Pno int,
Pname varchar(20),
Plocation varchar(20),
Dno int,
primary key(Pno),
foreign key(Dno) references DEPARTMENT (Dno)
);

create table WORKS_ON


( Pno int,
Ssn varchar(8),
Hours int,
primary key(Ssn,Pno),
foreign key(Ssn) references EMPLOYEE(Ssn), foreign key(Pno) references PROJECT(Pno)
);
Contents of Tables:
QUERIES:

1. Make a list of all project numbers for projects that involve an employee whose last
name is ‘Scott’, either as a worker or as a manager of the department that controls
the project.

(select p.Pno from PROJECT p, DEPARTMENT d, EMPLOYEE e


where p.Dno=d.Dno
and d.MgrSsn=e.Ssn
and e.Lname=’scott’)
UNION
(select p1.Pno from PROJECT p1, WORKS_ON w, EMPLOYEE e1
where p1.Pno=w.Pno
and e1.Ssn=w.Ssn
and e1.Lname=’scott’);

2. Show the resulting salaries if every employee working on the ‘IoT’ project is givena
10 percent raise.

select e.Fname, e.Lname, 1.1*e.Salary as incr_sal from EMPLOYEE e, WORKS_ON w,


PROJECT p
where e.Ssn=w.Ssn
and w.Pno=p.Pno
and p.Pname=’iot’;

3. Find the sum of the salaries of all employees of the ‘Accounts’ department, as well as
the maximum salary, the minimum salary, and the average salary in this
department

select sum(e.Salary) as total_salary, max(e.Salary) as max_salary, min(e.Salary) as


min_salary, avg(e.Salary) as average_salary from EMPLOYEE e, DEPARTMENT d
where e.Dno=d.Dno
and d.dname='account';
4. Retrieve the name of each employee who works on all the projects Controlled by
department number 5 (use NOT EXISTS operator).

select e.Fname,e.Lname from EMPLOYEE e


where NOT EXISTS(select * from WORKS_ON
w
where w.Pno IN (select p.Pno from PROJECT
p where p.Dno=5)
and NOT EXISTS (select * from WORKS_ON
o where o.Ssn=e.Ssn
and o.Pno=w.Pno));

(Note: In project table give only one project controlled by Dept No 5.)

5. For each department that has more than five employees, retrieve the department
number and the number of its employees who are making more than Rs. 6,00,000.

select d.Dno, count (*) from DEPARTMENT d, EMPLOYEE e


where d.Dno=e.Dno
and e.Salary>600000
and d.Dno in (select e1.Dno from EMPLOYEE e1
group by e1.Dno having count
(*)>5)
group by d.Dno;
REFERENCES
[1].Fundamentals of Database Systems, Ramez Elmasri and Shamkant B. Navathe,
7th
Edition, 2017, Pearson.
[2].Database management systems, Ramakrishnan, and Gehrke, 3rd Edition,
2014, McGraw Hill

You might also like