Receive all updates via Facebook. Just Click the Like Button Below

Blogger Widgets
/

Watch Intro About My Website ! ! !

Sunday, 3 November 2013


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:

Id
LastName
FirstName
Address
City
1
Navis
Anto
Madras
TVL
2
Ji
Bala
Bombay
TVL
3
Christopher
Franklin
America
KK
4
Nilsen
Tom
Vingvn 23
KK

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:

Id
LastName
FirstName
Address
City
1
Navis
Anto
Madras
TVL
4
Nilsen
Tom
Vingvn 23
KK
3
Christopher
Franklin
America
KK
2
Ji
Bala
Bombay
TVL

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:

Id
LastName
FirstName
Address
City
2
Ji
Bala
Bombay
TVL
3
Christopher
Franklin
America
KK
4
Nilsen
Tom
Vingvn 23
KK
1
Navis
Anto
Madras
TVL


Tagged:

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...