SQL ORDER BY clause
SQL

SQL ORDER BY clause — How to Sort Query Results

CHAPTER 7 of 30 SQL Tutorial — Free Course on Neotech Navigators
By default, SQL returns rows in no guaranteed order. The SQL ORDER BY clause lets you sort results by one or more columns in ascending or descending order. In this chapter, you will learn how to sort text, numbers, and how to sort by multiple columns at once.

The SQL ORDER BY Clause

The ORDER BY clause sorts the result set based on one or more columns. It is always placed at the end of a SELECT statement (before LIMIT if used). You learned the correct clause order in Chapter 2: SQL Syntax.

SQL — ORDER BY Syntax
SELECT column1, column2
FROM table_name
ORDER BY column1 ASC|DESC;
  • ASC — Ascending order (A→Z, 1→100). This is the default.
  • DESC — Descending order (Z→A, 100→1).

We will use this Employees table:

TABLE: Employees
+----+-----------+------------+--------+-----------+
| ID | Name      | Department | Salary | City      |
+----+-----------+------------+--------+-----------+
| 1  | Priya K.  | Sales      | 55000  | Mumbai    |
| 2  | John D.   | Marketing  | 62000  | London    |
| 3  | Arun M.   | IT         | 78000  | Delhi     |
| 4  | Emma W.   | Sales      | 51000  | Sydney    |
| 5  | Ravi S.   | IT         | 72000  | Mumbai    |
| 6  | Sara L.   | HR         | 58000  | New York  |
+----+-----------+------------+--------+-----------+

Sort in Ascending Order (ASC)

Ascending is the default. Both queries below produce the same result:

SQL
SELECT Name, Salary
FROM Employees
ORDER BY Salary ASC;
✓ OUTPUT
+-----------+--------+
| Name      | Salary |
+-----------+--------+
| Emma W.   | 51000  |
| Priya K.  | 55000  |
| Sara L.   | 58000  |
| John D.   | 62000  |
| Ravi S.   | 72000  |
| Arun M.   | 78000  |
+-----------+--------+

Salaries are sorted from lowest to highest. Since ASC is the default, you can omit it: ORDER BY Salary gives the same result.

Sort in Descending Order (DESC)

SQL
SELECT Name, Salary
FROM Employees
ORDER BY Salary DESC;
✓ OUTPUT
+-----------+--------+
| Name      | Salary |
+-----------+--------+
| Arun M.   | 78000  |
| Ravi S.   | 72000  |
| John D.   | 62000  |
| Sara L.   | 58000  |
| Priya K.  | 55000  |
| Emma W.   | 51000  |
+-----------+--------+

Now the highest salary appears first. DESC must be written explicitly — it is never the default.

Sort by Text Column

ORDER BY works on text columns too. It sorts alphabetically:

SQL
SELECT Name, City
FROM Employees
ORDER BY City;
✓ OUTPUT
+-----------+-----------+
| Name      | City      |
+-----------+-----------+
| Arun M.   | Delhi     |
| John D.   | London    |
| Priya K.  | Mumbai    |
| Ravi S.   | Mumbai    |
| Sara L.   | New York  |
| Emma W.   | Sydney    |
+-----------+-----------+

ORDER BY Multiple Columns

You can sort by multiple columns. SQL sorts by the first column, then uses the second column to break ties:

SQL
SELECT Name, Department, Salary
FROM Employees
ORDER BY Department ASC, Salary DESC;
✓ OUTPUT
+-----------+------------+--------+
| Name      | Department | Salary |
+-----------+------------+--------+
| Sara L.   | HR         | 58000  |
| Arun M.   | IT         | 78000  |
| Ravi S.   | IT         | 72000  |
| John D.   | Marketing  | 62000  |
| Priya K.  | Sales      | 55000  |
| Emma W.   | Sales      | 51000  |
+-----------+------------+--------+

Departments are sorted A→Z. Within each department, salaries are sorted highest first. Notice IT shows Arun (78K) before Ravi (72K), and Sales shows Priya (55K) before Emma (51K).

💡 Key Point

Each column in ORDER BY can have its own sort direction. ORDER BY Department ASC, Salary DESC sorts departments alphabetically but salaries highest-first within each department. You can mix ASC and DESC freely.

ORDER BY with WHERE

