NUllsfirst/last function in PostgresQL

Source: Internet
Author: User
Tags postgres database
Nullslast indicates that the Null value is always placed behind all values during sorting, that is, when orderbyadesc is processed, the PostgresQL executor considers that the Null value is smaller than all values, while the ord

Nulls last indicates that the Null value is always placed behind all values during sorting, that is, when order by a desc is processed, the PostgresQL executor considers that the Null value is smaller than all values, while ord

Nulls first/last
The Nulls first/last function is mainly used in order by sorting clauses, which affects the position of Null values in the sorting result. Simply put, Nulls first indicates that the Null value is always in front of all values during sorting, that is, when processing order by a desc, the PostgresQL executor considers that the Null value is greater than all values, when order by a or order by a asc, the executor considers that the Null value is smaller than all values, and puts the Null value first. Nulls last indicates that the Null value is always placed behind all values during sorting, that is, when order by a desc is processed, the PostgresQL executor considers that the Null value is smaller than all values, when order by a or order by a asc, the executor considers that the Null value is greater than all values, and puts the Null value first. When the Nulls first/last function is not specified, the executor considers that the Null value must be greater than all values by default, and the sorting result of the order by clause is processed.

Simple Nulls first/last function display
The following tests are performed in the Postgres database. The database version is 9.2.2 and the test system is Linux.

Simple functions of a common table:

Create table test1 (a int, B int );

Insertinto test1 values (1, 2 );

Insertinto test1 values (3, 4 );

Insertinto test1 values (5 );

Select * from test1 order by B desc nulls first;

A B

5

3 4

1 2

Select * from test1 order by B desc nulls last;

A B

3 4

1 2

5

Select * from test1 order by B desc nulls; Error

The partition table function is simple. Note the following when creating a partition table in a PostgresQL database:

Createtable test_hash (a int, B int );

Createtable test_hash1 (check (a> = 0 and a <5) inherits (test_hash );

Createtable test_hash2 (check (a> = 5) inherits (test_hash );

Createrule test_hash_1 as on insert to test_hash where (a> = 0 and a <5) do insteadinsert into test_hash1 values (NEW. a, NEW. B );

Createrule test_hash_2 as on insert to test_hash where (a> = 5) do instead insertinto test_hash2 values (NEW. a, NEW. B );

Insertinto test_hash values (1, 2 );

Insertinto test_hash values (3, 4 );

Insertinto test_hash values (5 );

Select * from test_hash order by B desc nulls first;

A B

5

3 4

2 2

Select * from test_hash order by B desc nulls last;

A B

3 4

1 2

5

The above are the Nulls first/last functions of the order by clause in the select statement. The same applies to other places that require the order by clause, such as creating an index. Here is just a simple demonstration of the function, there is time to write some articles to analyze the PostgresQL database.

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.