III.6.1. Using ORDER BY to Sort Your Results
Often, you need your data returned in a certain order, such as alphabetically or based on a number (say, sales figures). The only way to ensure your data is returned in a certain order is by using the ORDER BY clause within a SELECT statement.
The basic syntax of an ORDER BY clause is shown in the following code:
USE AdventureWorks2008; GO SELECT * from Person.Person ORDER BY LastName
The ORDER BY clause follows the column list, the WHERE clause, and any joins in the SELECT statement. This returns an alphabetical listing of all the people in the Person.Contact table.
The following steps show you how to create and run a query with the ORDER BY clause. These steps assume you have AdventureWorks2008 installed on your system.
|
Launch SQL Server Management Studio (SSMS).
Choose StartAll ProgramsMicrosoft SQL Server 2008 ...
Get Microsoft® SQL Server™ 2008 All-In-One Desk Reference For Dummies® now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.