Sie sind auf Seite 1von 11

SQL Exercise

(Time limit: 2hrs)


The following tables represent data about employees, skills, projects, project teams.
The columns, their description and some sample data is provided. (If value is blank, treat
them as null)

Main Tables
Employee
emp_id
Unique
identifier

emp_name
Name of
employee

3251
4562
9812

Mark
Ryan
Brian

Employee_Skills
emp_id
Unique
identifier of
employee
whose skills
and proficiency
is recorded.
Refers to
employee
master table.
3251
3251
4562
Project
project_id
Unique
identifier of
project whose
team is enlisted
in the table

Designation
code that
represents the
designation.
Refers to
designation
master (lookup)
table
C2
C1
C1

join_date
Date of joining

skill_id
Unique
identifier of
skill for which
employees
proficiency
level is
recorded.
Refers to skill
master (lookup)
table
C
JAV
DES

date_effective
Date when
employee got
the skill

prof_level
Proficiency
level of given
employee for
given skill.
Takes values
E0 thru E4

exp_years
No. of years of
experience the
employee has
on the given
skill

12-12-2006
12-12-2004
12-12-2008

E1
E2
E0

3
4
0

date_start
Date when
project started

date_end
Date when
project ended /
ends

Name
Name of project

12-1-2004
12-12-2000
12-12-2008

supervisor_id
Employee id of
supervisor

3251
4562

2056
2078

1-1-2008
1-1-2007

Project_Team
emp_id
Unique
identifier of
employee
worked /works
in the project.
Refers to
employee table
3251
3251
4562
3251

project_id
Unique
identifier of
project whose
team is enlisted
in the table.
Refers to
Projects table
2056
2056
2056
2078

12-12-2007

Google Testing
Yahoo Development

date_start
Date when
employee got
into project in
the given role

date_end
Date when
employee was
released from
project if any

role_id
Role played by
employee in
project

1-1-2008
12-12-2008
12-12-2008
1-1-2007

11-12-2008

DES
MGR
DEV
DES

12-12-2007

Master Tables
Skills
skill_id
C
JAV
DES
TEST
OOP

Name
C Programming
Java Programming
Software Design
Software Testing
Object Oriented Programming

Designation
Designation
C1
C2
C3
C4
VP

Name
Engineer
Analyst
Consultant
Director
Vice President

Role
role_id
DES
MGR
DEV
SC
TST

Name
Designer
Manager
Developer
Site Coordinator
Tester

Answer the following

For all questions following standard procedure starting from


defining the problem to showing sample output / dry run.
Do not forget to explicitly articulate any assumptions you make
Add sufficient sample data to the above data (do not remove any
existing data) so that you can demonstrate good output for all the
following SQL and DML that you will design.
Inputs are specifically left as generic with examples so that youmay
demonstrate results for multiple inputs where needed

1. Find the names of all employees who are


currently in projects and are whose
designation is as given (say C1)
STEP 1: Problem Definiton:
o Context
a. Employee table has employee details name and designation
b. Project_Team has details of all employees who were / are
working on a given project
c. If date_end is null, then the project is the currently active
project for the given employee
o Input A designation code
o Output List of names of employees
a. With the given designation
b. Who are currently active in at least one project
o Special considerations
a. Same employee is active on multiple projects?
b. Multiple employees match the requirement
c. No employees match the requirement
o Sample scenario - Input C1 => Output Ryan
STEP 2: Solution Approach:
Find employees who are currently active:
o Criteria end_date is null in project_team table
o Output required for further matching emp_id
o Intermediate solution:
SELECT emp_id from project_team
where date_end is null
Get employee name based on previously got ids
o Criteria designation is as given in input on the employee
table

o Addl. Criteria emp_id in output of previous query


