Oracle SQL null value sorting (Nulls)

Source: Internet
Author: User

In data development applications, data needs to be sorted in most cases. However, in Oracle databases, there is a NULL value ). If some values in a column are empty, if this column is sorted, the rows with null values are sorted at the end, for example, if I sort the EMPLOYEES in HR mode by commission rate,

SELECT t. employee_id, t. last_name, t. commission_pct
FROM employees t
WHERE t. employee_id BETWEEN 178 AND 181
Order by t. commission_pct

The result is as follows:

179 Johnson 0.10
178 Grant 0.15
180 Taylor
181 Fleaur

180,181 due to no commission ratio
In some cases, we want to sort records with null values before rather than after them. In this case, we need to use keywords:
Nulls first: sorts records with null values before
Nulls last: sorts records with null values (default)

For example, make a few changes in the above example:

SELECT t. employee_id, t. last_name, t. commission_pct
FROM employees t
WHERE t. employee_id BETWEEN 178 AND 181
Order by t. commission_pct NULLS FIRST

The result is as follows:

180 Taylor
181 Fleaur
179 Johnson 0.10
178 Grant 0.15

180,181 no commission rate is pushed to the front

Related Article

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.