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.

You can also search these topics using sql order by clause, sql order by ascending, sql order by date, sql group by order by, order by multiple columns sql, order by alphabetical sql, order by date sql, sql order by number, sql view order by, multiple order by, multiple order by sql, sql order by 2 columns, order by case sql, oracle order by null last, sql order by reverse, sql order by two columns, order by descending sql, sql order by multiple columns.

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
You can also search these topics using sql order by case, sql order by example, order by desc sql server, order by clause in sql, sql order by command, sql order by multiple fields, order by two columns in sql, order by clause in sql with example, t sql order by descending, sql select from where order by, sql order by descending and ascending, sql query order by date, sql order by keyword, sql server sort by, sort the selected table in ascending order, arrange numbers in ascending order.