Adly
Adly

Reputation: 597

SELECT in Table (SQL)

If I have a table like that :

    ID           MANAGER_ID        NAME
    1              2               Ahmed
    2              3               Mostafa
    3              NULL            Mohamed
    4              1               Abbas
    5              3               Abdallah

If I wanted a sql statement to get the name of the managers in this company! how it will be?

Upvotes: 2

Views: 92

Answers (3)

Zevi Sternlicht
Zevi Sternlicht

Reputation: 5399

You just need

SELECT NAME from table_name

I just realized that you want it only where the Manager is NULL,

SELECT NAME from table_name WHERE MANAGER_ID IS NOT NULL

As I have been pointed out this will exude manager 3 because he is a manager as he manages other users. See @Gordon's answer.

Upvotes: 2

Gordon Linoff
Gordon Linoff

Reputation: 1269773

The question is a little trickier than it seems at first glance:

select Name
from t
where id in (select manager_id from t)

The query seems to require some sort of self-join, because the information about who is a manager is in the manager_id column. This does the self-join using in in the where clause

Upvotes: 6

Moyed Ansari
Moyed Ansari

Reputation: 8461

 Select Name 
from TABLENAME 
WHERE MANAGER_ID IS NOT NULL

Upvotes: 1

Related Questions