SQL ORDER BY Clause | ASC & DESC Sorting | Complete Notes | CBSE Class 12 Informatics Practices (2026–27)
Class 12 · Informatics Practices
SQL ORDER BY Clause
The ORDER BY clause is used to arrange the records returned by an SQL query in a specific order. By default, SQL sorts the data in ascending (ASC) order, but it can also sort data in descending (DESC) order.
Sorting data makes reports easier to read and helps users quickly identify the highest, lowest, earliest, or latest values.
Why Use ORDER BY?
- Arrange records in ascending or descending order.
- Create well-organized reports.
- Find highest and lowest values easily.
- Improve readability of query results.
Sample Table : Student
| RollNo | Name | City | Marks |
|---|---|---|---|
| 101 | Amit | Jaipur | 85 |
| 102 | Neha | Delhi | 92 |
| 103 | Rohan | Jaipur | 78 |
| 104 | Priya | Mumbai | 88 |
| 105 | Karan | Delhi | 81 |
| 106 | Meera | Jaipur | 91 |
Syntax of ORDER BY
SELECT column_list FROM table_name ORDER BY column_name;
Ascending Order (ASC)
The ASC keyword arranges data from the smallest to the largest value. It is the default sorting order.
Syntax
SELECT * FROM Student ORDER BY Marks ASC;
If ASC is omitted, SQL automatically sorts the records in ascending order.
Example 1: Sort by Marks (Ascending)
SELECT * FROM Student ORDER BY Marks;
Records are displayed from the lowest marks to the highest marks.
Descending Order (DESC)
The DESC keyword arranges data from the largest to the smallest value.
Syntax
SELECT * FROM Student ORDER BY Marks DESC;
Records are displayed from the highest marks to the lowest marks.
Example 2: Sort by Student Name
SELECT * FROM Student ORDER BY Name;
Records are sorted alphabetically from A to Z.
Example 3: Sort by Name in Descending Order
SELECT * FROM Student ORDER BY Name DESC;
Records are sorted alphabetically from Z to A.
Sorting Using Multiple Columns
More than one column can be used for sorting.
Syntax
SELECT * FROM Student ORDER BY City, Marks DESC;
The records are first sorted by City. Within each city, they are sorted by Marks in descending order.
ORDER BY with WHERE Clause
SELECT * FROM Student WHERE Marks >= 80 ORDER BY Marks DESC;
The query first filters students scoring 80 or above and then sorts them from highest to lowest marks.
ORDER BY with GROUP BY
SELECT City,
AVG(Marks) AS AverageMarks
FROM Student
GROUP BY City
ORDER BY AverageMarks DESC;
The query groups students by city, calculates the average marks for each city, and then displays the cities in descending order of average marks.
Execution Order of a Query
FROM ↓ WHERE ↓ GROUP BY ↓ HAVING ↓ SELECT ↓ ORDER BY
The ORDER BY clause is applied after the result set has been prepared.
Sorting Numeric and Text Data
| Column Type | Ascending | Descending |
|---|---|---|
| Numbers | Smallest → Largest | Largest → Smallest |
| Text | A → Z | Z → A |
| Dates | Oldest → Newest | Newest → Oldest |
Real-Life Applications
| Situation | Sorting |
|---|---|
| Merit List | Marks DESC |
| Employee Directory | Name ASC |
| Latest Transactions | Date DESC |
| Lowest Prices | Price ASC |
| City-wise Reports | City ASC |
Common Errors
| Error | Reason |
|---|---|
| Unknown Column | Incorrect column name. |
| Syntax Error | Incorrect placement of ORDER BY. |
| Using ORDER BY before WHERE | Incorrect SQL statement order. |
Quick Revision
| Keyword | Purpose |
|---|---|
| ORDER BY | Sort records |
| ASC | Ascending order (default) |
| DESC | Descending order |
| Multiple Columns | Sort by more than one column |
CBSE Exam Tips
ASCis the default sorting order.- Use
DESCto display records from highest to lowest or Z to A. - Remember that
ORDER BYis written at the end of the SQL query. - Practice sorting numeric, text, and date columns.
- Understand how
ORDER BYworks withWHEREandGROUP BY.
Summary
The ORDER BY clause is used to arrange query results in ascending or descending order. It supports sorting by one or more columns and works effectively with clauses such as WHERE and GROUP BY. Proper use of ORDER BY helps present data in an organized and meaningful way, making it an essential concept for both the CBSE Class 12 Informatics Practices examination and real-world database applications.