sanu j
sanu j

Reputation: 85

SQL 'select' Query with 3 conditions

SELECT * 
FROM REVIEW 
WHERE REVIEWERID =5 AND APPRAISEECONFIRMYN='Y' AND HRCONFIRMYN = NULL

Are 2 'AND' conditions allowed like this? I'm not getting the correct output. There are 2 records in the database that fulfil the above conditions. When I remove the last condition 'HRCONFIRMYN = NULL' and execute, I get the correct output.

How can this be solved? I need to check all 3 conditions whilst searchng the records.

Upvotes: 5

Views: 62804

Answers (2)

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79979

To compare the NULL values, you have to use the IS NULL predicate instead of = NULL like so:

SELECT * 
FROM REVIEW 
WHERE REVIEWERID = 5 
  AND APPRAISEECONFIRMYN = 'Y' 
  AND HRCONFIRMYN IS NULL

Upvotes: 13

John Woo
John Woo

Reputation: 263893

  • use OR instead
  • group your condition
  • use IS NULL when comparing with NULLs

query,

SELECT * 
FROM REVIEW 
WHERE (REVIEWERID =5 AND APPRAISEECONFIRMYN='Y') OR
      HRCONFIRMYN IS NULL

Upvotes: 2

Related Questions