Specifies the ordering of NULL values in an SQL query.

Controls whether NULLs appear first or last in ORDER BY results, using NULLS FIRST or NULLS LAST. Essential for predictable sorting when NULLs are present, since NULLs sort differently across databases by default.

Compatibility

Show:
Database System Support Status Since Version Notes
MySQL ✗ Not Supported MySQL does not support NULLS FIRST/LAST in ORDER BY. You can use ISNULL(col) (returns 1/0) or col IS NULL in CASE to control position.
PostgreSQL ✓ Supported 9.5
SQL Server ✗ Not Supported SQL Server does not support NULLS FIRST/LAST in ORDER BY. To get a similar effect, you can use a CASE expression: ORDER BY CASE WHEN column_name IS NULL THEN 0 ELSE 1 END, column_name ASC;
Oracle ✓ Supported 12c
SQLite ✗ Not Supported SQLite does not support NULLS FIRST/LAST in ORDER BY. You can use ISNULL(col) (returns 1/0) or col IS NULL in CASE to control position.

Details

NULLS FIRST and NULLS LAST provide control over the handling of NULL values in sorting, which can differ across databases.

Standard Syntax

SELECT * FROM table_name ORDER BY column_name NULLS FIRST;

Version Support

MySQL: Not supported PostgreSQL: Since 9.5 SQL Server: Not supported Oracle: Since 12c SQLite: Not supported

Per-Database Syntax & Notes

PostgreSQL

SELECT * FROM table_name ORDER BY column_name NULLS FIRST;

Oracle

SELECT * FROM table_name ORDER BY column_name NULLS FIRST;