SQL ORDER BY Keyword
The SQL ORDER BY keyword is used to sort the query results in ascending or descending order based on the columns(one or more) that you specify to order by.
It is used with SQL Select statement for arranging query result in particular order.
To sort query results in descending order, We can use DESC keyword with sql Order by clause.
Related Links
SQL ORDER BY Syntax
The basic syntax of SQL ORDER By clause is as follows:
SELECT column_name1, column_name2
FROM table_name
ORDER BY column_name ASC|DESC, column_name ASC|DESC;
Note: Which column you are going to use in order by clause, that column should be present in column-list.
Sample Database Table - Books
BookId | BookName | BookPrice | RelYear | DomainName | AuthorName |
---|---|---|---|---|---|
1 | Oracle PL/sql By Example | 150 | 2013 | Administration | Padmavathi |
2 | Red Gate Guide to SQL Server | 71.87 | 2010 | Performance | Nirmala |
3 | SQL Design Patterns | 85 | 2009 | Administration | Ramanathan |
4 | MySql Interview Questions | 75 | 2015 | Optimization | Azaghu Varshith |
5 | SQL for students | 65 | 2007 | Administration | Balan |
6 | Data Analysis Using SQL | 70 | 2006 | Administration | Azaghu Varshith |
SQL ORDER BY Clause Example
The following SQL statement selects all books which domainname "Administration" from the "Books" table, sorted by the "AuthorName" column:
SELECT * FROM Books
WHERE DomainName = 'Administration'
ORDER BY AuthorName;
The result of above query is:
BookId | BookName | BookPrice | RelYear | DomainName | AuthorName |
---|---|---|---|---|---|
6 | Data Analysis Using SQL | 70 | 2006 | Administration | Azaghu Varshith |
5 | SQL for students | 65 | 2007 | Administration | Balan |
1 | Oracle PL/sql By Example | 150 | 2013 | Administration | Padmavathi |
3 | SQL Design Patterns | 85 | 2009 | Administration | Ramanathan |
Note: The SQL ORDER BY keyword sorts the query result in ascending order by default.
SQL ORDER BY Clause Example By DESC Order
The following SQL statement selects all books which bookprice less than "100" from the "Books" table, sorted in the reverse order by the "BookId" column:
SELECT * FROM Books
WHERE BookPrice < 100
ORDER BY BookID DESC;
The result of above query is:
BookId | BookName | BookPrice | RelYear | DomainName | AuthorName |
---|---|---|---|---|---|
6 | Data Analysis Using SQL | 70 | 2006 | Administration | Azaghu Varshith |
5 | SQL for students | 65 | 2007 | Administration | Balan |
4 | MySql Interview Questions | 75 | 2015 | Optimization | Azaghu Varshith |
3 | SQL Design Patterns | 85 | 2009 | Administration | Ramanathan |
2 | Red Gate Guide to SQL Server | 71.87 | 2010 | Performance | Nirmala |
SQL ORDER BY Clause With Multiple Columns Example
The following SQL statement selects all books which BookID greater than "6" from the "Books" table, sorted by the "DomainName" and "AuthorName" column:
SELECT AuthorName, DomainName FROM Books
WHERE BookID > 3
ORDER BY AuthorName DESC, DomainName ASC;
The result of above query is:
AuthorName | DomainName |
---|---|
Balan | Administration |
Azagu Varshith | Administration |
Azagu Varshith | Optimization |
Related Links