Oracle SQL - AND Operator and Operator Precedence

Introduction

There is a possible problem if your compound conditions contain a mixture of AND and OR operators.

Demo

SQL>
SQL> select 'is true  ' as condition
  2  from   dual--   w w w .j  a v  a  2s.  c o m
  3  where  1=1 or 1=0 and 0=1;

CONDITION
---------
is true

SQL>
SQL>

With compound conditions, use parentheses to indicate the order in which you want the operations to be performed.

Demo

SQL>
SQL> select 'is true  ' as condition
  2  from   dual--  ww w  . j  ava  2  s  .  c o m
  3  where  (1=1 or 1=0) and 0=1;

no rows selected

SQL>
SQL> select 'is true  ' as condition
  2  from   dual
  3  where  1=1 or (1=0 and 0=1);

CONDITION
---------
is true

SQL>
SQL>

Related Topic