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.


Search Keys

  • sql order by clause
  • order by sql
  • sql order by
  • 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 id asc
  • order by case sql
  • oracle order by null last
  • sql order by reverse
  • mssql order by
  • sql order by two columns
  • order by descending sql
  • order by query
  • 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 The Complete Guide to SQL 110 2014 Optimization Pandurengan
5 MySql Interview Questions 75 2015 Optimization Azaghu Varshith
6 SQL for students 65 2007 Administration Balan
7 MySql for professionals 105 2006 Performance Sakunthala
8 SQL Server 2008 Transact-Sql 160 2015 Optimization Chandra
9 Oracle 11g PL/SQL Programming 123.45 2008 Database Vinoth Kumar
10 The Microsoft Data Warehouse 205 2013 Administration Ranjani Mai
11 Data Analysis Using SQL 70 2006 Administration Azaghu Varshith
12 Programming With MySQL 140 2012 Administration Padmavathi

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
11 Data Analysis Using SQL 70 2006 Administration Azaghu Varshith
6 SQL for students 65 2007 Administration Balan
12 Programming With MySQL 140 2012 Administration Padmavathi
1 Oracle PL/sql By Example 150 2013 Administration Padmavathi
3 SQL Design Patterns 85 2009 Administration Ramanathan
10 The Microsoft Data Warehouse 205 2013 Administration Ranjani Mai

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
11 Data Analysis Using SQL 70 2006 Administration Azaghu Varshith
6 SQL for students 65 2007 Administration Balan
5 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 > 6
ORDER BY AuthorName DESC, DomainName ASC;

The result of above query is:

AuthorName DomainName
Vinoth Kumar Database
Sakunthala Performance
Ranjani Mai Administration
Padmavathi Administration
Chandra Optimization
Azaghu Varshith Administration

Search Keys

  • sql order by case
  • sql order by example
  • order by desc sql server
  • order by clause in sql
  • order by example
  • 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
  • sort ascending
  • sql sort by date
  • sort descending
  • sql server sort by
  • sort the selected table in ascending order
  • sql sort by asc
  • sql sort column
  • arrange numbers in ascending order
  • select desc sql
  • desc sql