If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Oracle > Creating an efficient sql select query.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-22-12, 12:34
mg_ mg_ is offline
Registered User
 
Join Date: Jan 2012
Posts: 2
Creating an efficient sql select query.

Hello,

I have a table - Users: | id | name | manager_id |

The manager_id references the User.id,

I need to find an employee by an id or name and this employee has to be a manager to someone else.

The simplest initial idea was the following - do a sub-select and check if the id is present within the manager_id column, but this takes a bit of time.

Code:
SELECT usr.* 
FROM Users usr
WHERE (
 SELECT count(*)
 FROM Users mg
 WHERE mg.manager_id = usr.id AND
 rownum = 1
 ) > 0) AND usr.name = 'John'
Is there a way to, for example, inner join the table to it self leaving only the rows that are managers?
Reply With Quote
  #2 (permalink)  
Old 01-22-12, 12:49
anacedent anacedent is offline
Registered User
 
Join Date: Aug 2003
Location: Where the Surf Meets the Turf @Del Mar, CA
Posts: 6,416
__________________
You can lead some folks to knowledge, but you can not make them think.
The average person thinks he's above average!
For most folks, they don't know, what they don't know.
Reply With Quote
  #3 (permalink)  
Old 01-22-12, 13:11
mg_ mg_ is offline
Registered User
 
Join Date: Jan 2012
Posts: 2
Yes, thank you, thought I'd get an input from another forum as well.

Problem still remains.
Reply With Quote
  #4 (permalink)  
Old 01-25-12, 05:32
tcbenkhard tcbenkhard is offline
Registered User
 
Join Date: Jan 2011
Posts: 8
Quote:
Originally Posted by mg_ View Post
Hello,

I have a table - Users: | id | name | manager_id |

The manager_id references the User.id,

I need to find an employee by an id or name and this employee has to be a manager to someone else.

The simplest initial idea was the following - do a sub-select and check if the id is present within the manager_id column, but this takes a bit of time.

Code:
SELECT usr.* 
FROM Users usr
WHERE (
 SELECT count(*)
 FROM Users mg
 WHERE mg.manager_id = usr.id AND
 rownum = 1
 ) > 0) AND usr.name = 'John'
Is there a way to, for example, inner join the table to it self leaving only the rows that are managers?
Code:
select * 
from users 01 where id in (select manager_id from users 02)
OR

Code:
select * from users 01 
join users 02 on 01.id = 02.manager_id

Last edited by tcbenkhard; 01-25-12 at 06:09.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On