How is this a valid statement (where id is the primary key of the table):
select * from table group by id ;
and this is not:
select * from table group by name ;
ERROR: column "pgluser.id" must appear in the GROUP BY clause or be used in an aggregate function
Fiddle .
The question is why is the first a legal query, ie why grouping by primary key is valid?
2 Answers 2
id
is a primary key.
As far as I remember, this is actually a legal query according to ANSI/ISO SQL.
Grouping by primary key results in a single record in each group which is logically the same as not grouping at all / grouping by all columns, therefore we can select all other columns.
create table t (id int primary key,c1 int,c2 int)
insert into t (id,c1,c2) values (1,2,3),(4,5,6);
select * from t group by id;
+----+----+----+
| id | c1 | c2 |
+----+----+----+
| 1 | 2 | 3 |
+----+----+----+
| 4 | 5 | 6 |
+----+----+----+
Reference given by @a_horse_with_no_name
https://www.postgresql.org/docs/current/static/sql-select.html#SQL-GROUPBY
When GROUP BY is present, or any aggregate functions are present, it is not valid for the SELECT list expressions to refer to ungrouped columns except within aggregate functions or when the ungrouped column is functionally dependent on the grouped columns, since there would otherwise be more than one possible value to return for an ungrouped column. A functional dependency exists if the grouped columns (or a subset thereof) are the primary key of the table containing the ungrouped column.
While logically we would expect UNIQUE NOT NULL to follow the same behaviour, it applies only for PK (as described in the documentation)
create table t (id int unique not null,c1 int,c2 int);
insert into t (id,c1,c2) values (1,2,3),(4,5,6);
select * from t group by id;
[Code: 0, SQL State: 42803] ERROR: column "t.c1" must appear in the GROUP BY clause or be used in an aggregate function
-
thanks but the question is why is this a legal query IE why grouping by primary key is valid?Sachin Verma– Sachin Verma2016年12月13日 10:52:24 +00:00Commented Dec 13, 2016 at 10:52
-
Is it clear or additional explanation is needed?David דודו Markovitz– David דודו Markovitz2016年12月13日 11:05:50 +00:00Commented Dec 13, 2016 at 11:05
-
Is it just primary key that sql spec states this for, or all unique keys? could you find it in the SQL spec? this is interesting, never knew of this.Evan Carroll– Evan Carroll2016年12月13日 17:08:57 +00:00Commented Dec 13, 2016 at 17:08
-
1@EvanCarroll, tested, does not work for UNIQUE (UNIQUE NOT NULL)David דודו Markovitz– David דודו Markovitz2016年12月13日 17:13:10 +00:00Commented Dec 13, 2016 at 17:13
-
1I asked a question about this very behavior on PostgreSQL's developer mailing list a few years ago, and got a very informative answer about why PG recognizes functional dependencies from
primary key
s but not fromunique not null
constraints.Dan Lenski– Dan Lenski2017年06月17日 06:04:39 +00:00Commented Jun 17, 2017 at 6:04
I think the reason would be:
id
is primary key here(unique) and group by primary key is alike to group by *
. So it's just similar to
select * from table group by *
which should be fine.
table
and the error message.(id)
is the primary key.