The ORDER BY clause is used to sort data retrieved from a database table. By default, SQL does not guarantee the order of rows returned in a query. Using ORDER BY, you can arrange records in ascending (ASC) or descending (DESC) order.
Sorting data is extremely useful when creating reports, dashboards, leaderboards, invoices, and search results.
The ORDER BY clause can sort:
- Numbers
- Text
- Dates
- Multiple columns at the same time
It is commonly used together with SELECT and WHERE statements to retrieve organized and meaningful results. SQL ORDER BY Clause Practice questions with solutions help to understand the concepts.
What is the SQL ORDER BY Clause?
The ORDER BY clause sorts the result set based on one or more columns.
Basic Syntax
SELECT column_name
FROM table_name
ORDER BY column_name;
Ascending Order (Default)
SELECT *
FROM students
ORDER BY marks ASC;
Descending Order
SELECT *
FROM students
ORDER BY marks DESC;
Sample Table Used Throughout This Chapter
students
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 102 | Amit | 22 | Java | Noida | 91 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
Why Use ORDER BY?
The ORDER BY clause helps you:
- Display highest marks first
- Arrange names alphabetically
- Sort employees by salary
- Display newest records first
- Generate clean business reports
- Improve readability of query results
Sorting Keywords
| Keyword | Meaning |
|---|---|
| ASC | Ascending Order (Default) |
| DESC | Descending Order |
1. SQL Query to Sort Students by Marks (Ascending)
Problem Statement
Write an SQL query to display all students sorted by marks in ascending order.
SQL Solution
SELECT *
FROM students
ORDER BY marks ASC;
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
| 102 | Amit | 22 | Java | Noida | 91 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
Explanation
The ASC keyword sorts records from smallest to largest.
For numeric values:
84 → 88 → 90 → 91 → 95
If ASC is omitted, SQL sorts in ascending order by default.
Concepts Covered
- ORDER BY
- ASC
- Numeric Sorting
2. SQL Query to Sort Students by Marks (Descending)
Problem Statement
Write an SQL query to display students sorted by marks in descending order.
SQL Solution
SELECT *
FROM students
ORDER BY marks DESC;
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 102 | Amit | 22 | Java | Noida | 91 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
Explanation
The DESC keyword sorts records from largest to smallest.
This is commonly used for:
- Highest Marks
- Highest Salary
- Top Sales
- Best Performing Students
Concepts Covered
- ORDER BY
- DESC
- Descending Sorting
3. SQL Query to Sort Student Names Alphabetically
Problem Statement
Write an SQL query to display students sorted by name in alphabetical order.
SQL Solution
SELECT *
FROM students
ORDER BY name ASC;
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 102 | Amit | 22 | Java | Noida | 91 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
Explanation
The ORDER BY clause can also sort text values alphabetically.
The sorting order is:
A → Z
This is useful for:
- Customer Lists
- Student Lists
- Product Catalogs
- Employee Records
Concepts Covered
- ORDER BY
- Alphabetical Sorting
- ASC
- Text Sorting
4. SQL Query to Sort Student Names in Descending Order
Problem Statement
Write an SQL query to display all students sorted by name in descending (Z to A) order.
Sample Table
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 102 | Amit | 22 | Java | Noida | 91 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
SQL Solution
SELECT *
FROM students
ORDER BY name DESC;
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 105 | Rohit | 21 | Java | Faridabad | 90 |
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 102 | Amit | 22 | Java | Noida | 91 |
Explanation
The DESC keyword sorts text values in reverse alphabetical order.
Sorting order:
Z → A
This type of sorting is commonly used when users want to view records starting from the last alphabetically.
Concepts Covered
- ORDER BY
- DESC
- Alphabetical Sorting
- Text Data
5. SQL Query to Sort Students by Age
Problem Statement
Write an SQL query to display students sorted by age in ascending order.
Sample Table
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 102 | Amit | 22 | Java | Noida | 91 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
SQL Solution
SELECT *
FROM students
ORDER BY age ASC;
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
| 102 | Amit | 22 | Java | Noida | 91 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
Explanation
The query sorts records by the age column from the youngest to the oldest.
Notice that:
- Students with the same age (Rahul and Rohit) appear together.
- SQL does not guarantee the order of rows with identical values unless another sorting column is specified.
Concepts Covered
- ORDER BY
- ASC
- Numeric Sorting
- Integer Data
6. SQL Query to Sort Students by Age (Descending)
Problem Statement
Write an SQL query to display students sorted by age from highest to lowest.
Sample Table
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 102 | Amit | 22 | Java | Noida | 91 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
SQL Solution
SELECT *
FROM students
ORDER BY age DESC;
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 102 | Amit | 22 | Java | Noida | 91 |
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
Explanation
The DESC keyword arranges numeric values from largest to smallest.
This type of sorting is commonly used to:
- Display senior employees first
- Show highest experience first
- Arrange ages from oldest to youngest
Concepts Covered
- ORDER BY
- DESC
- Numeric Sorting
7. SQL Query to Sort Students by City
Problem Statement
Write an SQL query to display students sorted by city name in alphabetical order.
SQL Solution
SELECT *
FROM students
ORDER BY city ASC;
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 102 | Amit | 22 | Java | Noida | 91 |
Explanation
The query sorts the city column alphabetically.
Records with the same city appear together.
This type of sorting is useful for:
- Branch-wise reports
- City-wise customer lists
- Location-based analysis
Concepts Covered
- ORDER BY
- ASC
- Text Sorting
8. SQL Query to Sort Students by Course
Problem Statement
Write an SQL query to display students sorted by course name alphabetically.
SQL Solution
SELECT *
FROM students
ORDER BY course ASC;
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 102 | Amit | 22 | Java | Noida | 91 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
Explanation
The ORDER BY clause sorts text values alphabetically.
The order becomes:
Java
Python
SQL
Concepts Covered
- ORDER BY
- Text Sorting
- ASC
9. SQL Query to Sort Students by ID
Problem Statement
Write an SQL query to display students sorted by Student ID.
SQL Solution
SELECT *
FROM students
ORDER BY id ASC;
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 102 | Amit | 22 | Java | Noida | 91 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
Explanation
Sorting by ID is commonly used because IDs are usually primary keys.
It ensures records appear in their insertion or logical order.
Concepts Covered
- ORDER BY
- Primary Key Sorting
- Numeric Data
10. SQL Query to Sort Students by Multiple Columns
Problem Statement
Write an SQL query to sort students first by course, then by marks in descending order.
SQL Solution
SELECT *
FROM students
ORDER BY course ASC,
marks DESC;
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 102 | Amit | 22 | Java | Noida | 91 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
Explanation
SQL sorts the first column (course) first.
If two or more records have the same course, SQL sorts them using the second column (marks DESC).
For example:
Java
- Amit → 91
- Rohit → 90
Python
- Rahul → 88
- Priya → 84
This technique is widely used in business reporting where multiple sorting levels are required.
Concepts Covered
- ORDER BY
- Multiple Column Sorting
- ASC
- DESC
- SQL Reports
11. SQL Query Using WHERE with ORDER BY
Problem Statement
Write an SQL query to display students who scored more than 85 marks, sorted by marks in descending order.
Sample Table
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 102 | Amit | 22 | Java | Noida | 91 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
SQL Solution
SELECT *
FROM students
WHERE marks > 85
ORDER BY marks DESC;
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 102 | Amit | 22 | Java | Noida | 91 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
| 101 | Rahul | 21 | Python | Delhi | 88 |
Explanation
SQL executes the query in this order:
- Filter rows using the
WHEREclause. - Sort the filtered result using the
ORDER BYclause.
This combination is one of the most common patterns in SQL.
Concepts Covered
- WHERE
- ORDER BY
- DESC
- Query Execution Order
12. SQL Query to Display Top Scoring Students
Problem Statement
Write an SQL query to display students from highest marks to lowest marks.
SQL Solution
SELECT name,
marks
FROM students
ORDER BY marks DESC;
Sample Output
| name | marks |
|---|---|
| Neha | 95 |
| Amit | 91 |
| Rohit | 90 |
| Rahul | 88 |
| Priya | 84 |
Explanation
Sorting marks in descending order helps identify the highest-performing students quickly.
This technique is widely used for:
- Merit Lists
- Leaderboards
- Sales Rankings
- Employee Performance Reports
Concepts Covered
- ORDER BY
- DESC
- Ranking Data
13. SQL Query to Sort Cities in Descending Order
Problem Statement
Write an SQL query to display students sorted by city name in descending alphabetical order.
SQL Solution
SELECT *
FROM students
ORDER BY city DESC;
Sample Output
| id | name | city |
|---|---|---|
| 102 | Amit | Noida |
| 104 | Priya | Gurgaon |
| 105 | Rohit | Faridabad |
| 101 | Rahul | Delhi |
| 103 | Neha | Delhi |
Explanation
The DESC keyword arranges text values in reverse alphabetical order:
Z → A
Concepts Covered
- ORDER BY
- DESC
- Text Sorting
14. SQL Query to Sort by Course and Name
Problem Statement
Write an SQL query to sort students first by course and then by name.
SQL Solution
SELECT *
FROM students
ORDER BY course ASC,
name ASC;
Sample Output
| id | name | course |
|---|---|---|
| 102 | Amit | Java |
| 105 | Rohit | Java |
| 104 | Priya | Python |
| 101 | Rahul | Python |
| 103 | Neha | SQL |
Explanation
SQL first sorts by the course column.
If multiple students belong to the same course, SQL then sorts them alphabetically by name.
This is useful for grouped reports.
Concepts Covered
- Multiple Column Sorting
- ORDER BY
- ASC
15. SQL Query Combining Multiple Filters and Sorting
Problem Statement
Write an SQL query to display students who:
- Belong to Delhi
- Scored more than 85 marks
Sort the result by marks in descending order.
SQL Solution
SELECT *
FROM students
WHERE city = 'Delhi'
AND marks > 85
ORDER BY marks DESC;
Sample Output
| id | name | city | marks |
|---|---|---|---|
| 103 | Neha | Delhi | 95 |
| 101 | Rahul | Delhi | 88 |
Explanation
This query combines:
WHEREfor filteringANDfor multiple conditionsORDER BYfor sorting
This pattern is one of the most frequently used SQL query structures in real-world applications.
Examples include:
- Customers from a specific city sorted by total purchases
- Employees in a department sorted by salary
- Students in a class sorted by marks
Concepts Covered
- WHERE
- AND
- ORDER BY
- DESC
- Multiple Conditions
- SQL Reporting
Chapter Summary
In this chapter, you learned how to use the SQL ORDER BY clause to sort records returned by a query. Since SQL does not guarantee the order of rows by default, the ORDER BY clause is essential for presenting data in a meaningful and organized way.
You explored sorting data in both ascending (ASC) and descending (DESC) order, working with numeric and text columns, sorting by multiple columns, and combining the ORDER BY clause with the WHERE clause.
These techniques are widely used in reporting systems, dashboards, business applications, and data analysis.
Throughout this chapter, you covered:
- Introduction to the
ORDER BYClause - Ascending Order (
ASC) - Descending Order (
DESC) - Sorting Numeric Values
- Sorting Text Values
- Sorting by Multiple Columns
- Combining
WHEREwithORDER BY - Real-world Sorting Examples
Mastering the ORDER BY clause will help you create clean, professional, and easy-to-read SQL query results.
Key Takeaways
- The
ORDER BYclause sorts query results. ASCsorts data from smallest to largest or A to Z.DESCsorts data from largest to smallest or Z to A.ASCis the default sorting order.- You can sort numbers, text, and dates.
- Multiple columns can be used for sorting.
WHEREfilters records beforeORDER BYsorts them.- Sorting improves report readability and user experience.
- The
ORDER BYclause is frequently used in SQL interviews and real-world applications. - Combining filtering and sorting creates powerful SQL queries.
Frequently Asked Questions (FAQs)
1. What is the SQL ORDER BY clause?
The ORDER BY clause is used to sort the rows returned by an SQL query.
Example:
SELECT *
FROM students
ORDER BY marks DESC;
2. What is the default sorting order?
The default sorting order is Ascending (ASC).
Example:
SELECT *
FROM students
ORDER BY marks;
This is equivalent to:
SELECT *
FROM students
ORDER BY marks ASC;
3. What is the difference between ASC and DESC?
ASC
Sorts data from:
- Smallest → Largest
- A → Z
DESC
Sorts data from:
- Largest → Smallest
- Z → A
4. Can I sort multiple columns?
Yes.
Example:
SELECT *
FROM students
ORDER BY course,
marks DESC;
SQL first sorts by course, then by marks within each course.
5. Can ORDER BY be used with WHERE?
Yes.
Example:
SELECT *
FROM students
WHERE marks > 80
ORDER BY marks DESC;
SQL first filters the records and then sorts the filtered result.
6. Can ORDER BY sort text values?
Yes.
Example:
SELECT *
FROM students
ORDER BY name ASC;
This arranges names alphabetically.
7. Does ORDER BY change the original table?
No.
ORDER BY only changes the order of rows displayed in the query result.
The data stored in the database remains unchanged.
8. Where is ORDER BY used in real-world applications?
The ORDER BY clause is used in:
- Student Result Systems
- Employee Payroll Reports
- Banking Applications
- Hospital Management Systems
- E-commerce Product Listings
- Sales Reports
- CRM Software
- Inventory Management
- Business Intelligence Dashboards
- Analytics Reports
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
