The SQL query result set is sorted by specified conditions.
Oracle considers null to be the largest.
Sort in ascending order. By default, the null value is placed behind.
Sort in descending order. By default, the null value is ranked first.
There are several ways to change this situation:
(1) Use the nvl function or decode function to convert null to a specific value.
(2) Use case syntax to convert null to a specific value (later than oracle9i. Similar to sqlserver ):
1. When the value is a value, specify the position for sorting.
Select * from (
Select 1 t from dual
Union all
Select 2 t from dual
Union all
Select 3 t from dual
Union all
Select 4 t from dual
) A order by case when a. t = 2 then 1 else 0 end
2. When the value is a value, the sorting is always at the end
Select * from (
Select 1 t from dual
Union all
Select 2 t from dual
Union all
Select 3 t from dual
Union all
Select 4 t from dual
) A order by case when a. t = 2 then null else 0 end asc nulls last
Result:
(3) Use nulls first or nulls last syntax.
This is oracle's syntax for sorting null values.
Nulls first: Place null at the top. For example, select * from mytb order by mycol nulls first
Null last: the last line of null. For example, select * from mytb order by mycol nulls last
If you want to sort the columns containing null according to your own wishes, you can perform the preceding operations.