Home > Ask the Oracle Database / Applications Experts > SQL Questions & Answers > All employees under a given manager
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

All employees under a given manager

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Oracle tips, scripts, and expert advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


>
QUESTION POSED ON: 23 April 2007

I want to display all the employees who report to a person. My data is like a tree structure.

eid   ename   mgrid
 1    Emp1     0
 2    Emp2     1
 3    Emp3     1
 4    emp4     2
 5    emp5     3
 6    emp6     4

For example, if I give eid as 2, I need report as:

eid   ename   mgrid
 4    emp4     2
 6    emp6     4

I need this data in the report because
- eid 4's mgrid is 2
- eid 6's mgrid is 4.

How to do that using SQL?



Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
SQL
How to check SQL query construction with the Mimer Validator
Using the SQL GROUP BY clause for counting combinations
How to use an SQL CASE expression
How to sort an SQL UNION query with special ORDER BY sequence
How to use string functions to make an SQL join
An SQL solution for a customer order homework problem
How to use SQL's POSITION function with substrings
Using SQL date functions to get totals for last three days
Using CASE in the SQL ORDER BY clause
What's the difference between an SQL inner join and equijoin?

Oracle and SQL
Using the SQL GROUP BY clause for counting combinations
How to use an SQL CASE expression
How to use the Oracle Database SQL Reference Manual
How to use SQL Developer to run SQL statements
How to work with the Oracle database home page
How to use SQL*Plus in Oracle
How to use SQL Developer to work with an Oracle database
How to view and edit table column definitions
How to sort an SQL UNION query with special ORDER BY sequence
How to use string functions to make an SQL join
Oracle and SQL Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
autonomous transaction  (SearchOracle.com)
CFML  (SearchOracle.com)
dynamic SQL  (SearchOracle.com)
foreign key  (SearchOracle.com)
Java Database Connectivity  (SearchOracle.com)
Open Database Connectivity  (SearchOracle.com)
Oracle  (SearchOracle.com)
stored procedure  (SearchOracle.com)
The Open Group  (SearchOracle.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


This is a great example of a requirement to return a subtree. (Please refer to this Wikipedia page if you do not recognize terms like subtree, node, or level.)

Suppose the given eid is 2. So we are looking for all nodes that are in the subtree of 2. Here is one solution which uses nested subqueries:

select eid
     , ename   
     , mgrid
  from employees as t1
 where mgrid = 2
    or exists 
       ( select * 
           from employees as t2
          where eid = t1.mgrid 
            and (
                mgrid = 2 
             or exists 
                ( select * 
                    from employees as t3
                   where eid = t2.mgrid 
                     and mgrid = 2 
                )                 
                ) 
       ) 

In effect, the query returns all employees:

  • whose manager is 2, or
  • whose manager's manager is 2, or
  • whose manager's manager's manager is 2

There are many approaches to subtree problems, and this one certainly appears straightforward enough. However, our choice of solution should always be governed by practical considerations.

Is this a practical solution? Absolutely, yes. We have only four levels of data. Therefore, the query needs to "walk down" at most three levels from any given node. Try the query with the given node 1 to confirm.

But what if there are actually five levels of data and not four? Will the query still work?

Of course the query will still work! <grin>  It will correctly return complete subtrees when there are one, two, or three levels below the given node. However, if a given node has more than three levels below it, only the first three levels down from that node will be returned.

What to do, what to do? Obviously, one answer is simply to extend the nested subquery pattern in the query to an additional level. Practical? You betcha!

But what if there actually is an unknown number of levels of data and not five? What then?

  1. For the employee hierarchy, in the real world there is no such thing as an unknown number of levels. Ask the HR department. Then extend the query as many levels as necessary, plus one or two just to be safe. The number of levels is not really the issue here. Instead, it is the more practical consideration of whether umpteen levels of data producing an exponential bazillion rows of output is really of any use to anyone. Perhaps limiting the results to four levels down isn't such a bad idea. See also Categories and Subcategories for additional thoughts and query examples.

  2. The employee hierarchy is an example of the adjacency list model, recognized by its use of a "parent id" (in this case mgrid). For other data structures, where there really is an unknown number of levels, consider using a different data model. See Trees in SQL by Joe Celko, which describes the nested set model, an alternative to the adjacency list model.




Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Oracle White Papers: Fusion Middleware
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts