user1342164 user1342164 - 5 months ago 9
SQL Question

Using SQL AND OR together not working

I am trying to use the sql statement below to get the records where the city is Atlanta and the taxonomy code can be 2 different values. It isn't working properly because it is not giving me just Atlanta. If I remove the last

OR
condition it works perfectly, but I need to have it be either taxonomy value.

Any ideas what I am doing wrong? TIA

SELECT NPI, ...
FROM NPIData
WHERE ([Provider Business Mailing Address City Name] = 'ATLANTA')
AND ([Healthcare Provider Taxonomy Code_1] = '122300000X')
OR ([Healthcare Provider Taxonomy Code_1] = '1223G0001X')

Answer

OR has a lower precedence than AND, so you should use parenthesis:

WHERE [Provider Business Mailing Address City Name] = 'ATLANTA' 
  AND ([Healthcare Provider Taxonomy Code_1] = '122300000X' OR
       [Healthcare Provider Taxonomy Code_1] = '1223G0001X')

A much better option here is to just use in:

WHERE [Provider Business Mailing Address City Name] = 'ATLANTA'
  AND [Healthcare Provider Taxonomy Code_1] in ('122300000X', '1223G0001X')