Ted Lindsey Ca
Shane Weebe Ca
David Levinson Ca
Geoff Kim Ca
(6 row(s) affected)
Sorting Results Using ORDER BY
Unless you specify some sorting criteria, SQL Server cant guarantee to return
the results in a particular order. Well most likely receive the results sorted by
the primary key, because its easier for SQL Server to present the results in this
way than any other, but this ordering isnt guaranteed. This explains why, in
some of the examples weve completed so far, the order of the results you see on
your machine may differ from what you see in this book. The ORDER BY clause
provides you with a quick way to sort the results of your query in either ascending
or descending order. For instance, to retrieve the names of your employees in
alphabetical order, you would need to execute this command:
SELECT EmployeeID, Name
FROM Employees
ORDER BY Name
Looks simple, doesnt it?
EmployeeID Name
----------- --------------------------------------------------
5 David Levinson
6 Geoff Kim
2 Jessica Ruvalcaba
4 Shane Weebe
3 Ted Lindsey
1 Zak Ruvalcaba
(6 row(s) affected)
Note that the default ordering here is ascending (i.e. running from A to Z). You
could add the DESC designation (for descending) to the end of the statement, to
order the results backwards:
SELECT EmployeeID, Name
FROM Employees
ORDER BY Name DESC
306
Chapter 8: Speaking SQL

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition 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.