Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-29 · container image postgres:17
After GROUP BY, each group becomes one row. A column that is not grouped can have several values in a group, so PostgreSQL refuses to pick one. The message names the column (emp.name) and the ^ points to where it is used — in SELECT or in ORDER BY.
MySQL with ONLY_FULL_GROUP_BY off silently picks a value, so queries ported from MySQL often hit this. The same SQLSTATE 42803 is also used for aggregate functions are not allowed in WHERE.
01A SELECT column that is neither in GROUP BY nor aggregated
The query groups by dept but also selects name. The Sales group has two names (Kim and Lee), so there is no single value to show.
Setup
CREATE TABLE emp (id int PRIMARY KEY, dept text, name text, salary int);
INSERT INTO emp VALUES (1, 'Sales', 'Kim', 300), (2, 'Sales', 'Lee', 400), (3, 'IT', 'Park', 500);CREATE TABLE
INSERT 0 3SELECT dept, name, sum(salary) FROM emp GROUP BY dept;ERROR: 42803: column "emp.name" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT dept, name, sum(salary) FROM emp GROUP BY dept;
^SELECT dept, name, salary FROM emp ORDER BY dept, name; dept | name | salary
-------+------+--------
IT | Park | 500
Sales | Kim | 300
Sales | Lee | 400
(3 rows)Decide what you want per group. Here string_agg(name, ', ' ORDER BY name) lists all names (Sales → Kim, Lee). If you want one row per employee, add name to GROUP BY instead.
SELECT dept, string_agg(name, ', ' ORDER BY name) AS names, sum(salary) FROM emp GROUP BY dept ORDER BY dept; dept | names | sum
-------+----------+-----
IT | Park | 500
Sales | Kim, Lee | 700
(2 rows)02Grouping by the foreign key column instead of the parent table's primary key
The query groups by o.customer_id and selects c.name. They have the same value after the join, but PostgreSQL only knows that c.name depends on c.id, the primary key of customers (customers_pkey). Grouping by a column of another table does not count.
Setup
CREATE TABLE customers (id int PRIMARY KEY, name text);
CREATE TABLE orders (id int PRIMARY KEY, customer_id int REFERENCES customers (id), amount int);
INSERT INTO customers VALUES (1, 'Kim'), (2, 'Lee');
INSERT INTO orders VALUES (10, 1, 100), (11, 1, 250), (12, 2, 80);CREATE TABLE
CREATE TABLE
INSERT 0 2
INSERT 0 3SELECT o.customer_id, c.name, sum(o.amount)
FROM orders o JOIN customers c ON c.id = o.customer_id
GROUP BY o.customer_id;ERROR: 42803: column "c.name" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT o.customer_id, c.name, sum(o.amount)
^SELECT conname, contype FROM pg_constraint WHERE conrelid = 'customers'::regclass; conname | contype
----------------+---------
customers_pkey | p
(1 row)Group by c.id. Because it is the primary key, PostgreSQL allows any other column of customers (like c.name) in SELECT without listing it in GROUP BY.
SELECT c.id, c.name, sum(o.amount)
FROM orders o JOIN customers c ON c.id = o.customer_id
GROUP BY c.id
ORDER BY c.id; id | name | sum
----+------+-----
1 | Kim | 350
2 | Lee | 80
(2 rows)03Filtering on an aggregate in WHERE
WHERE filters rows before grouping, so sum(amount) does not exist yet. The message is aggregate functions are not allowed in WHERE, with the same SQLSTATE 42803.
Setup
CREATE TABLE sales (id int PRIMARY KEY, region text, amount int);
INSERT INTO sales VALUES (1, 'East', 700), (2, 'East', 600), (3, 'West', 200), (4, 'West', 300);CREATE TABLE
INSERT 0 4SELECT region, sum(amount) FROM sales WHERE sum(amount) > 1000 GROUP BY region;ERROR: 42803: aggregate functions are not allowed in WHERE
LINE 1: SELECT region, sum(amount) FROM sales WHERE sum(amount) > 10...
^SELECT region, sum(amount) FROM sales GROUP BY region ORDER BY region; region | sum
--------+------
East | 1300
West | 500
(2 rows)Move the condition to HAVING, which filters after grouping. Only East (1300) remains.
SELECT region, sum(amount) FROM sales GROUP BY region HAVING sum(amount) > 1000; region | sum
--------+------
East | 1300
(1 row)04ORDER BY a column that is not grouped
The SELECT list is fine, but ORDER BY login_at sorts by a raw column. User kim has two login times, so PostgreSQL cannot sort the group by one of them. The ^ points into the ORDER BY.
Setup
CREATE TABLE logins (id int PRIMARY KEY, user_name text, login_at timestamp);
INSERT INTO logins VALUES (1, 'kim', '2026-11-01 09:00'), (2, 'kim', '2026-11-03 10:00'), (3, 'lee', '2026-11-02 08:30');CREATE TABLE
INSERT 0 3SELECT user_name, count(*) FROM logins GROUP BY user_name ORDER BY login_at DESC;ERROR: 42803: column "logins.login_at" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ... count(*) FROM logins GROUP BY user_name ORDER BY login_at D...
^SELECT user_name, login_at FROM logins ORDER BY user_name, login_at; user_name | login_at
-----------+---------------------
kim | 2026-11-01 09:00:00
kim | 2026-11-03 10:00:00
lee | 2026-11-02 08:30:00
(3 rows)Sort by an aggregate, such as max(login_at) for the latest login. Showing it as last_login makes the order visible.
SELECT user_name, count(*), max(login_at) AS last_login FROM logins GROUP BY user_name ORDER BY max(login_at) DESC; user_name | count | last_login
-----------+-------+---------------------
kim | 2 | 2026-11-03 10:00:00
lee | 1 | 2026-11-02 08:30:00
(2 rows)FAQ
How do I fix 'must appear in the GROUP BY clause or be used in an aggregate function'?
Either add the column to GROUP BY or wrap it in an aggregate such as max(), min(), sum() or string_agg(). Choose based on whether you want one row per value or one value per group.
Why does this query work in MySQL but not in PostgreSQL?
MySQL without ONLY_FULL_GROUP_BY returns an arbitrary value for ungrouped columns. PostgreSQL requires the column to be grouped, aggregated, or dependent on a grouped primary key.
Do I have to list every column of the table in GROUP BY?
No. If you group by the table's primary key, PostgreSQL lets you select the table's other columns. In a join, group by the parent's primary key (c.id), not the child's FK column.
How do I filter by sum() or count() in PostgreSQL?
Use HAVING after GROUP BY. WHERE runs before grouping, so aggregates are not allowed there.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12