Heeiman
Heeiman

Reputation: 269

SQL use AND to find two values in a column

I am new to SQL and have a bit of a problem. I'm trying to

find the names of any patients in this database that are suffering from both insomnia and a cough

(it's for school so don't worry, no real people harmed by my incompetence).

To achieve this I wrote this:

 SELECT patientName 
   FROM Patient 
  WHERE patientID IN (SELECT patientID 
                        FROM Suffers 
                       WHERE illnessName LIKE '%Insomnia%' 
                         AND illnessName LIKE '%Cough%')

When I execute it I get no results though (I've got an empty cursor). This is a problem, because I know there is one patient in the DB with both those illnesses. What am I doing wrong?

Btw, I have to use the AND operator here, that seems to be the point of this exercise.

Grateful for any and all help :)

Upvotes: 2

Views: 3588

Answers (1)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

You should check patientID twice; your query edited:

 SELECT patientName 
   FROM Patient 
  WHERE patientID IN (SELECT patientID FROM Suffers WHERE illnessName LIKE '%Insomnia%')  
    AND patientID IN (SELECT patientID FROM Suffers WHERE illnessName LIKE '%Cough%')  

Now, patientID belongs to Insomnia and Cough Suffers

Upvotes: 5

Related Questions