Description

The SQL keyword ORDER BY is used to sort the records returned by a SELECT statement, by adding the clause ORDER BY at the end of a statement.

  • Without the ORDER BY clause, the records returned by a SELECT statement are not in any particular order.
  • The ORDER BY clause must be defined at the end of a SELECT statement.
  • The sort order can be ascending or descending.
  • The default sort order is ascending.
  • The column name must be stated before defining the sort order.

ORDER BY 

The below SQL fetches all the employees, sorted by employee name in default sort order (which is ascending) as the sort order is not specified.

SELECT * 
FROM employees
ORDER BY employee_name;

ORDER BY ASC

The below SQL fetches all the employees, sorted by employee name in ascending order.

SELECT * 
FROM employees
ORDER BY employee_name ASC;

ORDER BY DESC

The below SQL fetches all the employees, sorted by employee name in descending order.

 

SELECT * 
FROM employees
ORDER BY employee_name DESC;

ORDER BY Multiple Columns

The below SQL fetches all the employees, sorted by hire date in descending order and employee name in ascending order.

SELECT * 
FROM employees 
ORDER BY hire_date DESC, employee_name ASC;

Related Links