How to get ID in PostgreSQL?

Here is the Table that I have

I am learning SQL using Postgres. Here I want to get the employeeID where the employee does not have any cell phone, type ‘C’, that is the answer should be 3, 4, 5 because those employees don’t have cell phone. I tried the following query but it doesn’t give me the right answer instead it gives me 1, 2, 3, 4, 5

SELECT employeeID
FROM PhoneInfo
WHERE Type != 'C';

What am I doing wrong?

>Solution :

SELECT DISTINCT employeeID
FROM PhoneInfo
WHERE employeeID NOT IN (
                         SELECT employeeID
                         FROM PhoneInfo
                         WHERE Type = 'C'
                        ); 

This query will give you the right answer. Here, we are trying to find the employeeID that does not exist in the list of the employeeIDs that have type C. Hope that helps!

Leave a Reply