I have two tables, user:
id | full_name | is_admin |
---|---|---|
1 | jane | 0 |
2 | Helio | 0 |
3 | fran | 0 |
4 | mila | 0 |
5 | admin | 1 |
approver :
id | subordinate_id | approver_id |
---|---|---|
1 | 1 | 2 |
2 | 3 | 4 |
and I would like to perform a query that brings up the user names that do not have the id in the subordinate_id column of the approvers table.
I tried it this way:
SELECT
full_name
FROM user AS U
WHERE NOT EXISTS(
SELECT * FROM approver AS A
WHERE A.subordinate_id = U.id AND U.is_admin = 0);
but in this case the admin user is still coming, and I would like to not bring whoever has the is_admin column of the usuario table = 1. I want to bring only common users and not admin.
Can someone help me with this?
3
Answers
Try this query
You need to put
AND U.is_admin = 0
outsideEXISTS
DB Fiddle Demo
You can try this query. We can select the admin="0" in the first table the can left join on second table. And after that We can do the inverse of inner join to get the results you are expecting.