o Output required: emp_name
o Intermediate solution:
select emp_name from employee where designation = ?
and emp_id in ?previous_output?
NOTE: Approach can be reversed to filter first by designation as well
STEP 3: Sample Test Cases
1. Input C2 => Output Mark
2. Input C1 => Output Ryan
3. Input VP => Output None
NOTE
Missing test case: multiple output scenario
Need to add data to demonstrate multiple records
This step can also be interchanged with step 2
STEP 4: Dry Run
1. Intermediate result of query irrespective of input is 3251, 4562
2. Mapped names are: Mark, Ryan
3. If input is
a. C1 further filter on employee returns Ryan
b. C2 further filter on employee returns Mark
c. VP - further filter on employee returns no result
NOTE: Dry run will vary if approach is reversed
STEP 5: Translation Approach
The translation approach is depicted as sub-bullets (Intermediate Solution) in
STEP 2: Solution approach.
NOTE:
Though intermediate solution is inserted above it is expected that the part is
filled only at this point of time. Insertion is done above just to show mapping
between these steps
Also note that intermediate solution testing is skipped as this is a solution for a
paper test.
STEP 6: Final Query
select emp_name from employee where designation = 'C1'
and emp_id in (SELECT emp_id from project_team where date_end is null)

OR
select distinct emp_name from employee e inner join project_team pe
on pe.emp_id = e.emp_id where designation = 'C1'
and date_end is null
OR
select emp_name from employee e inner join
(SELECT emp_id, date_end from project_team where date_end is null) pe
on pe.emp_id = e.emp_id
where designation = 'C1' and date_end is null
STEP 7: Final Testing
This step is skipped as this solution is for a paper test with no access to actual
database.

2. For each project, find the number of


employees who have a particular skill (say
JAV).
STEP 1: Problem Definiton:
o Context
a. Employee_Skills table has details about skills of a given
employee
b. Project_Team table has details of all employees who were / are
working on a given project
c. Project table has name of project
o Input A skill id
o Output List of names of projects and corresponding number of
employees
a. with the given skill
o Special considerations
a. The project may have no team at all
b. None of the team may have the skill
c. Multiple employees have skills in the same project
d. The same employee may have been on multiple roles in a given
project - - in this case he should not be double counted
e. The same employee may have worked on multiple projects in
this case double counting is fine.
o Sample scenario - Input JAV => Output
a. Google Testing 1
b. Yahoo Development
1
o Sample scenario - Input DES => Output
a. Google Testing 1
b. Yahoo Development
0

o
STEP 6: Final Query
SELECT p.name, count(distinct pe.emp_id)
from project p left outer join project_team pe
on p.project_id = pe.project_id left outer join employee_skills es
on es.emp_id = pe.emp_id where skill_id = 'JAV' or skill_id is null
group by p.name
OR
SELECT p.name, count(emp_id) from project p left outer join
(SELECT distinct project_id, emp_id from project_team where emp_id in
(select emp_id from employee_skills where skill_id = 'JAV')) pe
on p.project_id = pe.project_id group by p.name
OR
SELECT p.name, count(pe.emp_id) from project p left outer join
(SELECT distinct project_id, pt.emp_id from project_team pt
INNER JOIN employee_skills es ON es.emp_id = pt.emp_id
where skill_id = 'JAV') pe
on p.project_id = pe.project_id group by p.name
OR
Other equivalent
Typical Mistakes
o If count(*) is done instead then all places where 0 is expected will
return 1
o If outer join not done some records will be skipped
o While doing outer join if OR skill_id is null check is not added
some records will be skipped
o If group by not done correctly or if additional fields selected etc,
query will not execute
o and many more

3. Find all people (name and total experience


in given skill) who have a particular skill at
a particular proficiency (say JAV E2)
who are not currently in any project
STEP 6: Final Query
SELECT emp_name, exp_years from employee e
inner join employee_skills es on e.emp_id = es.emp_id
where skill_id = 'JAV' and prof_level = 'E2'

and NOT EXISTS (SELECT 1 FROM project_team pt


where pt.emp_id = e.emp_id and date_end is null)
OR
SELECT emp_name, exp_years from employee e
inner join employee_skills es on e.emp_id = es.emp_id
where skill_id = 'JAV' and prof_level = 'E2'
and e.emp_id not in (SELECT emp_id from project_team
where date_end is null)
OR
Other equivalent
Typical Mistakes
o
o and many more

