Errors duplicating code in PostgreSQL

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 “” 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!

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;

1 Like