Example table

download Example table

of 19

Transcript of Example table

  • 7/30/2019 Example table

    1/19

    Learning Oracle SQL

    Your precious replies and comments will be highly appreciated. Thanks.

    [Note : If any queries or doubts, please post it in the comments in this blog]

    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_Portfolio

    emp_id emp_name Designation join_date supervisor_id

    Uniqueidentifier

    Name ofemployee

    code that represents the designation.Refers to designation master (lookup)

    table

    Date ofoining

    Employee id ofsupervisor

    3251 Mark C2 12-1-2004

    4562 Ryan C1 12-12-2000 3251

    9812 Brian C1 12-12-2008 4562

    CREATE:Code:SQL> create table employee_portfolio (emp_id varchar2(10),emp_name

    varchar2(30),designation varchar2(20) ,join_date date,supervisor_id varchar2(20), foreign key(designation)references master_designation_details(designation),unique(emp_id));

    Table created.

    INSERTCode:SQL> insert into employee_portfolio values('&emp_id','&emp_name','&designation',to_date('&join_date','dd-mm-yyyy'),'&supervisor_id');

    Enter value for emp_id: 3251

    Enter value for emp_name: MarkEnter value for designation: C2Enter value for join_date: 12-1-2004Enter value for supervisor_id:old 1: insert into employee_portfolio values('&emp_id','&emp_name','&designation',to_date('&join_new 1: insert into employee_portfolio values('3251','Mark','C2',to_date('12-1-2004','dd-mm-yyyy')

    1 row created.

    http://techforum4u.com/OracleLinks.phphttp://techforum4u.com/OracleLinks.phphttp://techforum4u.com/OracleLinks.php
  • 7/30/2019 Example table

    2/19

    SQL> /Enter value for emp_id: 4562Enter value for emp_name: RyanEnter value for designation: C1Enter value for join_date: 12-12-2000Enter value for supervisor_id: 3251old 1: insert into employee_portfolio values('&emp_id','&emp_name','&designation',to_date('&join_new 1: insert into employee_portfolio values('4562','Ryan','C1',to_date('12-12-2000','dd-mm-yyyy'

    1 row created.

    SQL> /Enter value for emp_id: 9812Enter value for emp_name: BrianEnter value for designation: C1Enter value for join_date: 12-12-2008Enter value for supervisor_id: 4562

    old 1: insert into employee_portfolio values('&emp_id','&emp_name','&designation',to_date('&join_new 1: insert into employee_portfolio values('9812','Brian','C1',to_date('12-12-2008','dd-mm-yyyy

    1 row created.

    SELECTCode:SQL> select * from employee_portfolio;

    EMP_ID EMP_NAME DESIGNATION JOIN_DATESUPERVISOR_ID---------- ------------------------------ -------------------- --------- ----

    ----------------3251 Mark C2 12-JAN-044562 Ryan C1 12-DEC-00 32519812 Brian C1 12-DEC-08 4562

    Employee_Skill

    emp_id skill_id date_effective prof_level exp_years

    Unique identifier of

    employee whose

    skills androficiency is

    recorded. Refers to

    employee master

    table.

    Unique identifier of

    skill for which

    employeesroficiency level is

    recorded. Refers to

    skill master (lookup)

    table

    Date when

    employee got

    the skill

    Proficiency level

    of given employee

    or given skill.Takes values E0

    thru E4

    No. of years of

    experience the

    employee hason the given

    skill

    3251 C 12-12-2006 E1 3

    3251 JAV 12-12-2004 E2 4

    4562 DES 12-12-2008 E0 0

    CREATE

  • 7/30/2019 Example table

    3/19

    Code:SQL> create table employee_skill (emp_id varchar2(20),skill_idvarchar2(30),date_effective date,prof_level varchar2(20),exp_years varchar2(20),foreign key(emp_id) referencesemployee_portfolio(emp_id));

    Table created.INSERTCode:SQL> insert into employee_skill values('&emp_id','&skill_id',to_date('&date_effective','dd-mm-yyyy'),'&prof_level','&exp_years');Enter value for emp_id: 3251Enter value for skill_id: CEnter value for date_effective: 12-12-2006Enter value for prof_level: E1Enter value for exp_years: 3old 1: insert into employee_skill values('&emp_id','&skill_id',to_date('&date_effective','dd-mm-y

    new 1: insert into employee_skill values ('3251','C',to_date('12-12-2006','dd-mm-yyyy'),'E1','3')

    1 row created.

    SQL> /Enter value for emp_id: 3251Enter value for skill_id: JAVEnter value for date_effective: 12-12-2004Enter value for prof_level: E2Enter value for exp_years: 4old 1: insert into employee_skill values('&emp_id','&skill_id',to_date('&date_effective','dd-mm-ynew 1: insert into employee_skill values ('3251','JAV',to_date('12-12-

    2004','dd-mm-yyyy'),'E2','4'

    1 row created.

    SQL> /Enter value for emp_id: 4562Enter value for skill_id: DESEnter value for date_effective: 12-12-2008Enter value for prof_level: E0Enter value for exp_years: 0old 1: insert into employee_skill values('&emp_id','&skill_id',to_date('&date_effective','dd-mm-ynew 1: insert into employee_skill values ('4562','DES',to_date('12-12-

    2008','dd-mm-yyyy'),'E0','0'

    1 row created.

    SELECTCode:SQL> select * from employee_skill;

    EMP_ID SKILL_ID DATE_EFFE PROF_LEVELEXP_YEARS

  • 7/30/2019 Example table

    4/19

    -------------------- ------------------------------ --------- -------------------- -----------------3251 C 12-DEC-06 E133251 JAV 12-DEC-04 E244562 DES 12-DEC-08 E00

    Employee_Project

    project_id date_start date_end Name

    Unique identifier of project whose

    team is enlisted in the table

    Date when project

    started

    Date when project

    ended / ends

    Name of project

    2056 1-1-2008 Google Testing

    2078 1-1-2007 12-12-2007 YahooDevelopment

    CREATECode:SQL> create table employee_project(project_id varchar2(20),date_startdate,date_end date,Name varchar2(30));

    Table created.

    INSERTCode:SQL> insert into employee_projectvalues('&project_id',to_date('&date_start','dd-mm-yyyy'),to_date('&date_end','dd-mm-yyyy'),'&Name');Enter value for project_id: 2056Enter value for date_start: 1-1-2008

    Enter value for date_end:Enter value for name: Google Testingold 1: insert into employee_projectvalues('&project_id',to_date('&date_start','dd-mm-yyyy'),to_danew 1: insert into employee_project values('2056',to_date('1-1-2008','dd-mm-yyyy'),to_date('','dd-

    1 row created.

    SQL> /Enter value for project_id: 2078Enter value for date_start: 1-1-2007Enter value for date_end: 12-12-2007

    Enter value for name: Yahoo Developmentold 1: insert into employee_projectvalues('&project_id',to_date('&date_start','dd-mm-yyyy'),to_danew 1: insert into employee_project values('2078',to_date('1-1-2007','dd-mm-yyyy'),to_date('12-12-

    1 row created.

    SELECTCode:

  • 7/30/2019 Example table

    5/19

    SQL> select * from employee_project;

    PROJECT_ID DATE_STAR DATE_END NAME-------------------- --------- --------- ------------------------------2056 01-JAN-08 Google Testing2078 01-JAN-07 12-DEC-07 Yahoo Development

    Employee_Project_Team

    emp_id project_id date_start date_end role_id

    Unique identifier of

    employee worked

    /works in the project.

    Refers to employeetable

    Unique identifier of

    roject whose team is

    enlisted in the table.

    Refers to Projects table

    Date when

    employee got into

    roject in the

    given role

    Date when

    employee was

    released from

    roject if any

    Role played

    by employee

    in project

    3251 2056 1-1-2008 11-12-2008 DES

    3251 2056 12-12-2008 MGR

    4562 2056 12-12-2008 DEV

    3251 2078 1-1-2007 12-12-2007 DES

    CREATECode:SQL> create table Employee_project_team(emp_id varchar2(20),project_idvarchar2(30),date_start date,date_end date,role_id varchar2(20),foreign key(emp_id) referencesEmployee_portfolio(emp_id),foreignkey (project_id) references employee_project(project_id));

    Table created.

    INSERTCode:SQL> insert into employee_project_teamvalues('&emp_id','&project_id',to_date('&date_start','dd-mm-yyyy'),to_date('&date_end','dd-mm-yyyy'),'&role_id');Enter value for emp_id: 3251Enter value for project_id: 2056Enter value for date_start: 1-1-2008Enter value for date_end: 11-12-2008Enter value for role_id: DESold 1: insert into employee_project_teamvalues('&emp_id','&project_id',to_date('&date_start','dd-new 1: insert into employee_project_team values('3251','2056',to_date('1-1-2008','dd-mm-yyyy'),to_

    1 row created.

    SQL> /Enter value for emp_id: 3251Enter value for project_id: 2056Enter value for date_start: 12-12-2008Enter value for date_end:Enter value for role_id: MGRold 1: insert into employee_project_teamvalues('&emp_id','&project_id',to_date('&date_start','dd-

  • 7/30/2019 Example table

    6/19

    new 1: insert into employee_project_team values('3251','2056',to_date('12-12-2008','dd-mm-yyyy'),t

    1 row created.

    SQL> /Enter value for emp_id: 4562Enter value for project_id: 2056Enter value for date_start: 12-12-2008Enter value for date_end:Enter value for role_id: DEVold 1: insert into employee_project_teamvalues('&emp_id','&project_id',to_date('&date_start','dd-new 1: insert into employee_project_team values('4562','2056',to_date('12-12-2008','dd-mm-yyyy'),t

    1 row created.

    SQL> /Enter value for emp_id: 3251

    Enter value for project_id: 2078Enter value for date_start: 1-1-2007Enter value for date_end: 12-12-2007Enter value for role_id: DESold 1: insert into employee_project_teamvalues('&emp_id','&project_id',to_date('&date_start','dd-new 1: insert into employee_project_team values('3251','2078',to_date('1-1-2007','dd-mm-yyyy'),to_

    1 row created.

    SELECTCode:SQL> select * from employee_project_team;

    EMP_ID PROJECT_ID DATE_STAR DATE_ENDROLE_ID-------------------- ------------------------------ --------- --------- --------------------3251 2056 01-JAN-08 11-DEC-08 DES3251 2056 12-DEC-08 MGR4562 2056 12-DEC-08 DEV3251 2078 01-JAN-07 12-DEC-07 DES

    Master Tables

    Master_Skill

    skill_idName

    C C Programming

    JAV Java Programming

    DES Software Design

    TEST Software Testing

    OOP Object Oriented Programming

    CREATE

  • 7/30/2019 Example table

    7/19

    Code:SQL> create table master_skill(skill_id varchar2(20),skill_namevarchar2(30),primary key(skill_id));Table created.

    INSERTCode:

    SQL> insert into master_skill values('&skill_id','&skill_name');Enter value for skill_id: CEnter value for skill_name: C Programmingold 1: insert into master_skill values('&skill_id','&skill_name')new 1: insert into master_skill values('C','C Programming')

    1 row created.

    SQL> /Enter value for skill_id: JAVEnter value for skill_name: Java Programmingold 1: insert into master_skill values('&skill_id','&skill_name')new 1: insert into master_skill values('JAV','Java Programming')

    1 row created.

    SQL> /Enter value for skill_id: DESEnter value for skill_name: Software Designold 1: insert into master_skill values('&skill_id','&skill_name')new 1: insert into master_skill values('DES','Software Design')

    1 row created.

    SQL> /Enter value for skill_id: TestEnter value for skill_name: Software Testing

    old 1: insert into master_skill values('&skill_id','&skill_name')new 1: insert into master_skill values('Test','Software Testing')

    1 row created.

    SQL> /Enter value for skill_id: OOPEnter value for skill_name: Object Oriented Programmingold 1: insert into master_skill values('&skill_id','&skill_name')new 1: insert into master_skill values('OOP','Object Oriented Programming')

    1 row created.

    SELECT

    Code:SQL> select * from master_skill;

    SKILL_ID SKILL_NAME-------------------- ------------------------------C C ProgrammingJAV Java ProgrammingDES Software DesignTest Software TestingOOP Object Oriented Programming

  • 7/30/2019 Example table

    8/19

    Master_Designation_details

    DesignationName

    C1 Engineer

    C2 Analyst

    C3 Consultant

    C4 Director

    VP Vice President

    CREATECode:SQL> create table master_designation_details(designationvarchar2(20),designation_name varchar2(30),primary key(designation));

    Table created.

    INSERTCode:SQL> insert into master_designation_detailsvalues('&designation','&designation_name');Enter value for designation: C1Enter value for designation_name: Engineerold 1: insert into master_designation_detailsvalues('&designation','&designation_name')new 1: insert into master_designation_details values('C1','Engineer')

    1 row created.

    SQL> /

    Enter value for designation: C2Enter value for designation_name: Analystold 1: insert into master_designation_detailsvalues('&designation','&designation_name')new 1: insert into master_designation_details values('C2','Analyst')

    1 row created.

    SQL> /Enter value for designation: C3Enter value for designation_name: Consultantold 1: insert into master_designation_detailsvalues('&designation','&designation_name')new 1: insert into master_designation_details values('C3','Consultant')

    1 row created.

    SQL> /Enter value for designation: C4Enter value for designation_name: Directorold 1: insert into master_designation_detailsvalues('&designation','&designation_name')new 1: insert into master_designation_details values('C4','Director')

  • 7/30/2019 Example table

    9/19

    1 row created.

    SQL> /Enter value for designation: VPEnter value for designation_name: Vice Presidentold 1: insert into master_designation_detailsvalues('&designation','&designation_name')new 1: insert into master_designation_details values('VP','Vice President')

    1 row created.

    SELECTCode:SQL> select * from master_designation_details;

    DESIGNATION DESIGNATION_NAME-------------------- ------------------------------C1 Engineer

    C2 AnalystC3 ConsultantC4 DirectorVP Vice President

    Master_Role

    role_idName

    DES Designer

    MGR Manager

    DEV Developer

    SC Site Coordinator

    TST Tester

    CREATECode:SQL> create table master_role(role_id varchar2(20),role_namevarchar2(30),primary key(role_id));

    Table created.

    INSERTCode:SQL> insert into master_role values('&role_id','&role_name');Enter value for role_id: DESEnter value for role_name: Designer

    old 1: insert into master_role values('&role_id','&role_name')new 1: insert into master_role values('DES','Designer')

    1 row created.

    SQL> /Enter value for role_id: MGREnter value for role_name: Managerold 1: insert into master_role values('&role_id','&role_name')new 1: insert into master_role values('MGR','Manager')

  • 7/30/2019 Example table

    10/19

    1 row created.

    SQL> /Enter value for role_id: DEVEnter value for role_name: Developerold 1: insert into master_role values('&role_id','&role_name')new 1: insert into master_role values('DEV','Developer')

    1 row created.

    SQL> /Enter value for role_id: SCEnter value for role_name: Site Coordinatorold 1: insert into master_role values('&role_id','&role_name')new 1: insert into master_role values('SC','Site Coordinator')

    1 row created.

    SQL> /

    Enter value for role_id: TSTEnter value for role_name: Testerold 1: insert into master_role values('&role_id','&role_name')new 1: insert into master_role values('TST','Tester')

    1 row created.

    SELECTCode:SQL> select * from master_role;

    ROLE_ID ROLE_NAME-------------------- ------------------------------DES Designer

    MGR ManagerDEV DeveloperSC Site CoordinatorTST Tester

    Answer the following

    For all questionsfollowing standard procedure starting from defining the problemto 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

  • 7/30/2019 Example table

    11/19

    is as given (say C1).

    STEP 1: Problem Definiton:o Context

    a. Employee_portfolio table has employee detailsname and designation

    b. Employee_Project_Team has details of all employees who were / are working on agiven project

    c. If date_end is null, then the project is the currently active project for the givenemployee

    o InputA designation code

    o OutputList 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 Criteriaend_date is null in employee_project_team table o Output required for further matchingemp_id o Intermediate solution: SELECT emp_id from employee_ project_team

    where date_end is null

    Get employee name based on previously got ids

    o Criteriadesignation is as given in input on the employee table o Addl. Criteriaemp_id in output of previous query

  • 7/30/2019 Example table

    12/19

    o Output required: emp_name o Intermediate solution: select emp_name from employee_portfolio where designation = ? and emp_id in ?previous_output?

    NOTE: Approach can be reversed to filter first by designation as wellSTEP 3: Sample Test Cases

    1. Input C2 => Output Mark

    2. Input C1 => Output Ryan3. Input VP => Output None

    NOTEMissing test case: multiple output scenario

    Need to add data to demonstrate multiple records

    This step can also be interchanged with step 2STEP 4: Dry Run

    1. Intermediate result of query irrespective of input is 3251, 45622. Mapped names are: Mark, Ryan

    3. If input is

    a. C1further filter on employee returns Ryan b. C2further 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 aboveit is expected that the part is filled only at thispoint 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

    Code:SQL> select emp_name from employee_portfolio where designation = 'C1'andemp_id in (SELECT emp_id from employee_project_team where date_end is null);

  • 7/30/2019 Example table

    13/19

    EMP_NAME------------------------------Ryan

    OR

    Code:SQL> select distinct emp_name from employee_portfolio e inner joinemployee_project_team pe on pe.emp_id = e.emp_id where designation = 'C1' and date_end is null;

    EMP_NAME------------------------------Ryan

    OR

    Code:SQL> select emp_name from employee_portfolio e inner join (SELECT emp_id,date_end from employee_pr

    oject_team where date_end is null) pe on pe.emp_id = e.emp_id wheredesignation = 'C1' and date_endis null;

    EMP_NAME------------------------------Ryan

    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 InputA skill id

    o OutputList of names of projects and corresponding number of employees

    a. with the given skill

    o Special considerations

  • 7/30/2019 Example table

    14/19

    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 projectsin this case doublecounting 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 2: Final Query

    Code:SQL> SELECT p.name, count(distinct pe.emp_id) No_of_Employee fromemployee_project p left outer joinemployee_project_team pe on p.project_id = pe.project_id left outer joinemployee_skill es on es.emp_id = pe.emp_id where skill_id = 'JAV' or skill_id is null group by p.name

    2 /

    NAME NO_OF_EMPLOYEE------------------------------ --------------Google Testing 1Yahoo Development 1

    OR

    Code:SQL> SELECT p.name, count(emp_id) No_of_Employee from employee_project p leftouter join (SELECT distinct project_id, emp_id from employee_project_team where emp_id in (selectemp_id from employee_skill where skill_id = 'JAV')) pe on p.project_id = pe.project_id group byp.name;

  • 7/30/2019 Example table

    15/19

    NAME NO_OF_EMPLOYEE------------------------------ --------------Google Testing 1Yahoo Development 1

    ORCode:

    SQL> SELECT p.name, count(pe.emp_id) No_of_Employee from employee_project pleft outer join (SELECTdistinct project_id, pt.emp_id from employee_project_team pt INNER JOINemployee_skill es ON es.emp_id = pt.emp_id where skill_id = 'JAV') pe on p.project_id = pe.project_idgroup by p.name;

    NAME NO_OF_EMPLOYEE------------------------------ --------------Google Testing 1Yahoo Development 1

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

    Code:SQL> SELECT emp_name, exp_years from employee_portfolio e inner joinemployee_skill es on e.emp_id =es.emp_id where skill_id = 'JAV' and prof_level = 'E2' and NOT EXISTS(SELECT 1 FROM employee_project_team pt where pt.emp_id = e.emp_id and date_end is null);

    no rows selected

    OR

    Code:SQL> SELECT emp_name, exp_years from employee_portfolio e inner joinemployee_skill 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 employee_project_team where date_end is null);

  • 7/30/2019 Example table

    16/19

    no rows selected

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

    Code:SQL> SELECT emp_name, exp_years, d.designation_name, p.name project_name fromemployee_portfolio e inner join employee_skill es on e.emp_id = es.emp_id left outer joinmaster_designation_details d ond.designation = e.designation left outer join employee_project_team pt onpt.emp_id = e.emp_id leftouter join employee_project p on pt.project_id = p.project_id where skill_id= 'JAV' and prof_level= 'E2' and pt.date_end is null;

    EMP_NAME EXP_YEARS DESIGNATION_NAMEPROJECT_NAME

    ------------------------------ -------------------- ------------------------------ -----------------Mark 4 AnalystGoogle Testing

    Typical Mistakes

    o If date_end criteria is not used the same employee will appear multiple times for each of thepast 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 joinis also okayinner join on projects is not acceptable

    5. Find all employees who report to an employee (say Ryan) who are not currently in any

    project

    Code:SQL> select e.emp_name from employee_portfolio e inner joinemployee_portfolio sup on e.supervisor_id = sup.emp_id where sup.emp_name = 'Ryan' and e.emp_id not in (SELECTemp_id from employee_project_team where date_end is null);

    EMP_NAME------------------------------Brian

    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)

    Code:

  • 7/30/2019 Example table

    17/19

    SQL> select e.emp_name, d.designation_name, p.name project_name, r.role_namefrom employee_portfolio e inner join employee_portfolio sup on e.supervisor_id = sup.emp_id innerjoin employee_portfoliosup2 on sup.supervisor_id = sup2.emp_id left outer joinmaster_designation_details d on d.designation = e.designation left outer join employee_project_team pt on pt.emp_id =e.emp_id and pt.date_end is null left outer join employee_project p on pt.project_id = p.project_idleft outer join master_role r on pt.role_id = r.role_id where sup2.emp_name = 'Mark';

    EMP_NAME DESIGNATION_NAME PROJECT_NAMEROLE_NAME------------------------------ ------------------------------ ------------------------------ -------Brian Engineer

    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.

    Code:SQL> select e.emp_id, e.emp_name, e.designation, sup.emp_id supervisor_id,sup.emp_name supervisor_name, sup.designation supervisor_designation from employee_portfolio e leftouter join employee_portfolio sup on e.supervisor_id = sup.emp_id order by e.designation desc,e.emp_name;

    EMP_ID EMP_NAME DESIGNATION SUPERVISORSUPERVISOR_NAME SUPERVISOR_DESIGNATI---------- ------------------------------ -------------------- ---------- --------------------------3251 Mark C29812 Brian C1 4562Ryan C14562 Ryan C1 3251Mark C2

    8. Provide complete details of all current and previous projects of all employees reporting

    to a given employee (say Mark)

    Code:SQL> select e.*, p.*, pt.* from employee_portfolio e inner joinemployee_portfolio sup on e.superv

    isor_id = sup.emp_id left outer join employee_project_team pt on pt.emp_id =e.emp_id left outer join employee_project p on pt.project_id = p.project_id where sup.emp_name ='Mark';

    EMP_ID EMP_NAME DESIGNATION JOIN_DATESUPERVISOR_ID PROJECT_ID DATE_STAR DATE---------- ------------------------------ -------------------- --------- -------------------- ------

  • 7/30/2019 Example table

    18/19

    4562 Ryan C1 12-DEC-00 32512056 01-JAN-08 Google Testing

    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.

    INSERT:

    Code:SQL> insert into employee_portfolio values('1234','Terry','C2','12-MAY-2004','');

    1 row created.

    UPDATE:

    Code:SQL> UPDATE employee_portfolio set supervisor_id='1234' where

    supervisor_id='3251';

    1 row updated.

    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

    Code:SQL> update employee_project_team set date_end = (select date_end fromemployee_project where date_end is not null) where project_id =(select project_id from employee_project

    where date_end is not null) and date_end is null;

    0 rows updated.

    11. Find the employee who has the maximum number of direct reportees.

    Code:SQL> select s.emp_id, s.emp_name from employee_portfolio s inner joinemployee_portfolio 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_portfolio where supervisor_id is not null groupby supervisor_id) emp_cnt );

    EMP_ID EMP_NAME---------- ------------------------------1234 Terry4562 Ryan

  • 7/30/2019 Example table

    19/19

    12. Find the employees who have worked on the same project in the most number of roles.

    Code:SQL> select distinct e.emp_id, emp_name from employee_project_team pt inner

    join employee_portfolioe on e.emp_id = pt.emp_id group by e.emp_id, emp_name, project_id HAVINGcount(distinct role_id) =(SELECT max(num_roles) max_roles FROM (SELECT emp_id, project_id,count(distinct role_id) num_rolesfrom employee_project_team group by emp_id, project_id) emp_roles);

    EMP_ID EMP_NAME---------- ------------------------------3251 Mark