Errors duplicating code in PostgreSQL


#1

Hi, I'm using this to help learn PostgreSQL. I get this is based on SQLite so will be a little different.

When I run a query like:

SELECT name, city, MAX(age) FROM table GROUP BY city;

I get a return error saying "ERROR: column "table.name" must appear in the GROUP BY clause or be used in an aggregate function".

Any help with this? Do I need a different syntax for PostgreSQL? Thanks!


#2

When you are using any of the aggregate functions, anything that is not getting aggregated must be included in your GROUP BY list. This is common across all relational database SQL variants.

(FYI, double dash is a "comment out" syntax in most SQL languages)

So in your case you have the following:
SELECT name, --no aggregate
city, --no aggregate
MAX(age) --aggregate
FROM table
GROUP BY name, city;