MySQL Orderby a number, Nulls last
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
MySQL is one of the most popular relational database management systems, widely used for its robustness and versatility. One of the common tasks when querying databases is sorting the results. The ORDER BY clause in MySQL is employed to sort data retrieved from a database in ascending or descending order. This article will delve specifically into ordering data by numbers with a focus on placing NULL values last.
Understanding ORDER BY in MySQL
The ORDER BY clause in MySQL is used to sort rows returned by a SELECT statement. It allows you to sort query results by one or more columns.
Syntax
- ASC: Ascending order (default)
- DESC: Descending order
However, dealing with columns that may contain NULL values can present unique challenges. By default, NULL values are considered lower than any non-null values and appear first when ordering in ascending order.
Ordering with NULLS LAST
In scenarios where you prefer to have NULL values appear last in the order, you'll need to employ specific strategies as MySQL, unlike some other SQL standards, does not natively support a NULLS LAST/NULLS FIRST syntax.
Technical Approach
To simulate NULLS LAST, you can modify the ORDER BY clause using conditional expressions:
- Using
IS NULLClause: You can use anIS NULLcondition within theORDER BYclause to shiftNULLvalues to the end.
In this approach, IS NULL is treated as a boolean: true (1) for NULL values and false (0) for non-null values. Thus, non-null values get priority in the sorting sequence.
- Using
CASE: Another method is using aCASEexpression:
This statement operates similarly by using a CASE to assign a sorting key before sorting by the actual column value.
Example
Consider a table employees with the following data:
| emp_id | salary |
| 1 | 70000 |
| 2 | NULL |
| 3 | 50000 |
| 4 | 20000 |
| 5 | NULL |
To order salaries in ascending order with NULL values last, the query would be:
Resulting Order
| emp_id | salary |
| 4 | 20000 |
| 3 | 50000 |
| 1 | 70000 |
| 2 | NULL |
| 5 | NULL |
Performance Considerations
When using expressions such as IS NULL or CASE in the ORDER BY clause, there might be an impact on performance, especially on large datasets. It is advisable to properly index columns involved in sorting operations and consider the database's execution plan to mitigate any potential slowdowns.
Conclusion
Sorting data in MySQL while handling NULL values requires additional steps but can be effectively managed using conditional techniques. By understanding and employing IS NULL or CASE expressions, you can ensure that NULL values appear last in your query results, improving data presentation and meeting business requirements.
Summary Table
| Method | Description | Use Case |
IS NULL | Evaluates NULL as true (1) and non-null as false (0) to order non-nulls first. | Quick and efficient way to order NULLs last |
CASE | Leveraging CASE for assigning a sort key to NULLs. | Provides explicit control over sorting |
| Indexing | Speed up sorting operations on large datasets. | Essential when dealing with large datasets |
MySQL’s flexibility and power allow you to correctly order datasets while managing the intricacies of NULL values, ensuring your applications maintain optimal data organization without compromise.

