Loading
SQL ORDER BY
In real life, information is always easier to understand when it is organized.

Examples

  • Student list sorted by marks
  • Product list sorted by price
  • Employees sorted by joining date
When data is stored in a database, it is not saved in any meaningful order for display purposes.
So, when we retrieve data, SQL needs instructions on how to arrange the result.
This is why SQL provides the ORDER BY clause.


What Is the ORDER BY Clause?

The ORDER BY clause is used to sort the result set of a query based on one or more columns.
ORDER BY controls how the output is displayed, not how data is stored in the table.


Syntax

SELECT column1, column2
FROM table_name
ORDER BY column_name;

By default, sorting is done in ascending order.


Sorting Order of ASC and DESC

SQL provides two sorting directions

KeywordMeaning
ASCAscending order (default)
DESCDescending order


Example: employees Table

idnamesalarydepartment
1Amit60000IT
2Neha75000HR
3Rahul50000IT
4Priya90000Finance
5Karan45000HR


Example - 1: Sort by Salary (Ascending)

SELECT name, salary
FROM employees
ORDER BY salary ASC;

Displays employees from lowest to highest salary.


Example - 2: Sort by Salary (Descending)

SELECT name, salary
FROM employees
ORDER BY salary DESC;

Displays employees from highest to lowest salary.


Sorting Text Data

When sorting text columns:

  • ASC - Alphabetical order (A to Z)
  • DESC - Reverse alphabetical order (Z to A)


Example: Sort by Name

SELECT *
FROM employees
ORDER BY name;

Sorted alphabetically by employee name.


ORDER BY with Multiple Columns

Why Multiple Columns Are Needed

Sometimes, one column is not enough for meaningful sorting.

Example

  • First sort by department
  • Then sort by salary within each department


Syntax

SELECT *
FROM table_name
ORDER BY column1, column2;


Example: Sort by Department, then Salary

SELECT *
FROM employees
ORDER BY department ASC, salary DESC;

Employees are grouped by department, and within each department, sorted by salary.


ORDER BY with WHERE Clause

The WHERE clause filters data before sorting.


Example: Sort Delhi Employees by Salary

SELECT *
FROM employees
WHERE department = 'IT'
ORDER BY salary DESC;

Only IT department employees are sorted by salary.


ORDER BY with LIMIT (Top-N Queries)

ORDER BY is commonly used with LIMIT to fetch top or bottom records.


Example: Top 3 Highest Paid Employees

SELECT name, salary
FROM employees
ORDER BY salary DESC
LIMIT 3;

Returns the top 3 highest-paid employees.


Important Points About ORDER BY

  • ORDER BY always comes at the end of the query
  • Default sorting is ascending
  • Sorting affects only the result set
  • ORDER BY can use column names or column positions
  • ORDER BY improves readability of output


Two Minute Drill

  • ORDER BY is used to sort query results
  • ASC sorts in ascending order
  • DESC sorts in descending order
  • Multiple columns can be used for sorting
  • WHERE filters first, ORDER BY sorts later
  • Commonly used with LIMIT for Top-N queries