pty
pty

Reputation: 121

Get value of one column based on another column in same table

Here is what I am trying to do: Get all people with the same boss and email the boss. I can only use the table below and I would like to add another column showing the boss email which is found in the table.

id      name       email            boss     
-------------------------------------------
1       Apple      [email protected]    Rita    
2       Bob        [email protected]      Rita    
3       Charlie    [email protected]  Nuna      
4       Dan        [email protected]      Rita   
5       Rita       [email protected]     Sheeba    
6       Nuna       [email protected]     Sheeba

Thanks for any help.

Upvotes: 0

Views: 50

Answers (1)

Hogan
Hogan

Reputation: 70513

You use a join to do that. It looks like the code below. When joining to the same table you have to use an alias.

SELECT base.name, boss.email as boss_email
from the_table_name_you_did_not_say as base
join the_table_name_you_did_not_say as boss on base.boss = boss.name

Technically you don't have to use an alias on the base table if you select a wildcard * for some platforms

Upvotes: 2

Related Questions