For example, a validCASEexpression might be: CASE WHEN column1 > value1 THEN result1 END Limiting Rows What do you do if you want to select a small subset of the rows in a table, but you
Trang 1In this example, only one row meets the qualification that the QuantityPurchased column be greater than 6 Although not as commonly used, it’s also possible to use the “is greater than” operator with a text column This example:
SELECT
FirstName,
LastName
FROM Orders
WHERE LastName > 'K'
returns:
Since the test is for last names greater than K, it only brings back Smith and Lopez, but not Harper When applied to text fields, the greater than and less than operators indicate selection by the alphabetic order of the values In this case, Smith and Lopez are returned, since S and L are after K in the alphabet
Finally, it should be noted that all of these operators can also be used with the WHEN keyword in the searched format of theCASE expression For example, a validCASEexpression might be:
CASE
WHEN column1 > value1 THEN result1
END
Limiting Rows
What do you do if you want to select a small subset of the rows in a table, but you don’t care which rows are returned? Let’s say that you have a table with 50,000 rows, and you want to see just a few rows of data to see what it looks like It wouldn’t make sense to use theWHERE clause for this purpose, since you don’t care which rows are returned
The solution is to use a special keyword to specify that you want to limit how many rows are returned This is another instance where the syntax differs among
Chapter 7 ■ Row-Based Logic
66
Trang 2databases In Microsoft SQL Server, the keyword that accomplishes this task
isTOP
The general format is:
SELECT
TOP number
columnlist
FROM table
D A T A B A S E D I F F E R E N C E S : M y S Q L a n d O r a c l e
MySQL uses the keyword LIMIT rather than TOP The general format is:
SELECT
columnlist
FROM table
LIMIT number
Oracle uses the keyword ROWNUM rather than TOP The ROWNUM keyword needs to be specified in
a WHERE clause, as follows:
SELECT
columnlist
FROM table
WHERE ROWNUM <= number
In the remainder of this chapter, you’ll see statements using the MicrosoftTOP
keyword If you’re using MySQL or Oracle, you can simply substitute the
equivalentLIMITorROWNUMkeywords
Let’s say that you want to see the first 10 rows from a table The SELECT to
accomplish this looks like:
SELECT
TOP 10 *
FROM table
This statement returns all columns in the first 10 rows from the table Like any
SELECTstatement without anORDER BYclause, there’s no way to predict which
10 rows will be returned It depends on how the data is physically stored in the
table
Trang 3Similarly, you can list specific columns to return:
SELECT
TOP 10
column1, column2
FROM table
In essence, the TOP keyword accomplishes something similar to the WHERE clause, because it permits you to return a small subset of rows in the specified table Keep in mind, though, that rows returned using theTOPkeyword are not a true random sample in a statistical sense They’re only the first rows that qualify, based on how the data is physically stored in the database
Limiting Rows with a Sort
Another use of theTOPkeyword is to use it in combination with theORDER BY clause to obtain a certain number of rows with the highest values, based on a
specified category This type of data selection is commonly referred to as a Top N
selection Here’s an example, taken from this Books table:
3 Merchant of Venice Shakespeare 5
Let’s say that you want to see the three books that sold the most in the current month TheSELECTto accomplish this is:
SELECT
TOP 3
Title AS 'Book Title',
CurrentMonthSales AS 'Quantity Sold'
FROM Books
ORDER BY CurrentMonthSales DESC
Chapter 7 ■ Row-Based Logic
68
Trang 4The output is:
Pride and Prejudice 15
The Scarlet Letter 12
Let’s examine this statement in some detail The TOP 3 in the second line
indicates that only three rows are to be returned The main question to ask is
how it determines which three rows to display The answer is found in the
ORDER BYclause If there were noORDER BYclause, thenSELECTwould simply
bring back any three rows of data, but that’s not what you want You’re looking
for the three rows with the highest sales In order to accomplish this, you need to
sort the rows by the CurrentMonthSales column in descending order Why
descending? Because when you sort in descending order, the highest numbers
appear first If you had sorted in ascending order, you would get the books with
the least amount of sales, not the most
Now, let’s add one more twist to this scenario Let’s say that you only want to see
the book by Shakespeare that sold the most In order to accomplish this, you
need to add aWHEREclause, as follows:
SELECT
TOP 1
Title AS 'Book Title',
CurrentMonthSales AS 'Quantity Sold'
FROM Books
WHERE Author ¼ 'Shakespeare'
ORDER BY CurrentMonthSales DESC
This brings back this data:
Romeo and Juliet 8
TheWHERE clause adds the qualification that you are only looking at books by
Shakespeare You also revised theTOPkeyword to specifyTOP 1, indicating that
you only want to see one row of data
Trang 5D A T A B A S E D I F F E R E N C E S : O r a c l e
The procedure for limiting and sorting rows in Oracle is a bit more complex, since the ROWNUM is in the WHERE clause in Oracle syntax You need to sort the data first and then apply the ROWNUM selection criteria The general format is:
SELECT *
FROM
(SELECT
columnlist
FROM table
ORDER BY columnlist DESC)
WHERE ROWNUM <= number
This is an early example of a subquery, which will be covered in detail in Chapter 14 In brief, this statement consists of two separate SELECT statements The inner SELECT, enclosed in parentheses, sorts the desired data by the specified columnlist, in descending order The outer SELECT statement then retrieves data from the inner SELECT using the ROWNUM keyword to limit the number of rows that are displayed.
Looking Ahead
This chapter introduced the topic of how to apply selection criteria to queries A number of basic operators, such as equals and greater than, were introduced The ability to specify some basic selection criteria goes a long way towards making ourSELECTstatement truly useful With theWHEREclause, you can now issue a statement that retrieves all customers from the state of New York
The related topic of limiting the number of rows returned in a query was also covered in this chapter Finally, the ability to limit rows in combination with an ORDER BYclause allows for a useful Top N type of data selection
In our next chapter, “Boolean Logic,” I am going to greatly enhance our selection criteria capabilities by introducing a bunch of new keywords that add sophisti-cated logic to theWHEREclause Yes, it’s true that you can now select customers from the state of New York, but what if you wanted to select customers who are
in New York or California, but not in Buffalo or Los Angeles? The keywords covered in the next chapter will allow you to do that
Chapter 7 ■ Row-Based Logic
70