Beginner

164. ORDER BY Expression

Learn how calculated expressions can be used to sort query results.

📘 Lesson 164 of 728 ⏱ 8 min 🎯 Intermediate

ORDER BY can sort results using expressions or calculated values, allowing records to be ordered based on derived information.

Sometimes the required sorting value does not exist as a stored column and must be calculated from existing data.

Employees can be sorted by annual salary calculated from monthly salary.

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

Expression-based sorting is useful for financial analysis, calculated metrics, ratios, rankings, and business reports.

  • ORDER BY can use expressions.
  • Calculated values can determine sorting.
  • Expressions can combine multiple columns.
  • Aliases may also be used depending on the database.
  • Expression sorting can support business metrics.
Can ORDER BY use a calculation?

Yes. ORDER BY can sort results using expressions or calculated values.

Why use an expression in ORDER BY?

It allows results to be sorted using a value derived from existing columns.

Have you finished this lesson?

Introduction to SQL – All Lessons