I'm curious about a query I saw written "by a friend" (LOL) and I was surprised by it. Here's the simplified case:
create table my_table (
a int,
b int,
c int,
d int,
primary key (a, b, c)
);
insert into my_table (a, b, c, d) values (1, 2, 3, 4);
insert into my_table (a, b, c, d) values (5, 6, 7, 8);
select *, count(*), sum(d)
from my_table
group by 1, 2, 3
This actually works in PostgreSQL, and translates in the last line 1, 2, 3
to a, b, c
, and never to c, b, a
or other.
Result (PostgreSQL 11.1):
a b c d count sum
- - - - ----- ---
5 6 7 8 1 8
1 2 3 4 1 4
So, does *
actually preserve column ordering?
I won't probably write a confusing query like this ever, but still wanted to know if the SQL Standard said anything about it.