Java Reference
In-Depth Information
Chapter 8: Organizing Search Results and Using
Indexes
In This Chapter
This chapter discusses various ways of organizing and analyzing the data returned by SQL queries.
These include sorting the data by one or more columns, grouping the data and performing statistical
analysis, and filtering the grouped results.
The chapter also addresses the use of indexes to make your queries more efficient. Using indexes
wisely can result in a very significant improvement in performance, while using indexes incorrectly can
result in very poor performance.
The final topic discussed in this chapter is the use of Views. Views provide a means of creating
temporary tables based on a particular query.
Using ORDER BY to Sort the Results of a Query
A common requirement when retrieving data from an RDBMS by using the SELECT statement is to sort
the results of the query in alphabetic or numeric order on one or more of the columns. You sort the
results by using the ORDER BY clause in a statement like this:
SELECT First_Name, Last_Name, City, State
FROM CUSTOMERS
WHERE Last_Name = 'Corleone'
ORDER BY First_Name;
This gives you a list of all the Corleones sorted in ascending order by first name, as shown in Table
8-1 :
Table 8-1: Records Sorted Using ORDER BY
First_Name
Last_Name
City
State
Francis
Corleone
New York
NY
Fredo
Corleone
New York
NY
Michael
Corleone
New York
NY
Sonny
Corleone
Newark
NJ
Vito
Corleone
Newark
NJ
The default sort order is ascending. This can be changed to descending order by adding the DESC
keyword as shown in the next example:
Note
The keywords ASC and DESC can be used to specify ascending or descending sort
 
Search WWH ::




Custom Search