A CASE expression to create the same output for this data is: SELECT CASE WHEN Fruit ¼ 'X' THEN 'Fruit' WHEN Vegetable ¼ 'X' THEN 'Vegetable' ELSE 'Other' END AS 'Category', ProductDescr
Trang 1This next example will be taken from this data:
ProductID Fruit Vegetable Spice ProductDescription
In this situation, the database contains multiple columns to indicate whether the
product is a fruit, vegetable, or spice A CASE expression to create the same
output for this data is:
SELECT
CASE
WHEN Fruit ¼ 'X' THEN 'Fruit'
WHEN Vegetable ¼ 'X' THEN 'Vegetable'
ELSE 'Other'
END AS 'Category',
ProductDescription AS 'Description'
FROM Products
Once again, the result is:
Category Description
Since the data now uses three separate columns to indicate if the product is a
fruit, vegetable, or spice, you need to use the searched format of theCASEclause
in order to apply the needed logic The simple format only works with an analysis
of a single column
Due to the inherent complexity of IF-THEN-ELSE logic, theCASEexpression is one
of the more challenging topics in this book In this chapter, we have focused on
usingCASEexpressions in theSELECTcolumnlist However,CASEexpressions can
also be utilized in other SQL clauses, such as theORDER BYclause, and other clauses
not yet discussed, such as theWHEREandHAVINGclauses
The Searched Format 61
Trang 2Let’s give just one example of additional uses of theCASEexpression Although
we have not yet talked about the WHERE clause, let’s imagine that we know something about it As will be explained in Chapter 7, theWHERE clause allows you to apply selection criteria to the rows that will be presented to the user A typical expression might be something like:
WHERE ProductDescription ¼ 'White Glove'
This is a very specific directive You only want to see rows where the product is a white glove The value of the CASE expression is that it allows you to apply conditional logic to the value you’re looking for, perhaps based on the value of some other column For example, you may have another column, named ProductType, which gives more information about products Using aCASE ex-pression, you can select products that are white gloves if the ProductType equals
X, or products that are socks if the ProductType equals Y In essence, you can substitute aCASEexpression for the value ‘White Glove’ in theWHEREclause in order to describe some more complex logic
Looking Ahead
CASEexpressions can be utilized to provide a logical evaluation for a column or expression in aSELECTcolumnlist There are two basic formats for the
expres-sion: the simple and the searched A typical use is to provide translations for data items with cryptic values Finally, although this chapter is titled ‘‘Column-Based Logic,’’ CASE expressions can be used in places other than the columns in a
SELECTcolumnlist They can be used anyplace where you would like to specify
conditional logic for a specific column or data element
In our next chapter, we are going to move beyond logic as it applies to column values and talk about how to apply logic to the selection of entire rows This is the topic for which you’ve been patiently waiting, no doubt The ability to specify selection criteria in yourSELECT statements is critical to most normal queries
In the real world, it would be very unusual to issue aSELECTstatement without some sort of selection criteria The topics discussed in the next chapter will allow you to accomplish that objective
Chapter 6 ■ Column-Based Logic
62
Trang 3chapter 7
Row-Based Logic
Keywords Introduced: WHERE ,
TOP / LIMIT / ROWNUM
At long last, I am now going to show you how to apply selection criteria to your tables Up until this point, our SELECT statements have always brought back every row in the table This would rarely be the case in real-world situations Normally, you are interested in only retrieving data that meets certain criteria The topics in this chapter will address this issue
If you’re selecting customers, then you would typically only get to see a subset of all your customers If you’re retrieving orders from your customers, you prob-ably only want to see orders that meet certain conditions If you’re looking at products, then you’re probably only interested in viewing certain types of pro-ducts Rarely does someone want to simply see everything Your interest (or anyone else’s) in your data is typically directed toward a small subset of data in order to analyze or view one particular aspect
Applying Selection Criteria
Selection criteria in SQL begins with the WHERE clause The WHERE keyword accomplishes the task of selecting a subset of rows The logic utilized for the
WHERE keyword builds on the column-based logic seen in the last chapter The difference is that, whereas theCASE expression only allowed you to apply logic
to a specific column, you are now going to apply logic to all the rows in a table
63
Trang 4This is the general format of theSELECTstatement, including theWHEREclause and other clauses previously discussed:
SELECT columnlist
FROM tablelist
WHERE condition
ORDER BY columnlist
As can be seen, theWHEREclause must always be between theFROMandORDER BY
clauses In fact, if any clause is used, it must appear in the order shown
Let’s look at an example, taken from data in this Orders table:
OrderID FirstName LastName QuantityPurchased PricePerItem
We’ll start with a statement with a simpleWHEREclause:
SELECT
FirstName,
LastName,
QuantityPurchased
FROM Orders
WHERE LastName ¼ 'Harper'
The output is:
FirstName LastName QuantityPurchased
Since theWHERE clause stipulates to only select rows with a LastName equal to
‘Harper’, only one of the three rows in the table is returned
Notice that the desired value of the LastName column was enclosed in quotes, due to the fact that LastName is a text column For numeric fields, no quotes are necessary For example, the followingSELECT is equally valid and would have returned the same data:
Chapter 7 ■ Row-Based Logic
64
Trang 5FirstName,
LastName,
QuantityPurchased
FROM Orders
WHERE QuantityPurchased ¼ 5
WHERE Clause Operators
In the previous statements, an equals sign (=) is used as the operator in
theWHERE clause The equals sign indicates a test for equality The general
for-mat shown above indicates that a condition follows the WHERE keyword This
condition consists of an operator with two expressions on either side
The following is a list of the basic operators that can be used in theWHEREclause:
WHERE Operator Meaning
More advanced operators will be covered in the next chapter
The meaning of the equals (=) and does not equal (<>) operators should be
obvious Here’s an example of aWHEREclause with an “is greater than” operator,
taken from the same Orders table:
SELECT
FirstName,
LastName,
QuantityPurchased
FROM Orders
WHERE QuantityPurchased > 6
The result is:
FirstName LastName QuantityPurchased
WHERE Clause Operators 65