Vous êtes sur la page 1sur 5

Database II Assignment #3

1. Use subqueries to list the surname of employees who have been on a


course with employee 19.
select surname from employee where empno in (select distinct empno from
empcourse where courseno in (select courseno from empcourse where
empno=19)) and empno!=19

2. List the surname of those who earn less than the average salary of all
staff
select surname from employee where empno in (select empno from jobhistory
where salary < (select avg(salary) from jobhistory where enddate is null) and
enddate is null)

3. List the surname of employees who earn more than the average salary
of employees who have jobs connected with Accounts.
select surname from employee where empno in (select empno from
jobhistory where salary > (select avg(salary) from jobhistory where position
like '%Account%' and enddate is null) and enddate is null)

4. List the surname and forenames of employees who earn more than any
employee connected to Admin.
select surname,forenames from employee where empno in (select empno
from jobhistory where salary > (select min(salary) from jobhistory where
position like '%Admin%' and enddate is null) and enddate is null)

5. List the surname and position of employees earning less than everyone
in department 3.
select surname,position from employee e join jobhistory j on
e.empno=j.empno where salary < (select min(salary) from jobhistory where
empno in (select empno from employee where depno=3) and enddate is
null) and enddate is null

Database II Assignment #3
6. List the surname of employees who has been on a course which
employee number 14 has not been on.
select surname from employee where empno in (select distinct empno from
empcourse where courseno not in (select courseno from empcourse where
empno=14))

7. List the surname of employees who are currently doing a unique job.
select distinct e.surname from employee e
join jobhistory js on js.empno = e.empno
where position in
(
select position from jobhistory
where enddate is null
group by position
having count(*) = 1
)
and js.enddate is null
8. List the surname of employees who have done or are doing any job
which employee 23 has done or is doing. Include the surname of employee
23.
select surname from employee where empno in (select distinct empno from
jobhistory where position in (select position from jobhistory where empno=23))
9. List the course names of courses which employees from department
number 2 have been on.
select cname from course where courseno in (select courseno from
empcourse where empno in (select empno from employee where depno=2))

10. List the employee surname, forenames, and course for each time an
employee attended a course at which no other colleague was present.

Database II Assignment #3
select surname, forenames, c.cname from employee e
join empcourse ec on e.empno = ec.empno
join course c on ec.courseno = c.courseno
where c.cname in
(
select cname from empcourse ec
join course c on ec.courseno = c.courseno
group by cname
having count(*) = 1
)
11. List the full names and positions of employees who have been on any of
the courses that Robert Roberts has been on. Use subselect.
select surname,forenames,position from employee e join jobhistory j on
e.empno=j.empno where e.empno in (select empno from empcourse where
courseno in(select courseno from empcourse where empno in (select empno
from employee where surname='Roberts' and forenames='Robert'))and
surname!='Roberts' and forenames!='Robert') and j.enddate is null
or
select distinct surname, forenames, js.position from employee e
Join empcourse ec on e.empno = ec.empno
join course c on ec.courseno = c.courseno
join jobhistory js on e.empno = js.empno
where c.cname in
(select c.cname from employee e
Join empcourse ec on e.empno = ec.empno
join course c on ec.courseno = c.courseno
where e.surname = 'Roberts' and forenames = 'Robert')

Database II Assignment #3
and js.enddate is null and e.surname except 'Roberts' and forenames except
'Robert'

12. List the employee numbers and telephone numbers of all staff in
department 3 or 1. Make use of UNION
select empno,telno from employee where depno=3
union
select empno,telno from employee where depno=1

13. List the employee number of the head of department 4 and the employee
numbers of all staff in department 4 in a single table using union.
select head from department where depno=4
union
select empno from employee where depno=4

14. List the full name of everyone living in Edinburgh or who is in accounts.
Perform this task using UNION.
select surname,forenames from employee where address like '%Edinburgh%'
union
(select surname,forenames from employee where empno in(select empno
from jobhistory where position like '%Account%' and enddate is null))

Database II Assignment #3
15. Write a query which shows which empno has been to the most courses.
List the empno and the number of courses that person has been on. There
may be multiple employees with the biggest count.

SELECT empno,count(*)
from empcourse
group by empno
having count(*) = (
select max(count(*))
from empcourse
group by empno
);

16. Write a query which shows which employee has had the most jobs with
the company. There may be multiple employees with the biggest job count.
Show the surname,forename, and the job count in your answer.

select surname, forenames, cnt from employee e join (SELECT


empno,count(*) as cnt
from jobhistory
group by empno
having count(*) = (
select max(count(*))
from jobhistory
group by empno
)) j
on e.empno=j.empno

Vous aimerez peut-être aussi