Beginner

20. ORDER BY

Learn how to sort SQL query results in ascending or descending order.

📘 Lesson 20 of 728 ⏱ 8 min 🎯 Beginner

The ORDER BY clause is used to sort the rows returned by a SQL query based on one or more columns. ASC is used for ascending order and DESC is used for descending order. If no sorting direction is specified, ascending order is generally used. ORDER BY can be applied to numbers, text, dates, and calculated values.

Sorting makes query results easier to understand and is especially useful when identifying the highest salary, lowest sales, latest transaction, oldest customer, or top-performing product.

A manager wants to see employees from the highest salary to the lowest salary. ORDER BY can sort the Salary column in descending order.

SQL
SELECT
    EmployeeName,
    Salary
FROM Employees
ORDER BY Salary DESC;

ORDER BY is widely used in financial reports, employee reports, sales dashboards, customer analysis, transaction analysis, and ranking-related queries.

  • ORDER BY sorts query results.
  • ASC means ascending order.
  • DESC means descending order.
  • ORDER BY can use multiple columns.
  • ORDER BY does not change stored table data.
  • ORDER BY is useful for ranking and reporting.
What is ORDER BY used for?

ORDER BY is used to sort query results based on one or more columns.

What is the difference between ASC and DESC?

ASC sorts values in ascending order, while DESC sorts values in descending order.

Have you finished this lesson?

Introduction to SQL – All Lessons