Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 953
Adly Avatar asked Dec 13 '25 23:12

Adly


2 Answers

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

like image 154
Gordon Linoff Avatar answered Dec 16 '25 13:12

Gordon Linoff


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.

like image 28
Zevi Sternlicht Avatar answered Dec 16 '25 12:12

Zevi Sternlicht



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!