The SQL query result set is sorted by specified conditions.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.