Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL group by without aggregate function. Why does it work?

there is a strange behavior. I know that when using GROUP BY, all columns from SELECT should either be in group by clause, or have aggregate function. However, the next code shows something controversial:

// id is a primary key
select id, name from user group by name; <-- error because of group by (expected)
select id, name from user group by id; <-- works (unexpected!)

What am I missing? Can not find documentation that says about some potential special case for primary key in group by.

like image 676
Dmitriy Apollonin Avatar asked Nov 20 '19 17:11

Dmitriy Apollonin


People also ask

Is aggregate function necessary with GROUP BY?

Expressions that are not encapsulated within an aggregate function and must be included in the GROUP BY Clause at the end of the SQL statement. This is an aggregate function such as the SUM, COUNT, MIN, MAX, or AVG functions.

Can I use GROUP BY without Where?

The groupby clause is used to group the data according to particular column or row. 2. Having can be used without groupby clause,in aggregate function,in that case it behaves like where clause. groupby can be used without having clause with the select statement.

How does GROUP BY work in PostgreSQL?

The PostgreSQL GROUP BY clause is used in collaboration with the SELECT statement to group together those rows in a table that have identical data. This is done to eliminate redundancy in the output and/or compute aggregates that apply to these groups.

Why do we need aggregate function?

An aggregate function allows you to perform a calculation on a set of values to return a single scalar value. We often use aggregate functions with the GROUP BY and HAVING clauses of the SELECT statement.


2 Answers

This is covered, but not especially obvious, in the docs:

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.

In this case, I'm guessing that id is the primary key of the table user which would make name functionally dependent on id.

like image 174
Jeremy Avatar answered Oct 18 '22 22:10

Jeremy


Your primary key can guaranty unicity for the id column.

I tried your statement on PostgreSQL 11.6 and 12.1 and I got the same error

SELECT id, name from unicity group by id;
ERROR:  column "unicity.name" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT id, name from unicity group by id;

Which version of PostgreSQL are you using?

like image 42
Thomas B Avatar answered Oct 18 '22 22:10

Thomas B