How ORDER BY works in SQL? Best ORDER BY examples

How ORDER BY works in SQL? Best ORDER BY examples

Sorting data in SQL queries is an essential skill for anyone dealing with databases. The ORDER BY clause allows us to organize query results in ascending or descending order based on one or multiple columns. Understanding how ORDER BY works can significantly improve data retrieval efficiency and readability.

Understanding the ORDER BY Clause

The ORDER BY clause is used in SQL to sort query results. By default, it orders results in ascending order unless specified otherwise. The syntax is straightforward:

SELECT column1, column2
FROM table_name
ORDER BY column1 [ASC | DESC];

Here’s what each part means:

  • column1: The column by which the result set is sorted.
  • ASC (optional): Sorts the results in ascending order (default).
  • DESC (optional): Sorts the results in descending order.

Sorting by a Single Column

To demonstrate, let’s assume we have a table employees:

id name salary
1 Alice 50000
2 Bob 70000
3 Charlie 60000

If we want to retrieve employees sorted by salary in ascending order, we use:

SELECT * FROM employees ORDER BY salary ASC;

Conversely, to get employees with the highest salary first:

SELECT * FROM employees ORDER BY salary DESC;

Sorting by Multiple Columns

Sometimes, sorting by a single column isn’t enough. Suppose we want to sort employees first by salary (ascending), then by name (ascending) in case of ties:

SELECT * FROM employees ORDER BY salary ASC, name ASC;

This ensures that employees with the same salary appear in alphabetical order.

ORDER BY with NULL Values

Handling NULL values is crucial when sorting. By default, in most databases:

  • In ascending order (ASC), NULL values appear first.
  • In descending order (DESC), NULL values appear last.

Some databases allow explicit control over this:

SELECT * FROM employees ORDER BY salary ASC NULLS LAST;

This ensures that non-null values always come first.

Sorting with Expressions

The ORDER BY clause can also sort results based on computed expressions rather than raw column values. For example:

SELECT name, salary * 12 AS annual_salary
FROM employees
ORDER BY annual_salary DESC;

Here, we’re sorting by a computed column that represents annual salaries.

Using ORDER BY with LIMIT

Often, we only need the top N results. Combining ORDER BY with LIMIT (or TOP in SQL Server) achieves this:

SELECT * FROM employees ORDER BY salary DESC LIMIT 3;

In SQL Server, the equivalent query would be:

SELECT TOP 3 * FROM employees ORDER BY salary DESC;

ORDER BY with CASE Statements

Sorting dynamically based on conditions is possible using CASE statements. For instance, if we want to prioritize employees named “Alice” first, then sort by salary:

SELECT * FROM employees 
ORDER BY 
    CASE WHEN name = 'Alice' THEN 1 ELSE 2 END, 
    salary DESC;

This ensures that Alice appears first before ordering by salary.

Conclusion

The ORDER BY clause is a powerful SQL feature that allows control over how query results are presented. Whether sorting by single or multiple columns, handling NULL values, or using expressions, understanding these techniques ensures better data organization and retrieval efficiency.

 

Other interesting article:

How GROUP BY works in SQL? Best GROUP BY examples