1. Trang chủ
  2. » Công Nghệ Thông Tin

The Language of SQL- P19 potx

5 234 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 91,09 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

The result is:CustomerName State QuantityPurchased William Smith IL 4 Brenda Harper NY 5 Notice there are two sets of parentheses in this statement.. Our use of parentheses here is analo

Trang 1

The result is:

CustomerName State QuantityPurchased

William Smith IL 4

Brenda Harper NY 5

Notice there are two sets of parentheses in this statement Our use of parentheses here is analogous to the parentheses used in the composite functions seen in Chapter 4 With regard to functions, if there is more than one set of parentheses, the innermost set of functions always gets evaluated first The same is true of parentheses used in Boolean expressions In this example, the innermost set of parentheses contains:

(QuantityPurchased >= 3

AND QuantityPurchased <= 10)

After this is evaluated for each row, you can then proceed outward to the second set of parentheses:

(State ¼ 'IL'

AND (QuantityPurchased >= 3

AND QuantityPurchased <= 10))

Finally, you add in the final line in the WHERE clause (which is not enclosed in any parentheses at all):

WHERE State ¼ 'NY'

OR (State ¼ 'IL'

AND (QuantityPurchased >= 3

AND QuantityPurchased <= 10))

The NOT Operator

In addition to the AND and OR operators, it is often useful to use the NOT

operator to express a complex logical condition TheNOTexpresses a negation,

or opposite, of whatever follows theNOT Here’s a simple example:

SELECT

CustomerName,

State,

Chapter 8 ■ Boolean Logic

76

Trang 2

FROM Orders

WHERE NOT State ¼ 'NY'

The result is:

CustomerName State QuantityPurchased

William Smith IL 4

Natalie Lopez CA 10

This specifies a selection of rows where the state is not equal to NY In this simple

case, theNOToperator is not truly necessary The previous statement can also be

accomplished via the following equivalent statement:

SELECT

CustomerName,

State,

QuantityPurchased

FROM Orders

WHERE State <> 'NY'

In this situation, the not equals operator (<>) accomplishes the same thing as

theNOToperator

Here’s a more complex example with theNOToperator:

SELECT

CustomerName,

State,

QuantityPurchased

FROM Orders

WHERE NOT (State ¼ 'IL'

OR State ¼ 'NY')

The result is:

CustomerName State QuantityPurchased

Natalie Lopez CA 10

Trang 3

When theNOToperator is used before a set of parentheses, it negates everything

in the parentheses In this example, you are looking for all rows where the state is

not Illinois or New York.

Again, note that the NOT operator is not strictly necessary in this example The previous query can also be accomplished via the following equivalent statement:

SELECT

CustomerName,

State,

QuantityPurchased

FROM Orders

WHERE State <> 'IL'

AND State <> 'NY'

You may need to think a minute about why the previous two statements are really equivalent The first statement utilizes the NOT operator and a logical expression with anOR operator The second statement converts the same logic into an expression with anANDoperator

Here’s a final example of how the NOT operator can be used in a complex statement:

SELECT

CustomerName,

State,

QuantityPurchased

FROM Orders

WHERE NOT (State ¼ 'IL'

AND QuantityPurchased > 3)

The result is:

CustomerName State QuantityPurchased

Chapter 8 ■ Boolean Logic

78

Trang 4

As before, there is another way to express the previous statement without using

theNOT:

SELECT

CustomerName,

State,

QuantityPurchased

FROM Orders

WHERE State <> 'IL'

OR QuantityPurchased <= 3

As seen in these examples, it may not be logically necessary to use the NOT

operator in complex expressions with arithmetic operators such as equals (=)

and less than (<) However, it’s often more straightforward to place a NOT in

front of a logical expression than to convert that expression to one that doesn’t

use the NOT In other words, the NOT operator can provide a useful way of

expressing your logical thoughts

The BETWEEN Operator

We will now turn to two special operators that can simplify expressions that

would ordinarily require theORorAND operators These are theBETWEENand

INoperators TheBETWEEN operator allows you to abbreviate an AND

expres-sion with greater than or equal to (>=) and less than or equal to (<=) operators

into one simple expression with a single operator

Here’s an example Let’s say you want to select all rows with a quantity purchased

between 5 and 20 You can issue the followingSELECTstatement:

SELECT

CustomerName,

QuantityPurchased

FROM Orders

WHERE QuantityPurchased >= 5

AND QuantityPurchased <= 20

or you can issue this equivalent statement that utilizes theBETWEEN operator:

SELECT

CustomerName,

QuantityPurchased

FROM Orders

WHERE QuantityPurchased BETWEEN 5 AND 20

Trang 5

In both cases, theSELECTreturns this data:

CustomerName QuantityPurchased

Natalie Lopez 10

Brenda Harper 5

TheBETWEENoperator always requires a correspondingANDplaced between the two numbers

Note the relative simplicity of the BETWEEN operator Also notice that the

BETWEEN keyword is equivalent only to the greater than or equal to (>=) and less than or equal to (<=) operators It can’t be used to express something simply greater than (>) or less than (<) a range of numbers In this example, the row for Brenda Harper is selected since the quantity is equal to 5, and therefore is between 5 and 20

TheNOToperator can be used with theBETWEEN For example, thisSELECT: SELECT

CustomerName,

QuantityPurchased

FROM Orders

WHERE QuantityPurchased NOT BETWEEN 5 AND 20

retrieves this data:

CustomerName QuantityPurchased

William Smith 4

The IN Operator

Just as the BETWEEN represents a special case of the AND operator, the IN

operator allows for a special case of theOR Let’s say you want to see rows where the state is Illinois or New York You can issue thisSELECTstatement:

SELECT

CustomerName,

State

FROM Orders

WHERE State ¼ 'IL'

OR State ¼ 'NY'

Chapter 8 ■ Boolean Logic

80

Ngày đăng: 05/07/2014, 05:20