DBMS Assignment
by Saveen Dhiman
1. Write an SQL query to determine the 5th highest salary from employee table without using TOP
or limit method.
select salary from employee as e where 4 =(select count(distinct(salary)) from employee as e1
where [Link] > [Link]);
2. Retrieve the first and last names of employees with the same salary.
select distinct [Link],[Link] from employee as E, employee as W where [Link]=[Link]
and [Link]!=[Link];
3. Retrieve department number of departments that have less than five employees in it.
select dnumber as dno_having_less_employee from department where (select count(*) from employee
where dno=dnumber) <5;
4. Retrieve the names of employees who make at least $10,000 more than the employee who is paid
the least in the company.
select fname,lname from employee where salary>=(select min(salary) from employee)+10000;
5. Retrieve the number of male employees in each department.
select dno,count(ssn) from employee where sex='M' group by dno;
6. Retrieve the first and last names and department number and name of all employees directly
supervised by James Borg. Show results in ascending alpha order (by last name and then first
name).
select fname,lname,dno,dname from employee,department where super_ssn =(select ssn from
employee where fname='James' and lname='Borg') and dno=dnumber order by lname,fname;
7. Retrieve the name and number of departments which have employees who do not work on at least
one project. Show results in ascending alpha order. (NOTE: a department should appear on this
list if it has an employee who does not work on any project at all.)
select distinct([Link]),[Link] from department as d, employee as e where [Link]=[Link] and
[Link] in (select essn from works_on group by essn having count(pno)<(select count(pnumber) from
project)) order by [Link],[Link];
8. For each department, list the department name and the total number of hours assigned to projects
controlled by the department (irrespective of the employee to whom they are assigned) and the
total number of hours assigned to employees of the department (irrespective of the project
involved). Show results in ascending alpha order.
select dname,hours_for_project,hours_for_employee from department D, (select dnum,sum(hours) as
hours_for_project from project,works_on where pnumber=pno group by dnum) p, (select dno,sum(hours)
as hours_for_employee from employee,works_on where ssn=essn group by dno) E where [Link] =
[Link] and Dnumber = [Link] order by [Link] asc;
9. Retrieve the names of departments which have at least one project which employs every one of
the employees of the department that controls the project. Also show the name of the project.
Show results in ascending alpha order.
select [Link],pname from department D, (select pname,count(essn) as emp_proj,dnum from
project,works_on,employee where pnumber = pno and dno=dnum and essn=ssn group by dnum,pnumber)
P, (select count(ssn) as emp_dept,dno from employee group by dno) E where P.emp_proj = E.emp_dept
and [Link] = [Link] and [Link] = [Link];
10. Retrieve the first and last names of employees who work on projects which are not controlled by
their departments. Also show the names of the projects, the employee's department number, and
the number of the project's controlling department. (All of this should be shown in the same result
table.) Show results in ascending alpha order (by last name and then first name and then project
name).
select [Link], [Link], [Link] as project_name ,[Link] as controlling_dept,[Link] as dept_number
from employee e, project p, works_on w where [Link]=[Link] and [Link]=[Link] and dno!=dnum order
by [Link],[Link],[Link];
11. Retrieve the first and last names of employees who work on more than the average number of
projects. (Note: employees who do not work on any project are to be included in the average.)
Display their names, the number of projects they work on, and the average number of projects.
(The same average should be repeated in each row.) Show results in ascending alpha order (by
last name and then first name). [The average number of projects is the average number of projects
worked on per employee.]
select distinct [Link], [Link], r.proj_count as project_count , s.avg_proj as average_project from
employee as e, works_on as w, (select avg([Link]) as avg_proj from (select count(pno) as pnum from
works_on group by essn) as q) as s, (select count(pno) as proj_count,essn from works_on group by essn)
as r where [Link] = [Link] and r.proj_count>s.avg_proj and [Link] = [Link] order by [Link],[Link];
12. Retrieve the name and number of the project which uses the most employees. Also show the total
number of employees for that project. If there is more than one project that has attained that
maximum, list them all. Show results in ascending alpha order.
select [Link], [Link], b.emp_count from project as p, (select pno,count(*) as emp_count from
works_on group by pno having count(essn) = (select max(a.emp_count) from (select count(*) as
emp_count from works_on group by pno) as a)) as b where [Link] = [Link] order by [Link], [Link];
13. Do any departments have a location in which they have no projects? Retrieve the names of
departments which have at least one location which is not the same as any of the locations of the
department's projects. Show results in ascending alpha order. [This means that one department
location is different from every location of every project of that department.]
select [Link],[Link] from department as d,dept_locations as l where [Link] not in
(select plocation from project as p where [Link]=[Link]) and [Link]=[Link] order
by dname asc;
14. List the names of dependents that have the same first name as an employee of whom they are not
the dependent. Also show the ssn of the employee with the same first name and the ssn of the
employee on whom the dependent is dependent ([Link]). (All of this should be shown in
the same table.) Show results in ascending alpha order.
select dependent_name, ssn as same_name_emp, essn as depend_upon from dependent, employee
where dependent_name = fname and ssn!=essn order by dependent_name;
15. Retrieve the first and last names of employees whose supervisor works on any project outside the
employee's department. Show results in ascending alpha order (by last name and then first name).
[Note that you are to retrieve the employee's name, not the supervisor's.]
select distinct (fname),lname from employee,project,works_on where dno != dnum and super_ssn =
essn and pno = pnumber order by lname ,fname;