SQL ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set.
The ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set by a specified column.
The ORDER BY keyword sort the records in ascending order by default.
If you want to sort the records in a descending order, you can use the DESC
keyword.
SQL ORDER BY Syntax
SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC
|
ORDER BY Example
The "Persons" table:
| P_Id |
LastName |
FirstName |
Address |
City |
| 1 |
Hansen |
Ola |
Timoteivn 10 |
Sandnes |
| 2 |
Svendson |
Tove |
Borgvn 23 |
Sandnes |
| 3 |
Pettersen |
Kari |
Storgt 20 |
Stavanger |
| 4 |
Nilsen |
Tom |
Vingvn 23 |
Stavanger |
Now we want to select all the persons
from the table above, however, we want to sort the persons by their last name.
We use
the following SELECT
statement:
SELECT * FROM Persons
ORDER BY LastName
|
The result-set will look like this:
| P_Id |
LastName |
FirstName |
Address |
City |
| 1 |
Hansen |
Ola |
Timoteivn 10 |
Sandnes |
| 4 |
Nilsen |
Tom |
Vingvn 23 |
Stavanger |
| 3 |
Pettersen |
Kari |
Storgt 20 |
Stavanger |
| 2 |
Svendson |
Tove |
Borgvn 23 |
Sandnes |
ORDER BY DESC Example
Now we want to select all the persons
from the table above, however, we want to sort the persons descending by their last name.
We use
the following SELECT
statement:
SELECT * FROM Persons
ORDER BY LastName DESC
|
The result-set will look like this:
| P_Id |
LastName |
FirstName |
Address |
City |
| 2 |
Svendson |
Tove |
Borgvn 23 |
Sandnes |
| 3 |
Pettersen |
Kari |
Storgt 20 |
Stavanger |
| 4 |
Nilsen |
Tom |
Vingvn 23 |
Stavanger |
| 1 |
Hansen |
Ola |
Timoteivn 10 |
Sandnes |
Learn how your website performs under various load conditions
 |
|
WAPT
is a load, stress and performance testing tool for websites and web-based applications.
In contrast to "800-pound gorilla" load testing tools, it is designed to minimize the learning
curve and give you an ability to create a heavy load from a regular workstation.
WAPT is able to generate up to 3000 simultaneously acting virtual users using standard hardware configuration.
Virtual users in each profile are fully customizable. Basic and NTLM authentication methods are supported.
Graphs and reports are shown in real-time at different levels of detail, thus helping to manage the testing process.
Download the free 30-day trial!
|
|