HomeOracle Page 2 - Clauses and Logical Operators for Retrieving Table Data
How to use the AND, OR, and NOT logical operators - Oracle
In this sixth part of a nine-part series that focuses on retrieving data from tables with the SELECT statement, you'll learn how to code the WHERE clause and how to use comparison and logical operators. This article is excerpted from chapter three of the book Murach's Oracle SQL and PL/SQL, written by Joel Murach (Murach Publishing; ISBN: 9781890774509).
You can use the AND and OR operators to combine two or more search conditions into a compound condition. And you can use the NOT operator to negate a search condition.
The first two examples illustrate the difference between the AND and OR operators. When you use the AND operator, both conditions must be true. So in the first example, only those vendors located in Springfield, New Jersey, are retrieved from the Vendors table. When you use the OR operator, though, only one of the conditions must be true. So in the second example, all vendors located in New Jersey and all the vendors located in Pittsburgh are retrieved.
The third example shows a compound condition that uses two NOT operators. As you can see, this expression is difficult to understand. Because of that, you should avoid using this operator. The fourth example in this figure, for instance, shows how the search condition in the third example can be rephrased to eliminate the NOT operator. As a result, the condition in the fourth example is much easier to understand.
The last two examples in this figure show how the order of precedence for the logical operators and the use of parentheses affect the result of a search condition. By default, the NOT operator is evaluated first, followed by AND and then OR. However, you can use parentheses to override the order of precedence or to clarify a logical expression, just as you can with arithmetic expressions. In the next to last example, for instance, no parentheses are used, so the two conditions connected by the AND operator are evaluated first. In the last example, though, parentheses are used so the two conditions connected by the OR operator are evaluated first.
The syntax of the WHERE clause with logical operators
(invoice_total >= 5000 OR NOT invoice_date < = '01-JUL-2008')
The same condition rephrased to eliminate the NOT operator
< 5000 AND invoice_date <= '01-JUL-2008'
A compound condition without parentheses
, invoice_date, invoice_total FROM invoices WHERE invoice_date > '01-MAY-2008' OR invoice_total > 500 AND invoice_total - payment_total - credit_total > 0 ORDER BY invoice_number
(91 rows selected)
The same compound condition with parentheses
(invoice_date > '01-MAY-2008' OR invoice_total > 500) AND invoice_total - payment_total - credit_total > 0 ORDER BY invoice_number
(39 rows selected)
You can use the AND and OR logical operators to create compound conditions that consist of two or more conditions. You use the AND operator to specify that the search must satisfy both of the conditions, and you use the OR operator to specify that the search must satisfy at least one of the conditions.
You can use the NOT operator to negate a condition, but that can make the search condition difficult to understand. If it does, you should rephrase the condition to eliminate NOT.
When Oracle evaluates a compound condition, it evaluates the operators in this sequence: (1) NOT, (2) AND, and (3) OR. You can use parentheses to override this order of precedence or to clarify the sequence in which the operations will be evaluated.
Please check back for the next part of the series.