Preamble
SQL Server operator ORDER BY is used to sort records in the SELECT query result set.
Syntax for ORDER BY statement in SQL
Sorting by one field:
SELECT * FROM tab n WHERE condition ORDER BY F1
You can sort by many fields, not one at a time:
SELECT * FROM tab n WHERE condition ORDER BY F1, F2...
By default, records are sorted in ascending order to be sorted in descending order – set DESC:
SELECT * FROM tab n WHERE condition ORDER BY F DESC
By default the sorting will be as if ASC was supplied:
SELECT * FROM tab n WHERE condition ORDER BY F ASC
A condition where it is not necessary – if it is not set, all records will be selected:
SELECT * FROM tab n ORDER BY F
Overall view:
SELECT list
FROM tab
[WHERE conditions]
ORDER BY list [ ASC | DESC ];
where:
- list – Columns or calculations that you want to get
- tab – The tables from which you want the records. The FROM sentence must contain at least one table
- WHERE conditions – optional. Conditions to be met for entries to be selected
- ASC – optional. ASC sorts the result set in ascending order. Default behavior if not specified.
- DESC – optional. DESC sorts the result set in descending order.
Note:
When ASC or DESC is not used in the ORDER BY, the results will be ordered in ascending order. This is same as ORDER BY ASC.
Sorting results in ascending order
To sort the results in ascending order, you can specify the ASC attribute. If no value (ASC or DESC) is specified after the field in the ORDER BY sentence, the sort order will be in ascending order by default. Let’s look at this further.
In this example, we have a table with the following data:
| cust_id | f_name | l_name | fav_website |
| 4000 | Justin | Bieber | google.com |
| 5000 | Selena | Gomez | bing.com |
| 6000 | Mila | Kunis | yahoo.com |
| 7000 | Tom | Cruise | oracle.com |
| 8000 | Johnny | Depp | NULL |
| 9000 | Russell | Crowe | google.com |
Enter the following SQL statement:
SELECT *
FROM cust
ORDER BY l_name;
Six records will be selected. Here are the results that you should get.
| cust_id | f_name | l_name | fav_website |
| 4000 | Justin | Bieber | google.com |
| 9000 | Russell | Crowe | google.com |
| 7000 | Tom | Cruise | oracle.com |
| 8000 | Johnny | Depp | NULL |
| 5000 | Selena | Gomez | bing.com |
| 6000 | Mila | Kunis | yahoo.com |
In this example, all records from the customer table will be returned, sorted by l_name field in ascending order, and will be equivalent to the next ORDER BY SQL sentence.
SELECT *
FROM cust
ORDER BY l_name ASC;
Most programmers skip the ASC attribute when sorting in ascending order.
Sorting results in descending order
The DESC attribute in the ORDER BY clause is used to sort a set of results in descending order. Let’s look more closely.
In this illustration, we have a table with the information below:
| sup_id | sup_name | city_name | state_name |
| 100 | Yandex | Moscow | Russian |
| 200 | Lansing | Michigan | |
| 300 | Oracle | Redwood City | California |
| 400 | Bing | Redmond | Washington |
| 500 | Yahoo | Sunnyvale | Washington |
| 600 | DuckDuckGo | Paoli | Pennsylvania |
| 700 | Qwant | Paris | France |
| 800 | Menlo Park | California | |
| 900 | Electronic Arts | San Francisco | California |
Type the following SQL query:
SELECT *
FROM sup
WHERE sup_id > 40
ORDER BY sup_id DESC;
There will be five records chosen. These are the outcomes you ought to obtain.
| sup_id | sup_name | city_name | state_name |
| 900 | Electronic Arts | San Francisco | California |
| 800 | Menlo Park | California | |
| 700 | Qwant | Paris | France |
| 600 | DuckDuckGo | Paoli | Pennsylvania |
| 500 | Yahoo | Sunnyvale | Washington |
In this case, the field sup_id will be used to sort the results in descending order.
Sorting results by relative position
You can also use the ORDER BY SQL statement to sort by relative position in the result set if the first field in the result set is f1, the second is f2, the third is f3, etc.
In this illustration, a table of products contains the information below:
| prod_id | prod_name | cat_id |
| 1 | Pear | 50 |
| 2 | Banana | 50 |
| 3 | Orange | 50 |
| 4 | Apple | 50 |
| 5 | Bread | 75 |
| 6 | Sliced Ham | 25 |
| 7 | Kleenex | NULL |
Enter the subsequent SQL query now:
SELECT prod_id,
prod_name
FROM prod
WHERE prod_name <> 'Bread'
ORDER BY 1 DESC;
There will be six records chosen. These are the outcomes you ought to obtain.
| prod_id | prod_name |
| 7 | Kleenex |
| 6 | Sliced Ham |
| 4 | Apple |
| 3 | Orange |
| 2 | Banana |
| 1 | Pear |
Since the prod_id field is at position #1 in the result set and will be equivalent to the following SQL statement ORDER BY, the results are sorted in this example by the prod_id field in descending order.
SELECT prod_id,
prod_name
FROM prod
WHERE prod_name <> 'Bread'
ORDER BY prod_id DESC;
Using ASC and DESC Attributes
You can use both the ASC and DESC attributes in a single SELECT statement when using the ORDER BY SQL clause to sort a result set.
Let’s use the same product table from the previous example for this one.
| prod_id | prod_name | cat_id |
| 1 | Pear | 50 |
| 2 | Banana | 50 |
| 3 | Orange | 50 |
| 4 | Apple | 50 |
| 5 | Bread | 75 |
| 6 | Sliced Ham | 25 |
| 7 | Kleenex | NULL |
Enter the subsequent SQL query now:
SELECT *
FROM prod
WHERE prod_id <>
ORDER BY cat_id DESC,
prod_name ASC;
There will be six records chosen. These are the outcomes you ought to obtain.
| prod_id | prod_name | cat_id |
| 5 | Bread | 75 |
| 4 | Apple | 50 |
| 2 | Banana | 50 |
| 3 | Orange | 50 |
| 1 | Pear | 50 |
| 6 | Sliced Ham | 25 |
In this example, the records are sorted first by the cat_id field in descending order, and then, for secondary sorting, by the prod_name field in ascending order.
Examples:
All examples will be listed in this table, unless otherwise stated:
| id | name | age | salary |
| 1 | Justin | 23 | 400 |
| 2 | Selena | 25 | 500 |
| 3 | Mila | 23 | 500 |
| 4 | Tom | 30 | 1000 |
| 5 | Johnny | 27 | 500 |
| 6 | Russell | 28 | 1000 |
Example №1
Let’s pull up the entire table of records and arrange them by age:
SELECT * FROM workers_1 WHERE id>0 ORDER BY age
The strings will be chosen by the SQL query in the following order:
| f_id | f_name | f_age | f_salary |
| 1 | Justin | 23 | 400 |
| 3 | Mila | 23 | 500 |
| 2 | Selena | 25 | 500 |
| 5 | Johnny | 27 | 500 |
| 6 | Russell | 28 | 1000 |
| 4 | Tom | 30 | 1000 |
All records have been chosen, so the WHERE block may not be necessary:
SELECT * FROM workers_1 ORDER BY f_age
The outcome won’t change if you explicitly specify the sort type, which is ASC:
SELECT * FROM workers_1 ORDER BY f_age ASC
Example №2
The records will now be sorted by decreasing age:
SELECT * FROM workers_1 ORDER BY f_age DESC
The strings will be chosen by the SQL query in the following order:
| f_id | f_name | f_age | f_sal |
| 4 | Tom | 30 | 1000 |
| 6 | Russell | 28 | 1000 |
| 5 | Johnny | 27 | 500 |
| 2 | Selena | 25 | 500 |
| 1 | Justin | 23 | 400 |
| 3 | Mila | 23 | 500 |
Example №3
Now let’s simultaneously sort the records by age and wage.
Records that have the same age (in this case, 23) will be found in the descending wages after the records are first sorted by age:
SELECT * FROM workers_1 WHERE id>0 ORDER BY f_age ASC, f_sal DESC
The strings will be chosen by the SQL query in the following order:
| f_id | f_name | f_age | f_sal |
| 3 | Mila | 23 | 500 |
| 1 | Justin | 23 | 400 |
| 2 | Selena | 25 | 500 |
| 5 | Johnny | 27 | 500 |
| 6 | Russell | 28 | 1000 |
| 4 | Tom | 30 | 1000 |
Example №4
Let’s sort by salary increase under the same conditions (i.e., first sorting by f_age).
The first and second entries will now be switched so that the f_sal is lower initially and then higher later:
SELECT * FROM workers_1 WHERE f_id>0 ORDER BY f_age ASC, f_sa DESC
The strings in the following order will be chosen by the SQL query:
| f_id | f_name | f_age | f_sal |
| 1 | Justin | 23 | 400 |
| 3 | Mila | 23 | 500 |
| 2 | Selena | 25 | 500 |
| 5 | Johnny | 27 | 500 |
| 6 | Russell | 28 | 1000 |
| 4 | Tom | 30 | 1000 |
Sql Training Online Sql Order By Sorting
About Enteros
Enteros offers a patented database performance management SaaS platform. It finds the root causes of complex database scalability and performance problems that affect business across a growing number of cloud, RDBMS, NoSQL, and machine learning database platforms.
The views expressed on this blog are those of the author and do not necessarily reflect the opinions of Enteros Inc. This blog may contain links to the content of third-party sites. By providing such links, Enteros Inc. does not adopt, guarantee, approve, or endorse the information, views, or products available on such sites.
Are you interested in writing for Enteros’ Blog? Please send us a pitch!
RELATED POSTS
How to Accelerate Financial Sector Growth with Enteros Database Software, Cost Attribution, and AI-Driven Root Cause Analysis
- 7 May 2026
- Database Performance Management
Introduction The financial sector is evolving rapidly as digital banking, fintech innovation, AI-powered services, and real-time transactions reshape the industry. Financial institutions are expected to provide seamless customer experiences, maintain high system reliability, and comply with strict regulatory standards—all while managing operational efficiency and controlling costs. However, as financial systems become increasingly data-intensive and distributed, … Continue reading “How to Accelerate Financial Sector Growth with Enteros Database Software, Cost Attribution, and AI-Driven Root Cause Analysis”
How to Drive Technology Sector Growth with Enteros Database Management Platform, Cost Estimation, AI SQL, and Generative AI
Introduction The technology sector is evolving at an unprecedented pace. From cloud-native applications and AI-powered platforms to real-time analytics and digital transformation initiatives, technology companies are under constant pressure to innovate, scale, and deliver exceptional user experiences. However, rapid growth also introduces significant operational challenges. Organizations must manage massive volumes of data, optimize infrastructure performance, … Continue reading “How to Drive Technology Sector Growth with Enteros Database Management Platform, Cost Estimation, AI SQL, and Generative AI”
Improving Database Performance and Reliability in Healthcare Systems with Advanced Analytics
Healthcare systems today are rapidly evolving as hospitals, clinics, and health-tech platforms increasingly rely on digital infrastructure. From electronic health records to telemedicine platforms, nearly every healthcare service depends on fast, secure, and reliable data access. At the center of this digital transformation lies the database. Healthcare organizations manage massive volumes of sensitive patient data, … Continue reading “Improving Database Performance and Reliability in Healthcare Systems with Advanced Analytics”
How Intelligent Database Analytics is Transforming Performance in BFSI Platforms
Introduction The Banking, Financial Services, and Insurance (BFSI) industry is undergoing a massive digital transformation. From mobile banking apps and real-time payment systems to AI-driven fraud detection and personalized financial services, modern financial platforms depend heavily on high-performance data infrastructure. At the center of this infrastructure lies the database layer, which processes millions of transactions, … Continue reading “How Intelligent Database Analytics is Transforming Performance in BFSI Platforms”