Loading

The SQL ORDER BY Keyword

  • In SQL, the ORDER BY clause is used to sort the result set of a query based on one or more columns.
  • It sorts the result set in ascending or descending order.

 Syntax:

SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;

EXAMPLE:

Let's consider the Employee table with columns id, name and salary:

If you want to Order them by their salary:

SELECT*
FROM employee
ORDER BY salary;

Output:

  • By default, it will sort from lowest to highest salary

If you want to order in descending order then we will use DESC:

SELECT*
FROM employees
ORDER BY salary DESC;