I understand that the SQL standard allows multiple NULL values in a column that is part of the UNIQUE constraint.
What I don’t understand is why the UNION construct (at least in PostgreSQL,) treats NULL values as the same. For example:
$ select * from tmp_a;
a | b
---+---
a | b
a |
|
(3 rows)
$ select * from tmp_b;
a | b
---+---
a | c
a |
|
(3 rows)
$ select a, b from tmp_a union select a, b from tmp_b order by 1, 2;
a | b
---+---
a | b
a | c
a |
|
(4 rows)
The General Rule in the SQL-92 Standard is as follows:
13.1 ‘declare cursor’ (remember
ORDER BYis part of a cursor)General Rule 3b:
The SQL-89 stated the same a little more clearly IMO:
I would guess that PostgreSQL is performing a sort to remove duplicates as required by
UNIONand is groupingNULLvalues together in line with Standards.