Home > Articles

This chapter is from the book

This chapter is from the book

The OR Operator

Let’s now look at the OR operator. The AND operator meant that all conditions must evaluate to true for the row to be selected. The OR operator means that the row will be selected if any of the conditions are determined to be true.

Here’s an example, taken from the same table:

SELECT
CustomerName,
QuantityPurchased,
PricePerItem
FROM Purchases
WHERE QuantityPurchased > 8
OR PricePerItem > 3

This SELECT returns this data:

CustomerName

QuantityPurchased

PricePerItem

Sandy Harris

10

1.25

James Turban

5

4.00

Why are the rows for Sandy Harris and James Turban displayed, and not the row for Kim Chiang? The row for Sandy Harris 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 condition needs to be true for an OR condition.

Likewise, the row for James Turban is selected because the second condition (PricePerItem > 3) is true for that row. The row for Kim Chiang isn’t selected because it doesn’t satisfy either of the two conditions.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.