Home SQL Basic SQL Order By
09.09.2010
Main Menu
Programming
.NET Controls
Operating System
Databases
Articles
Add to: Digg Add to: Del.icoi.us Add to: Reddit Add to: StumbleUpon Add to: Slashdot Add to: Yahoo Add to: Technorati Add to: Google
SQL Order By

In the previous few tutorials, you have learned how to select and filter out data from the database. However, there are times you might want to sort the data in a particular order and this could be either descending or ascending.  When sorting the data you could sort it base on the numerical value or alphabetical value. In order to achieve the sorting result we must use the ORDER BY keyword.

SYNTAX:

    1
     SELECT "column_name"
    2     FROM "table_name"
    3     [WHERE "condition"]
    4     ORDER BY "column_name" [ASC, DESC]

The ORDER BY keyword does not need to be used in conjunction with the WHERE keyword, however if you have a WHERE clause in your SQL statement then the ORDER BY keyword must be place after the WHERE clause. The ASC means that the return result will be sorted in ascending order and the DESC means that the return result will be sorted in the descending order.

The ORDER BY keyword also allow you to sort base on multiple column. For example if you want to sort by the First name and then by the last name then you would have to use the ORDER BY clause like this:

Table: Student

StudentID LastName FirstName DOB CourseFees CourseName
1 John Astone 07/15/1976 1000.00 Accountant
2 Bob Eastwood 02/13/1935 1500.00 Economic
3 Jane Hollywood 03/23/1939 2000.00 IT
4 Bob Eastwood 03/19/1980 3000.00 Economic

Example #1

In this example we will attempt to sor the result by student Firstname ascending.

    SELECT *

    FROM Student

    ORDER BY FirstName ASC 

RESULT:

StudentID LastName FirstName DOB CourseFees CourseName
1 John Astone 07/15/1976 1000.00 Accountant
2 Bob Eastwood 02/13/1935 1500.00 Economic
4 Bob Eastwood 03/19/1980 3000.00 Economic
3 Jane Hollywood 03/23/1939 2000.00 IT

Example #2

Sort by multiple columns.

    SELECT *

    FROM Student

    ORDER BY FirstName, LastName ASC

RESULT:

StudentID LastName FirstName DOB CourseFees CourseName
1 John Astone 07/15/1976 1000.00 Accountant
2 Bob Eastwood 02/13/1935 1500.00 Economic
4 Bob Eastwood 03/19/1980 3000.00 Economic
3 Jane Hollywood 03/23/1939 2000.00 IT

Example #3

Sort by descending.

    SELECT *

    FROM Student

    ORDER BY FirstName, LastName DESC

RESULT:

StudentID LastName FirstName DOB CourseFees CourseName
1 John Astone 07/15/1976 1000.00 Accountant
2 Bob Eastwood 02/13/1935 1500.00 Economic
4 Bob Eastwood 03/19/1980 3000.00 Economic
3 Jane Hollywood 03/23/1939 2000.00 IT

 



Comments (0)
Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
:D:angry::angry-red::evil::idea::love::x:no-comments::ooo::pirate::?::(
:sleep::););)):0
Security
Please input the anti-spam code that you can read in the image.

!joomlacomment 4.0 Copyright (C) 2009 Compojoom.com . All rights reserved."