Home > Articles

This chapter is from the book

This chapter is from the book

The AND Operator

The following examples will be taken from this Purchases table:

PurchaseID

CustomerName

State

QuantityPurchased

PricePerItem

1

Kim Chiang

IL

4

2.50

2

Sandy Harris

CA

10

1.25

3

James Turban

NY

5

4.00

Here’s an example of a WHERE clause that uses the AND operator:

SELECT
CustomerName,
QuantityPurchased
FROM Purchases
WHERE QuantityPurchased > 3
AND QuantityPurchased < 7

The AND clause 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 for which the QuantityPurchased is both greater than 3 and less than 7. Therefore, only these two rows are returned:

CustomerName

QuantityPurchased

Kim Chiang

4

James Turban

5

Notice that the row for Sandy Harris is not returned. Why? Sandy 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 the AND operator, all conditions specified must be true for the row to be selected.

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.