Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading