As with the BETWEEN operator, the NOT operator can be used with theIN, as shown in this example: SELECT CustomerName, State FROM Orders WHERE State NOT IN 'IL', 'NY' This retrieves this
Trang 1or you can use this equivalent statement that utilizes theINoperator:
SELECT
CustomerName,
State
FROM Orders
WHERE State IN ('IL', 'NY' )
In either case, the data retrieved is:
Notice that commas are used to separate all values within the parentheses
fol-lowing theINkeyword
The usefulness of theIN operator may not be obvious in this example, where
only two states are listed However, theINcan just as easily be used in situations
where you want to list dozens of specific values This greatly reduces the amount
of typing required for such a statement Another handy use for the IN operator
comes in situations where you want to use data from Excel in a SQL statement If
you want to obtain multiple values from adjacent cells in a spreadsheet for your
SQL statement, Excel allows you to copy those values with a comma delimiter
This result can then be pasted inside the parentheses following theINoperator
As with the BETWEEN operator, the NOT operator can be used with theIN, as
shown in this example:
SELECT
CustomerName,
State
FROM Orders
WHERE State NOT IN ('IL', 'NY' )
This retrieves this data:
Natalie Lopez CA
Trang 2One final note about the IN operator There is a second way to use the IN,
which is substantially different from the syntax just discussed In the second format of theIN operator, an entire SELECT statement is specified within the parentheses, allowing the individual values to be created logically when needed
This is called a subquery, and it will be covered in detail in Chapter 14.
Boolean Logic and NULL Values
At the start of this chapter, I stated that the Boolean logic in SQL evaluates complex expressions as either true or false This assertion was not completely correct When evaluating the conditions in a WHERE clause, there are actually three possibilities: true, false, and unknown The possibility of unknown derives from the fact that columns in SQL databases are sometimes allowed to have a NULL value As mentioned in Chapter 1, NULL values are those for which there
is an absence of data
SQL provides a special keyword to test for the presence of NULL values for a column specified in aWHERE clause The keyword is IS NULL Let’s look at an example taken from the following Products table:
For this example, you have to imagine that as rows are added to the Products table, they are initially not given any value in the Weight column They are initially given a value of NULL, and a user of the system later assigns a weight to the product
Let’s say that you attempt to use the followingSELECTto find products missing
a weight:
SELECT
ProductDescription,
Weight
FROM Products
WHERE Weight ¼ 0
Chapter 8 ■ Boolean Logic
82
Trang 3This would return:
This is not quite what you want A weight equal to zero is not the same as a
weight with a NULL value To correct this, you need to issue:
SELECT
ProductDescription,
Weight
FROM Products
WHERE Weight ¼ 0
OR Weight IS NULL
This returns:
TheIS NULLkeyword can also be negated asIS NOT NULL, which allows you to
retrieve all rows that do not have NULL for the specified column
It should be mentioned that theISNULLfunction, discussed in Chapter 4, can
provide an alternative to theIS NULLkeyword The equivalent of the previous
SELECTstatement, utilizing theISNULLfunction is:
SELECT
ProductDescription,
Weight
FROM Products
WHERE ISNULL(Weight, 0) ¼ 0
This SELECT retrieves the same two rows The ISNULL function converts all
values for the Weight column with a value of NULL to 0 Since the WHERE
clauses tests for a value of 0, it, in effect, tests for values of 0 or NULL
Trang 4You can also combine the ISNULL function and the IS NULL keyword in a singleSELECTstatement, such as:
SELECT
ProductDescription,
ISNULL (Weight, 0) AS 'Weight'
FROM Products
WHERE Weight ¼ 0
OR Weight IS NULL
This produces this data:
ProductDescription Weight
Looking Ahead
This chapter covered the important topic of how to create complex expressions
of selection logic The basic Boolean operators used in this endeavor wereAND,
OR, andNOT We also discussed theBETWEENandINoperators, which allow for
a more concise statement of the AND and OR operators in certain situations Parentheses are another essential tool in the formulation of complex expressions
By using parentheses or multiple sets of parentheses, you can create almost every imaginable logical condition Finally, we talked about how to deal with NULL values when selecting data
In our next chapter, we’re going to take an interesting detour into some alter-native ways to specify selection criteria We’re first going to look at the topic of pattern matching This will allow you to match by portions of a word or phrase and to do such things as find all products that contain the word ‘‘white.’’ The second half of the chapter will turn to the possibility of matching by the sound of
a word or phrase This, for example, will let you find all customers who have a first name that sounds like Haley, even if the name is spelled Hailey
Chapter 8 ■ Boolean Logic
84
Trang 5Inexact Matches
Keywords Introduced: LIKE ,
I would like to now turn to two situations in which the data to be retrieved is not precisely defined In the first circumstance, we will look at the need to retrieve data based on inexact matches with words or phrases For example, you may be interested in finding customers whose name contains the word ‘‘bank.’’
In the second situation, we will extend the idea of inexact matches to include the possibility of matching by the sound of a word or phrase For instance, you may
be interested in customers whose name sounds like ‘‘Smith,’’ even though it may not be spelled exactly that way
Pattern Matching
Let’s first look at inexact matches within phrases, which is often referred to as
pattern matching In SQL, the LIKEoperator is utilized in the WHERE clause to enable you to find matches against parts of a column value The LIKEoperator requires the use of special wildcard characters to specify exactly how the match is
to work Let’s start with an example from the Movies table shown on the next page Our first example of aSELECTstatement with aLIKEoperator is:
SELECT
MovieTitle AS 'Movie'
FROM Movies
WHERE MovieTitle LIKE '%LOVE%'
85