I have this users
table:
and this relationships
table:
So each user is paired with another one in the relationships
table.
Now I want to get a list of users
which are not in the relationships
table, in either of the two columns (user_id
or pair_id
).
How could I write that query?
First try:
SELECT users.id
FROM users
LEFT OUTER JOIN relationships
ON users.id = relationships.user_id
WHERE relationships.user_id IS NULL;
Output:
This is should display only 2 results: 5 and 6. The result 8 is not correct, as it already exists in relationships
. Of course I'm aware that the query is not correct, how can I fix it?
- I'm using PostgreSQL.