Search code examples
sqlwhere-clausedata-manipulation

SQL: Multiple Where Conditions


I want to select the following rows from my table if either of the following conditions are met:

  • Condition 1: var_1 can not equal 1 or 0

  • Condition 2: var_2 can not equal 2

For example, this means that the resulting table can have var_2 = 2 provided that var_1 <> 1,0. The same way, var_1 can be equal to 1 or 0, provided that var_2 <>2.

I tried to write the following SQL command:

select * from my_table where( ("var_1" <> 1 AND "var_1" <> 0 ) OR ("var_2" <>2 ) )p;

However, this is returning rows that violate this condition, for example:

  var_1 var_2
1     1     2
2     0     2

Can someone please show me how to correct this?

Thanks!


Solution

  • By default the characters enclosed within double quotes are string literals, so don't specify the column within double quotes.

    select * from my_table where( (var_1 <> 1 AND var_1 <> 0 ) OR (var_2 <>2 ) );