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

The Language of SQL- P17 pot

5 233 0

Đ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 98,03 KB

Nội dung

In 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: FirstName LastName William Smith Natalie Lopez 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 the CASE expression. For example, a valid CASE expression 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 the WHERE 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 Logic66 databases. In Microsoft SQL Server, the keyword that accomplishes this task is TOP. The general format is: SELECT TOP number columnlist FROM table DATABASE DIFFERENCES: MySQL and Oracle 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 Microsoft TOP keyword. If you’re using MySQL or Oracle, you can simply substitute the equivalent LIMIT or ROWNUM keywords. 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 statem ent returns all columns in the first 10 rows from the table. Like any SELECT statement without an ORDER BY clause, there’s no way to predict which 10 rows will be returned. It depends on how the data is physically stored in the table. Limiting Rows 67 Similarly, 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 the TOP keyword 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 the TOP keyword is to use it in combination with the ORDER BY clause to obtain a certain number of rows with the highest values, based on a specified catego ry. This type of data selection is commonly referred to as a Top N selection. Here’s an example, taken from this Books table: BookID Title Author CurrentMonthSales 1 Pride and Prejudice Austen 15 2 Animal Farm Orwell 7 3 Merchant of Venice Shakespeare 5 4 Romeo and Juliet Shakespeare 8 5 Oliver Twist Dickens 3 6 Candide Voltaire 9 7 The Scarlet Letter Hawthorne 12 8 Hamlet Shakespeare 2 Let’s say that you want to see the three books that sold the most in the current month. The SELECT to accomplish this is: SELECT TOP 3 Title AS 'Book Title', CurrentMonthSales AS 'Quantity Sold' FROM Books ORDER BY CurrentMonthSales DESC Chapter 7 ■ Row-Based Logic68 The output is: Book Title Quantity Sold Pride and Prejudice 15 The Scarlet Letter 12 Candide 9 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 BY clause. If there were no ORDER BY clause, then SELECT would 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 a WHERE clause, 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: Book Title Quantity Sold Romeo and Juliet 8 The WHERE clause adds the qualification that you are only looking at books by Shakespeare. You also revised the TOP keyword to specify TOP 1, indicating that you only want to see one row of data. Limiting Rows with a Sort 69 DATABASE DIFFERENCES: Oracle 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 our SELECT statement truly useful. With the WHERE clause, 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 BY clause allows for a useful Top N type of data selection. In our next chapter, “Boolean Logic,” I am going to great ly enhance our selection criteria capabilities by introducing a bunch of new keywords that add sophisti- cated logic to the WHERE clause. 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 Logic70 . Sort Another use of the TOP keyword is to use it in combination with the ORDER BY clause to obtain a certain number of rows with the highest values, based on a specified catego ry. This type of data. 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. statement truly useful. With the WHERE clause, 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

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