The AND Operator The following examples will all be taken from this Orders table: OrderID CustomerName State QuantityPurchased PricePerItem 1 William Smith IL 4 2.50 2 Natalie Lopez CA 1
Trang 1Boolean Logic
In the previous chapter, we introduced the concept of selection criteria, but only in its simplest form We’re now going to expand on that concept to greatly enhance our ability to specify the rows that are returned from a SELECT This is where the pure logic of SQL comes into play In this chapter, we are going to introduce
a number of operators that will allow you to create complex logical expressions With these new capabilities, if a user comes to you and says that she wants a list
of all female customers who live in ZIP codes 60601 through 62999, but excluding anyone who’s under the age of 30 or who doesn’t have an email address, that will
be something you can provide
Complex Logical Conditions
The WHERE clause introduced in the previous chapter utilized only simple selection criteria You sawWHEREclauses such as:
WHERE QuantityPurchased ¼ 5
The condition expressed in thisWHEREclause is quite simple: It returns all rows where the QuantityPurchased column has a value of 5
In the real world, the selection of data is often far from straightforward Accordingly, let’s now turn our attention to ways of specifying some more complex logical conditions in your selection criteria
71
Trang 2The ability to devise complex logical conditions is sometimes called Boolean logic.
This term, taken from mathematics, refers to the ability to formulate complex conditions that are evaluated as either true or false In the aforementioned example, the condition QuantityPurchased ¼ 5 is evaluated as either true or false for each row in the table Obviously, you only want to see rows where the condition is evaluated as true
The principle keywords used to create complex Boolean logic areAND, OR, and
NOT These three operators are used to add additional functionality to theWHERE
clause In the proper combination, theAND, OR, and NOT operators, along with parentheses, can specify just about any logical expression you can imagine
The AND Operator
The following examples will all be taken from this Orders table:
OrderID CustomerName State QuantityPurchased PricePerItem
1 William Smith IL 4 2.50
2 Natalie Lopez CA 10 1.25
3 Brenda Harper NY 5 4.00
Here’s an example of aWHEREclause that uses theANDoperator:
SELECT
CustomerName,
QuantityPurchased
FROM Orders
WHERE QuantityPurchased > 3
AND QuantityPurchased < 7
TheANDclause means that all conditions must evaluate to true for the row to be selected
This SELECT specifies that the only rows to be retrieved are those where the QuantityPurchased is both greater than 3 and less than 7 Therefore, only these two rows are returned:
CustomerName QuantityPurchased
William Smith 4
Brenda Harper 5
Chapter 8 ■ Boolean Logic
72
Trang 3Notice that the row for Natalie Lopez is not returned Why? Natalie purchased a
quantity of 10, which, in fact, does satisfy the first condition (QuantityPurchased
> 3) However, the second condition (QuantityPurchased < 7) is not satisfied
and therefore is not true When using theANDoperator, all conditions specified
must be true
The OR Operator
Let’s now look at the OR operator The AND clause meant that all conditions
must evaluate to true for the row to be selected TheOR clause means that the
row will be selected if any of the conditions is determined to be true.
Here’s an example, taken from the same table:
SELECT
CustomerName,
QuantityPurchased,
PricePerItem
FROM Orders
WHERE QuantityPurchased > 8
OR PricePerItem > 3
TheSELECTreturns this data:
CustomerName QuantityPurchased PricePerItem
Natalie Lopez 10 1.25
Brenda Harper 5 4.00
Why are the rows for Natalie Lopez and Brenda Harper displayed, and not the
row for William Smith? The row for Natalie Lopez is selected because it meets the
requirements of the first condition (QuantityPurchased > 8) It doesn’t matter
that the second condition (PricePerItem > 3) isn’t true, because only one
con-dition needs to be true for anORcondition
Likewise, the row for Brenda Harper is selected because the second condition
(PricePerItem > 3) is true for that row The row for William Smith isn’t selected
because it doesn’t satisfy either of the two conditions
Using Parentheses
Let’s say that you are only interested in orders from customers from either the
state of Illinois or the state of California Additionally, you only want to see
Trang 4orders where the quantity purchased is greater than 8 To satisfy this request, you put together thisSELECTstatement:
SELECT
CustomerName,
State,
QuantityPurchased
FROM Orders
WHERE State ¼ 'IL'
OR State ¼ 'CA'
AND QuantityPurchased > 8
When you execute this, you are expecting to get back only one row, for Natalie Lopez This is because you have two rows for customers in Illinois or California (Smith and Lopez) But only one of those (Lopez) has a quantity purchased greater than 8
However, when you execute this statement, you see:
CustomerName State QuantityPurchased
William Smith IL 4
Natalie Lopez CA 10
What went wrong? Why did you get back two rows instead of one? The answer lies with how SQL interprets theWHEREclause, which contains bothANDandOR
operators Like other computer languages, SQL has a predetermined order of evaluation, which specifies the order in which various operators are interpreted Unless told otherwise, SQL always processes the AND operator before the OR
operator So, in the previous statement, it first looks at the AND and evaluates the condition:
State ¼ 'CA'
AND QuantityPurchased > 8
The row that satisfies that condition is for Natalie Lopez It then evaluates theOR
operator, which allows for rows where the State equals IL That adds in the row for William Smith Therefore, it determines that both the William Smith and the Natalie Lopez rows meet the condition
Obviously, this isn’t what was meant This type of problem often comes up when
ANDandORoperators are combined in a singleWHEREclause The way to resolve
Chapter 8 ■ Boolean Logic
74
Trang 5the ambiguity is to use parentheses to specify the exact order of evaluation that
you would like Anything in parentheses is always evaluated first
Here’s how parentheses can be added to the previous SELECT to correct the
situation:
SELECT
CustomerName,
State,
QuantityPurchased
FROM Orders
WHERE (State ¼ 'IL'
OR State ¼ 'CA')
AND QuantityPurchased > 8
When this is executed, you now see this data:
CustomerName State QuantityPurchased
Natalie Lopez CA 10
The parentheses in theSELECT statement force theORexpression (State¼ ‘IL’
OR State¼ ‘CA’) to be evaluated first This produces the intended result
Multiple Sets of Parentheses
Let’s say that you want to select two different sets of rows from the Orders table:
first, rows for customers in New York, and second, rows for customers in Illinois
who have made a purchase with a quantity between 3 and 10 The following
SELECTaccomplishes this requirement:
SELECT
CustomerName,
State,
QuantityPurchased
FROM Orders
WHERE State ¼ 'NY'
OR (State ¼ 'IL'
AND (QuantityPurchased >= 3
AND QuantityPurchased <= 10))