Your Ad Here
SQL Basic

Bookmark and Share
SQL ORDER BY

Some times we need to list the output in a particular order. This could be in ascending order or descending order. Also could be based on either numerical value or text value. In this case, we can use the ORDER BY keyword to get our result. The syntax for an ORDER BY statement is:

SELECT "column_name"
FROM "table_name"
[WHERE "condition"]
ORDER BY "column_name" [ASC, DESC]

Here the WHERE statement is optional. ASC is nothing but ascending. DESC is nothing but descending.

The result will be shown by ascending order based on that column name if we use ASC following by ORDER BY keyword. The result will be shown by descending order based on that column name if we use DESC following by ORDER BY keyword.

let us assume the below table as a price information for a book shop. Table name is price_information.

serial_no book_name sales_price Date
1 J2EE Volume II $15 Aug-10-2009
2 Learn CSS 30 Days $12 Aug-12-2009
3 Core Java Volume II $10 Aug-15-2009
4 Photoshop in 24 hours $20 Aug-18-2009
 
Example 1:

Executed Query:
SELECT * FROM price_information ORDER BY book_name ASC

Result:
serial_no book_name sales_price Date
3 Core Java Volume II $10 Aug-15-2009
1 J2EE Volume II $15 Aug-10-2009
2 Learn CSS 30 Days $12 Aug-12-2009
4 Photoshop in 24 hours $20 Aug-18-2009

Description:
The query will return the result with ascending order by book_name

Today's Deal