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, column2FROM table_nameORDER BY column_name;By default, sorting is done in ascending order.
Sorting Order of ASC and DESC
SQL provides two sorting directions
| Keyword | Meaning |
|---|---|
| ASC | Ascending order (default) |
| DESC | Descending order |
Example: employees Table
| id | name | salary | department |
|---|---|---|---|
| 1 | Amit | 60000 | IT |
| 2 | Neha | 75000 | HR |
| 3 | Rahul | 50000 | IT |
| 4 | Priya | 90000 | Finance |
| 5 | Karan | 45000 | HR |
Example - 1: Sort by Salary (Ascending)
SELECT name, salaryFROM employeesORDER BY salary ASC;Displays employees from lowest to highest salary.
Example - 2: Sort by Salary (Descending)
SELECT name, salaryFROM employeesORDER 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 employeesORDER 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_nameORDER BY column1, column2;Example: Sort by Department, then Salary
SELECT *FROM employeesORDER 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
Example: Sort Delhi Employees by Salary
SELECT *FROM employeesWHERE 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
Returns the top 3 highest-paid employees.
Example: Top 3 Highest Paid Employees
SELECT name, salaryFROM employeesORDER BY salary DESCLIMIT 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