4. Find all people (name of employee,


designation name, project name and total
experience in given skill) who have a
particular skill at a particular proficiency
(say JAV E2);
STEP 6: Final Query
SELECT emp_name, exp_years, d.name, p.name from employee e
inner join employee_skills es on e.emp_id = es.emp_id
left outer join designation d on d.designation = e.designation
left outer join project_team pt on pt.emp_id = e.emp_id
left outer join project p on pt.project_id = p.project_id
where skill_id = 'JAV' and prof_level = 'E2' and pt.date_end is null
OR
Other equivalent
Typical Mistakes
o If date_end criteria is not used the same employee will appear
multiple times for each of the past projects (A valid assumption is
that there is a max of one current project in which employee
works)
o If inner join done on project_team or project, then people not in
any projects will not be listed
o While it may be okay to assume that there are matching
designation rows and hence inner join is also okay inner
join on projects is not acceptable

o and many more

5. Find all employees who report to an


employee (say Ryan) who are not currently
in any project
STEP 6: Final Query
select e.emp_name from employee e
inner join employee sup on e.supervisor_id = sup.emp_id
where sup.emp_name = Ryan
and e.emp_id not in (SELECT emp_id from project_team
where date_end is null)

6. Find the employees (name, designation,


current project name and role name if any)
who are second level reportees of a given
employee (say Mark)
STEP 6: Final Query
select e.emp_name, d.name, p.name, r.name from
employee e inner join employee sup on e.supervisor_id = sup.emp_id
inner join employee sup2 on sup.supervisor_id = sup2.emp_id
left outer join designation d on d.designation = e.designation
left outer join project_team pt on pt.emp_id = e.emp_id
and pt.date_end is null
left outer join project p on pt.project_id = p.project_id
left outer join role r on pt.role_id = r.role_id
where sup2.emp_name = 'Mark'

7. Create report with the following details of


all employees (employee id, name,
designation, supervisor id, supervisor name,
supervisor designation) List them by

designations (higher levels first) and within


that all names should be alphabetical.
STEP 6: Final Query
select e.emp_id, e.emp_name, e.designation,
sup.emp_id, sup.emp_name, sup.designation
from employee e left outer join employee sup
on e.supervisor_id = sup.emp_id
order by e.designation desc, e.emp_name

8. Provide complete details of all current and


previous projects of all employees reporting
to a given employee (say Mark)
STEP 6: Final Query
select e.*, p.*, pt.*
from employee e inner join employee sup
on e.supervisor_id = sup.emp_id
left outer join project_team pt on pt.emp_id = e.emp_id
left outer join project p on pt.project_id = p.project_id
where s.emp_name = Mark

9. An employee (say Mark) is handing over


charge to a new employee (say Terry).
Write the necessary DML to add the new
employee details and change the hierarchy
of all reportees of the original employee to
the new employee.
STEP 6: Final Query
INSERT INTO EMPLOYEE .;
UPDATE EMPLOYEE SET ..

10. Check for all projects that have been


ended in the past (end_date not null) and
update the project role end date of all
employees who are active (end_date null) to
the actual end date of the project.
STEP 6: Final Query
update project_team pt, project p
set pt.date_end = p.date_end
where p.project_id = pt.project_id
and pt.date_end is null and p.date_end is not null

11. Find the employee who has the


maximum number of direct reportees.
STEP 6: Final Query
select s.emp_id, s.emp_name
from employee s inner join employee e on e.supervisor_id = s.emp_id
group by s.emp_id, s.emp_name
having count(*) = (
select max(cnt) from
(select count(*) cnt from employee
where supervisor_id is not null
group by supervisor_id) emp_cnt )

12. Find the employees who have worked


on the same project in the most number of
roles.
STEP 6: Final Query
select distinct e.emp_id, emp_name from project_team pt
inner join employee e on e.emp_id = pt.emp_id
group by e.emp_id, emp_name, project_id

HAVING count(distinct role_id) =


(SELECT max(num_roles) max_roles FROM
(SELECT emp_id, project_id, count(distinct role_id) num_roles
from project_team
group by emp_id, project_id) emp_roles)

Das könnte Ihnen auch gefallen