Set A-1
Create the following relations :
Emp(eno,name,dno,salary)
Project(pno,pname,control-dno,budget)
Each employee can work on one or more projects, and a project can have many employees
working in it. The number of hours worked on each project , by an employee also needs to be stored.
Create the Relations accordingly, so that the relationship is handled properly and the relations are in normalized form (3NF).
Assume appropriate data types for the attributes. Add any new attributes , new relations as
required by the queries.
Insert sufficient number of records in the relations / tables with appropriate values as suggested by some of the queries.
Write the queries for following business tasks & execute them.
Ans.
create table emp(
e_no int primary key,
name char(20),
d_no int,
salary int,
city char(20),
gender char
);
create table project(
p_no int primary key,
p_name char(20),
control_dno int,
budget float,
d_name char(20)
);
create table emp_project(
p_no int references project(p_no)on delete cascade,
e_no int references emp(e_no)on delete cascade,no_hours int
);
insert into emp values(01,'ankur',101,49000,'pune','m');
insert into emp values(02,'aniket',102,9000,'karad','m');
insert into emp values(03,'piyush',103,29000,'satara','m');
insert into emp values(04,'prashant',104,21000,'wakad','m');
insert into emp values(05,'omkar',105,2100,'hadapsar','m');
insert into project values(1001,'codec',105,50000,'comp sci');
insert into project values(1002,'dream code',101,50000,'comp sci');
insert into project values(1003,'sci_craft',102,5000,'sci');
insert into project values(1004,'tally',103,50200,'commerce');
insert into project values(1005,'scilab',104,502001,'math');
insert into project values(1006,'sales',104,502001,'math');
Insert into emp_project values(1001,1,2);
Insert into emp_project values(1002,2,3);
Insert into emp_project values(1003,3,4);
Insert into emp_project values(1005,5,5);
insert into emp_project values(1001,2,4);
insert into emp_project values(1001,4,4);
1.list the names of departments that controls projects whose budget is greater than 50000.
Ans.
select d_name from project where budget>50000;
2.list the names of projects, controlled by department No 101, whose budget is greater than
atleast one project controlled by department No 102.
Ans.
select p_name from project where control_dno='101'
and budget>some(select budget from project where control_dno='102') ;
3. list the details of the projects with second maximum budget
Ans.
select* from project where budget=(select max (budget) from
project where budget<(select max(budget)from project)) limit 1;
4. list the details of the projects with third maximum budget.
Ans.
select* from project where budget=(select max (budget) from
project where budget<(select max(budget)from project where
budget<(select max (budget) from project))) limit 1;
5. list the names of employees, working on some projects that employee number __ is working.
Ans.
select name from emp where e_no in(select distinct e_no from
emp_project where p_no in(select e_no from emp_project where e_no='1'));
6. list the names of employees who do not work on any project that employee number __ works on
Ans.
select name from emp where e_no in(select distinct e_no from
emp_project where p_no not in(select e_no from emp_project where e_no='1'));
7. list the names of employees who do not work on any project controlled by ‘comp sci’
department
Ans.
select name from emp,emp_project where emp.e_no=emp_project.e_no and p_no not in
(select p_no from project where d_name='comp sci');
8. list the names of projects along with the controlling department name, for those projects
which has atleast __ employees working on it.
Ans.
select * from project where p_no in(select p_no from emp_project group by p_no having count(e_no)>=1);
9. list the names of employees who is worked for more than 10 hrs on atleast one project
controlled by ‘comp sci’ dept.
Ans.
select name from emp_project,emp where emp.e_no=emp_project.e_no and p_no not in
(select p_no from project where d_name='comp sci' and no_hours>7);
10. list the names of employees , who are males , and earning the maximum salary in their
department.
Ans.
select name from emp_project,emp where emp.e_no=emp_project.e_no and p_no in
(select p_no from project where d_name='comp sci' and no_hours>7);
11. list the names of employees who work in the same department as ‘comp sci’.
Ans.
select emp.name from emp,emp_project,project
where emp.e_no=emp_project.e_no and project.p_no=emp_project.p_no
and d_name='comp sci';
12. list the names of employees who do not live in pune or karad.
Ans.
select * from emp where city!='pune' and city!='karad';