You can combine ORDER BY with WHERE to filter AND sort in the same query. WHERE always comes before ORDER BY. You can also add AND, OR, NOT operators for multiple conditions:

SQL
SELECT Name, Salary
FROM Employees
WHERE Salary > 55000
ORDER BY Salary DESC;
✓ OUTPUT
+-----------+--------+
| Name      | Salary |
+-----------+--------+
| Arun M.   | 78000  |
| Ravi S.   | 72000  |
| John D.   | 62000  |
| Sara L.   | 58000  |
+-----------+--------+

ORDER BY Column Position

Instead of column names, you can use the column number in the SELECT list. Column 1 is the first selected column, column 2 is the second, and so on:

SQL
SELECT Name, Salary
FROM Employees
ORDER BY 2 DESC;

ORDER BY 2 means “sort by the second column in the SELECT list” which is Salary. This produces the same result as ORDER BY Salary DESC.

📌 Best Practice

Always use column names instead of numbers in ORDER BY. Column numbers break if someone adds or removes columns from the SELECT list. Names are self-documenting and safer for production queries.

To limit the number of sorted rows returned, you can combine ORDER BY with SQL TOP / LIMIT / FETCH in Chapter 12. For a complete reference of the ORDER BY syntax, see the official MySQL ORDER BY documentation.

If you work with data regularly, explore our ready-made Dashboard Templates on NextGenTemplates that use SQL-powered data for business reporting.

📺 Visit our YouTube channel @NeoTechNavigators for step-by-step video tutorials and dashboard demos on this topic.

🧪 Try It Yourself

Write a query that shows employees sorted by City alphabetically, and within each city, by Name alphabetically.

ANSWER
SELECT Name, City, Department
FROM Employees
ORDER BY City ASC, Name ASC;

Expected Output:

+-----------+-----------+------------+
| Name      | City      | Department |
+-----------+-----------+------------+
| Arun M.   | Delhi     | IT         |
| John D.   | London    | Marketing  |
| Priya K.  | Mumbai    | Sales      |
| Ravi S.   | Mumbai    | IT         |
| Sara L.   | New York  | HR         |
| Emma W.   | Sydney    | Sales      |
+-----------+-----------+------------+

Notice Mumbai has two employees — Priya comes before Ravi because the second sort column (Name) puts them in alphabetical order.

📝 What You Learned in This Chapter

  • ORDER BY sorts query results by one or more columns
  • ASC = ascending (default) — smallest to largest, A to Z
  • DESC = descending — largest to smallest, Z to A
  • You can sort by multiple columns — the second column breaks ties in the first
  • Each column can have its own ASC or DESC direction
  • ORDER BY always comes after WHERE (if WHERE is used)
  • Use column names instead of numbers for clarity

Frequently Asked Questions

What is the default sort order in SQL ORDER BY?
The default sort order is ASC (ascending). If you write ORDER BY Salary without specifying ASC or DESC, SQL sorts from smallest to largest for numbers and A to Z for text. You only need to write DESC explicitly when you want reverse order.
Can I sort by a column not in the SELECT list?
Yes. You can write SELECT Name FROM Employees ORDER BY Salary even though Salary is not in the SELECT list. The database uses the column for sorting but does not display it in the output. This works in most database systems.
How does ORDER BY handle NULL values?
It depends on the database. In MySQL and SQL Server, NULL values appear first in ascending order. In PostgreSQL and Oracle, NULL values appear last in ascending order. You can use NULLS FIRST or NULLS LAST in PostgreSQL to control this behavior explicitly. Learn more about NULLs in Chapter 9: SQL NULL Values.
Does ORDER BY slow down my query?
Sorting requires additional processing, so ORDER BY can slow down queries on very large tables. If you sort by an indexed column, the database can use the index to speed up sorting. For small to medium datasets, the performance impact is negligible.
Can I use ORDER BY with GROUP BY?
Yes. When using both, GROUP BY must come before ORDER BY. For example: SELECT Department, COUNT(*) AS Total FROM Employees GROUP BY Department ORDER BY Total DESC. This groups employees by department, then sorts departments by the count from highest to lowest.

📖 Chapter 7 of 30 — SQL Tutorial on Neotech Navigators

PK
Meet PK, the founder of NeotechNavigators.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your data analysis skills to the next level!
https://neotechnavigators.com