The SQL ORDER BY Clause is used to arrange query results in a specific order. When data is retrieved from a database table, the records are not guaranteed to appear in any particular sequence unless sorting is explicitly applied. The ORDER BY clause allows users to organize information in ascending or descending order based on one or more columns.
Sorting data is an essential requirement in database systems. For example, students may want to see their marks ranked from highest to lowest, customers may want products sorted by price, and administrators may need employee records arranged alphabetically. All of these tasks are performed using the ORDER BY clause.
The ORDER BY clause improves readability, analysis, reporting, and decision-making by presenting information in a structured format.
The ORDER BY clause is used with the SELECT statement to sort records retrieved from a table. It can arrange data in ascending order (smallest to largest, A to Z) or descending order (largest to smallest, Z to A).
Without ORDER BY, the database may return records in an unpredictable order depending on storage and query execution.
SELECT column_name FROM table_name ORDER BY column_name;
This query retrieves records and sorts them according to the specified column.
| StudentID | StudentName | Course | Marks |
|---|---|---|---|
| 101 | Rahul | BCA | 82 |
| 102 | Priya | B.Tech | 91 |
| 103 | Amit | MCA | 75 |
| 104 | Neha | BCA | 88 |
Ascending order arranges data from smallest to largest for numbers and from A to Z for text values.
ASC is the default sorting order in SQL.
SELECT * FROM Student ORDER BY Marks ASC;
| StudentName | Marks |
|---|---|
| Amit | 75 |
| Rahul | 82 |
| Neha | 88 |
| Priya | 91 |
Students are displayed from the lowest marks to the highest marks.
Descending order arranges data from largest to smallest for numbers and from Z to A for text values.
SELECT * FROM Student ORDER BY Marks DESC;
| StudentName | Marks |
|---|---|
| Priya | 91 |
| Neha | 88 |
| Rahul | 82 |
| Amit | 75 |
Students are displayed from highest marks to lowest marks.
ORDER BY can arrange text values alphabetically.
SELECT * FROM Student ORDER BY StudentName ASC;
| StudentName |
|---|
| Amit |
| Neha |
| Priya |
| Rahul |
Names are sorted alphabetically from A to Z.
SELECT * FROM Student ORDER BY StudentName DESC;
Names are displayed from Z to A.
Numeric columns such as marks, salary, age, and price can be sorted efficiently using ORDER BY.
SELECT * FROM Student ORDER BY Marks DESC;
The highest-scoring students appear first.
Dates can also be sorted using ORDER BY.
Consider an Orders table:
| OrderID | OrderDate |
|---|---|
| 1 | 2026-07-10 |
| 2 | 2026-07-15 |
| 3 | 2026-07-05 |
SELECT * FROM Orders ORDER BY OrderDate ASC;
Orders are displayed from oldest to newest.
SQL allows sorting using column numbers instead of column names.
SELECT StudentID, StudentName, Marks FROM Student ORDER BY 3 DESC;
The number 3 represents the third selected column, which is Marks.
Although supported, using column names is generally recommended because queries remain easier to understand.
SELECT StudentName, Marks FROM Student ORDER BY Marks DESC;
Only selected columns are displayed while sorting is still applied.
| EmployeeID | EmployeeName | Salary |
|---|---|---|
| 1 | Amit | 40000 |
| 2 | Neha | 65000 |
| 3 | Rohan | 55000 |
SELECT * FROM Employee ORDER BY Salary DESC;
Employees are displayed from highest salary to lowest salary.
SELECT ProductName, Price FROM Products ORDER BY Price ASC;
Products appear from the lowest price to the highest price.
SELECT StudentName, Marks FROM Student ORDER BY Marks DESC;
Displays student rankings based on marks.
SELECT ProductName, Price FROM Products ORDER BY Price ASC;
Displays products from the cheapest to the most expensive.
SELECT CustomerName, Balance FROM Customer ORDER BY Balance DESC;
Shows customers with the highest account balances first.
In practical database applications, sorting based on a single column is often not sufficient. SQL allows multiple columns to be specified in the ORDER BY clause. When the values of the first column are identical, SQL automatically uses the next column for sorting.
SELECT column_names FROM table_name ORDER BY column1 ASC, column2 ASC;
SELECT StudentName, Course, Marks FROM Student ORDER BY Course ASC, Marks DESC;
In this query, students are first grouped according to their course names. Within each course, students are sorted according to marks in descending order.
| StudentName | Course | Marks |
|---|---|---|
| Neha | BCA | 90 |
| Rahul | BCA | 80 |
| Priya | B.Tech | 95 |
| Amit | MCA | 75 |
Each column in ORDER BY can have its own sorting direction.
SELECT * FROM Employee ORDER BY Department ASC, Salary DESC;
Departments are sorted alphabetically, while salaries within each department are displayed from highest to lowest.
The WHERE clause filters records, while ORDER BY sorts the filtered results. These two clauses are commonly used together.
SELECT StudentName, Marks FROM Student WHERE Marks >= 70 ORDER BY Marks DESC;
Only students scoring 70 or more marks are displayed, and the results are sorted from highest to lowest marks.
DISTINCT removes duplicate values, while ORDER BY arranges the unique results.
SELECT DISTINCT Course FROM Student ORDER BY Course ASC;
The query displays all unique course names in alphabetical order.
After records are grouped using GROUP BY, the resulting groups can be sorted using ORDER BY.
SELECT Course, COUNT(*) AS TotalStudents FROM Student GROUP BY Course ORDER BY TotalStudents DESC;
Courses with the highest number of students appear first.
Aggregate functions such as COUNT(), SUM(), AVG(), MAX(), and MIN() often produce summarized results. ORDER BY can sort these summaries.
SELECT Department, AVG(Salary) AS AverageSalary FROM Employee GROUP BY Department ORDER BY AverageSalary DESC;
Departments are sorted according to their average salary values.
When data is retrieved from multiple tables using JOIN, ORDER BY helps organize the combined results.
SELECT Student.StudentName, Course.CourseName FROM Student INNER JOIN Course ON Student.CourseID = Course.CourseID ORDER BY Student.StudentName ASC;
Students are displayed alphabetically after combining information from both tables.
Aliases improve readability and can also be used in sorting.
SELECT StudentName, Marks AS Score FROM Student ORDER BY Score DESC;
The query sorts records using the alias Score instead of the original column name.
Sorting can also be applied to calculated values.
SELECT ProductName, Price, Quantity, Price * Quantity AS TotalValue FROM Products ORDER BY TotalValue DESC;
Products are sorted according to their total inventory value.
NULL values represent missing or unknown information. Their position in sorted results depends on the database system being used.
Some databases place NULL values first, while others place them last.
SELECT * FROM Employee ORDER BY Bonus ASC;
Employees with missing bonus values may appear at the beginning or end of the result set depending on database configuration.
ORDER BY is frequently used with LIMIT to retrieve only the top records.
SELECT * FROM Student ORDER BY Marks DESC LIMIT 5;
Displays the top five students based on marks.
SELECT TOP 5 * FROM Student ORDER BY Marks DESC;
Returns the five highest-scoring students.
Sorting requires additional processing. On large databases, inefficient sorting can slow query execution.
SELECT StudentName, Marks FROM Student ORDER BY Marks DESC;
The query generates a ranking list where students with the highest marks appear first.
SELECT ProductName, Price FROM Products ORDER BY Price ASC;
Customers can view products from the lowest price to the highest price.
SELECT CustomerName, Balance FROM Accounts ORDER BY Balance DESC;
Bank managers can quickly identify customers with the highest account balances.
SELECT EmployeeName, Salary FROM Employee ORDER BY Salary DESC;
Management can review employees according to salary levels.
It is used to sort query results in ascending or descending order.
Ascending order (ASC).
DESC.
Yes, alphabetically.
Yes, from smallest to largest or vice versa.
Yes, dates can be sorted chronologically.
Yes, multiple columns can be specified.
SQL uses ASC by default.
Yes.
Yes.
Yes.
Yes.
Yes.
WHERE executes before ORDER BY.
Yes.
They improve sorting performance.
Alphabetical order from A to Z.
To retrieve a limited number of sorted records.
To retrieve top records in SQL Server.
Yes, especially on large datasets.
Yes.
No.
Yes, although column names are recommended.
Generating rankings and reports.
It organizes data in a meaningful and readable format.
The SQL ORDER BY Clause is a powerful tool for arranging database records in a meaningful sequence. Whether sorting student marks, employee salaries, product prices, or transaction dates, ORDER BY improves readability and analysis. It supports ascending and descending sorting, multiple columns, joins, grouping, filtering, and aggregate functions.
A strong understanding of ORDER BY is essential for database developers, analysts, and students because organized data leads to better reporting, faster decision-making, and more effective database applications.