SQL Topics
ORDER BY Clause
title: ORDER BY Clause
When retrieving data from a database, records are not always displayed in a meaningful order. In many situations, users need information sorted alphabetically, numerically, chronologically, or based on business requirements.
For example:
- A school may want students sorted by marks.
- An online store may display products by price.
- A company may list employees by salary.
- A bank may sort transactions by date.
Without sorting, finding relevant information becomes difficult, especially when working with large datasets.
To solve this problem, SQL provides the ORDER BY Clause.
The ORDER BY clause is used to sort records returned by a query. It allows data to be arranged in ascending or descending order based on one or more columns.
In this lesson, you will learn how ORDER BY works, how sorting is performed, how to sort by multiple columns, and best practices for efficient data organization.
What is ORDER BY?
The ORDER BY clause is used to sort the result set of a query.
Example:
SELECT *
FROM Students
ORDER BY Age;Result:
| StudentID | Name | Age |
|---|---|---|
| 3 | Amit | 18 |
| 1 | Rahul | 20 |
| 2 | Priya | 22 |
The records are sorted by age.
Without ORDER BY, the database may return rows in any order.
Why is ORDER BY Important?
Consider a products table containing:
10,000 ProductsWithout sorting:
Laptop
Mouse
Phone
Keyboard
MonitorThe order may appear random.
With ORDER BY:
SELECT *
FROM Products
ORDER BY Price;Products are displayed in a meaningful sequence.
Benefits include:
- Easier analysis
- Better reporting
- Improved user experience
- Faster information retrieval
Basic ORDER BY Syntax
The general syntax is:
SELECT ColumnName
FROM TableName
ORDER BY ColumnName;Example:
SELECT *
FROM Students
ORDER BY Name;This sorts records alphabetically by name.
Understanding the Syntax
Example:
SELECT *
FROM Students
ORDER BY Age;SELECT
Specifies data to retrieve.
FROM
Specifies the source table.
ORDER BY
Specifies the sorting operation.
Age
Column used for sorting.
The database arranges records according to the values in the specified column.
Default Sorting Order
By default, ORDER BY sorts data in ascending order.
Example:
SELECT *
FROM Students
ORDER BY Age;Result:
18
20
22
25Ascending means:
- Smallest to largest
- A to Z
- Oldest date to newest date
Ascending Order (ASC)
The ASC keyword explicitly specifies ascending order.
Example:
SELECT *
FROM Students
ORDER BY Age ASC;Result:
| Name | Age |
|---|---|
| Amit | 18 |
| Rahul | 20 |
| Priya | 22 |
ASC is optional because ascending order is the default behavior.
Descending Order (DESC)
The DESC keyword sorts data in descending order.
Example:
SELECT *
FROM Students
ORDER BY Age DESC;Result:
| Name | Age |
|---|---|
| Priya | 22 |
| Rahul | 20 |
| Amit | 18 |
Descending means:
- Largest to smallest
- Z to A
- Newest date to oldest date
Sorting Text Values
Example:
SELECT *
FROM Students
ORDER BY Name;Result:
Amit
Priya
RahulText values are sorted alphabetically.
Sorting Numeric Values
Example:
SELECT *
FROM Products
ORDER BY Price;Result:
500
1000
2500
5000Numbers are sorted from smallest to largest.
Sorting Dates
Example:
SELECT *
FROM Orders
ORDER BY OrderDate;Result:
2025-01-01
2025-01-05
2025-01-10Older dates appear first.
To display newest dates first:
SELECT *
FROM Orders
ORDER BY OrderDate DESC;Sorting by Multiple Columns
SQL allows sorting using multiple columns.
Example:
SELECT *
FROM Students
ORDER BY City, Name;Sorting process:
- Sort by City.
- Within each city, sort by Name.
Result:
| City | Name |
|---|---|
| Delhi | Amit |
| Delhi | Rahul |
| Mumbai | Priya |
Multiple Column Sorting with DESC
Example:
SELECT *
FROM Employees
ORDER BY Department ASC,
Salary DESC;Explanation:
- Departments sorted alphabetically.
- Employees within each department sorted by salary.
Real-World Example
Consider an e-commerce website.
Products table:
| ProductID | ProductName | Price |
|---|---|---|
| 101 | Laptop | 60000 |
| 102 | Mouse | 500 |
| 103 | Mobile | 25000 |
Sort by price:
SELECT *
FROM Products
ORDER BY Price;Result:
| ProductName | Price |
|---|---|
| Mouse | 500 |
| Mobile | 25000 |
| Laptop | 60000 |
Customers can easily identify the cheapest products.
ORDER BY with WHERE
ORDER BY is often combined with WHERE.
Example:
SELECT *
FROM Products
WHERE Price > 1000
ORDER BY Price DESC;Process:
- Filter products.
- Sort remaining records.
This combination is very common in real applications.
ORDER BY with Aliases
Example:
SELECT
Name AS StudentName
FROM Students
ORDER BY StudentName;Result:
Records are sorted using the alias.
ORDER BY Using Column Position
SQL also allows sorting by column position.
Example:
SELECT Name, Age
FROM Students
ORDER BY 2;Meaning:
Sort by second column (Age)Although valid, using column names is usually clearer.
Common Errors
Sorting by a Non-Existent Column
Wrong:
SELECT *
FROM Students
ORDER BY Salary;If Salary does not exist:
Unknown Column ErrorMisspelling ASC or DESC
Wrong:
ORDER BY Age DESCorrect:
ORDER BY Age DESCIncorrect Multiple Column Syntax
Wrong:
ORDER BY Age NameCorrect:
ORDER BY Age, NameAssuming Database Returns Sorted Data
Without ORDER BY:
SELECT *
FROM Students;No guaranteed order exists.
Always use ORDER BY when sorting is required.
Best Practices
Always Specify ORDER BY When Needed
Never assume records will appear in a specific order.
Use Indexed Columns
Sorting indexed columns can improve performance.
Sort Only When Necessary
Large sorting operations can increase query execution time.
Use Meaningful Sorting Logic
Choose sorting columns based on business requirements.
Prefer Column Names Over Positions
Good:
ORDER BY AgeLess clear:
ORDER BY 2Common Interview Questions
What is the purpose of ORDER BY?
ORDER BY sorts query results based on one or more columns.
What is the default sorting order?
Ascending (ASC).
How do you sort records in descending order?
ORDER BY ColumnName DESC;Can ORDER BY sort multiple columns?
Yes.
Example:
ORDER BY City, Name;Summary
The ORDER BY clause is used to arrange query results in a meaningful order. It improves readability, reporting, and user experience by sorting records alphabetically, numerically, or chronologically.
In this lesson, you learned:
- What ORDER BY is
- Why sorting is important
- Ascending order
- Descending order
- Sorting text values
- Sorting numeric values
- Sorting dates
- Multiple-column sorting
- Common mistakes
- Best practices
Understanding ORDER BY is essential because properly organized data makes database applications easier to use and analyze.
Next Step
Continue to the next lesson:
GROUP BY →
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for ORDER BY Clause.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this SQL topic.
Search Terms
sql, sql complete guide, sql tutorial, sql notes, complete, guide, filtering, order
Related SQL Topics