Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

PostgreSQL ERROR: column "c.name" must appear in the GROUP BY clause or be used in an aggregate function (42803)

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

PostgreSQL stops a grouped query with ERROR: column "c.name" must appear in the GROUP BY clause or be used in an aggregate function when the SELECT list names a column that can have more than one value inside a group. The server will not guess which value to show. Either group by that column too, wrap it in an aggregate, or group by the table's primary key so the column is determined by it. Whichever you choose, check afterwards that the totals still add up.

What the error means

The error has SQLSTATE 42803, named grouping_error in Appendix A. PostgreSQL 17 source (tag REL_17_9, parse_agg.c) builds the text from the table alias and column name, column "%s.%s" must appear in the GROUP BY clause or be used in an aggregate function, which is why the message shows c.name and not just name. That is our reading of the code.

The manual states the rule on the GROUP BY page: "if a table is grouped, columns that are not listed in GROUP BY cannot be referenced except in aggregate expressions". Its example: SELECT * FROM test1 GROUP BY x fails "because there is no single value for the column y that could be associated with each group".

The rule has one exception. The SELECT page says an ungrouped column is allowed "when the ungrouped column is functionally dependent on the grouped columns", and the same page says "A functional dependency exists if the grouped columns (or a subset thereof) are the primary key of the table containing the ungrouped column". The page adds that PostgreSQL "recognizes functional dependency ... only when a table's primary key is included in the GROUP BY list". Our own reading: a join condition that makes two columns equal is not a case the page names.

The same rule applies to HAVING: "Each column referenced in condition must unambiguously reference a grouping column, unless the reference appears within an aggregate function or the ungrouped column is functionally dependent on the grouping columns".

Likely causes

Cause What you see How to confirm
A plain column in the SELECT list is not in GROUP BY The message names it, as alias.column The column appears in the select list, outside any sum(), max() or similar
SELECT * or t.* with GROUP BY The message names a column you never typed The select list uses *
The query groups by a foreign key, not the joined table's primary key The joined table's name or price is named \d of that table shows its primary key, which is not in the GROUP BY list
The query came from a database that accepts it Works there, fails here The query is built for an engine that accepts ungrouped columns; see MySQL ERROR 1055 (42000) "Expression #2 of SELECT list is not in GROUP BY clause": make the query deterministic before you switch only_full_group_by off
A subquery uses an ungrouped column of the outer query subquery uses ungrouped column "x.y" from outer query (source reading) The inner query refers to an outer column that the outer GROUP BY lacks
An ORM or report tool generated the SELECT list The column is one nobody typed Log the generated SQL and compare it with the model

Prove the cause before changing anything

Everything here only reads.

1. Read the column in the message. The part before the dot is the table alias in the query, and the part after is the column.

2. Find where the column sits. In the query text, check the select list and HAVING, where the manual's rule applies. Our own reading is that a column used only in ORDER BY is treated the same way, so check there too.

3. Read the table's primary key.

\d customers

The manual says \d shows "all columns, their types ... Associated indexes, constraints, rules, and triggers are also shown", so the primary key appears in the index list. A good result for the shortcut fix is that the column named in the message belongs to a table whose primary key you can add to GROUP BY. A bad result is a table with no primary key: then PostgreSQL has no dependency to use, and you must group by the column or aggregate it.

4. Record the row count and the total before changing the grain.

SELECT count(*), sum(total) FROM orders;

Keep both figures. Your fix must give the same total when the grouped results are added up, and Confirm steps 2 and 4 compare against them.

Fix

Pick by what the report means.

  • The column is a property of the thing you group by (a customer's name, a product's price). Group by that table's primary key. The manual's own example does this: "it would be enough to group by product_id", since "name and price would be functionally dependent on the product ID".
  • You want one value per group and any one will do. Wrap it in an aggregate such as max(c.name). The Aggregate Functions page lists any_value as returning "an arbitrary value from the non-null input values".
  • The column should split the groups. Add it to GROUP BY. The result has one row for every distinct combination, so there are more rows than before.
  • You want the detail rows with a total beside them. Use a window function. The manual says window functions "do not cause rows to become grouped into a single output row", so sum(o.total) OVER (PARTITION BY o.customer_id) keeps one output row for each order row.
  • You want one whole row for each group, for example the newest order. SELECT DISTINCT ON (customer_id) ... ORDER BY customer_id, ordered_at DESC keeps "only the first row of each set". The manual says the first row is "unpredictable unless ORDER BY is used", and that the DISTINCT ON expressions "must match the leftmost ORDER BY expression(s)".

The first option, applied to the example below, gives the candidate query that follows. Test it on a copy of the query, not in the query file:

SELECT c.id, c.name, sum(o.total) AS total
FROM customers c JOIN orders o ON o.customer_id = c.id
GROUP BY c.id;

Here c.id is the primary key of customers, so c.name depends on it and is allowed. This block only reads; it is a candidate to test before it replaces the old query.

Putting the chosen version into the query file is the one changing step, and it is set out below.

First save the current state:

cp -p customer_totals.sql customer_totals.sql.2026-12-03

Risk: the next edit changes the query that produces the customer totals report. If the new GROUP BY has a different grain from the old query, rows can split, merge or repeat, and the report's figures change with no error from PostgreSQL. Whatever reads the report, such as a spreadsheet or an invoice run, receives the new figures.

Edit customer_totals.sql and replace the select list and the GROUP BY clause with the version you tested.

Only if you need to back out: the copy below overwrites the edited customer_totals.sql with the saved one, so any other change made to the file since the save is lost. Copy the saved file back, then rerun the query.

cp -p customer_totals.sql.2026-12-03 customer_totals.sql

Example (illustrative): On db01, a report on app01 runs SELECT c.name, sum(o.total) FROM customers c JOIN orders o ON o.customer_id = c.id GROUP BY o.customer_id and fails with column "c.name" must appear in the GROUP BY clause or be used in an aggregate function. \d customers shows a primary key on id. GROUP BY o.customer_id is not that key, so PostgreSQL will not treat c.name as dependent on it. The table holds three orders: customer 1 with totals 100.00 and 50.00, customer 2 with 75.00. Grouping by c.id gives two rows, Acme 150.00 (100.00 + 50.00) and Bolt 75.00, and the overall total is 225.00 (150.00 + 75.00), the same as the three orders added up directly.

Confirm the fix

  1. The query runs without 42803.
  2. The sum of the grouped totals equals the sum recorded in prove step 4: compare the sum(total) figure saved there with the total of the report's rows, as in the example's 225.00. If a join drops rows (an inner join to a table with no match), the totals can differ for that reason alone.
  3. The number of rows is the number you expect for the chosen grain: one per customer for the primary-key option, more if you added a splitting column.
  4. For a window-function version, the row count equals the number of order rows, not the number of customers.

What to watch afterwards

  • A column that looks dependent but is not. A name that appears with two spellings under one customer id would be hidden by max() or any_value, and shown twice if you add it to GROUP BY. Check the data once before relying on either.
  • any_value returns "an arbitrary value", so two runs can return different names for the same group when the group holds different values (our reading).
  • Reports moved between engines. A query written for an engine that accepts ungrouped columns fails here at its first run.
  • After the query fails once, a client that carries on in the same transaction meets a second error. See PostgreSQL "ERROR: current transaction is aborted, commands ignored until end of transaction block" (25P02): find the first error.

Risks and limits

  • These commands have not been run or lab-tested.
  • Grouping by a primary key is accepted only because the manual says PostgreSQL recognises that case. The manual names only the primary-key case, so a dependency that holds only because of a join condition or a WHERE clause is not recognised (our reading).
  • any_value appears on the PostgreSQL 17 Aggregate Functions page. The page gives no version range for older releases; check the page for your own version.
  • Changing the grain of a query changes its meaning. The test is the reconciliation in "Confirm the fix", not the absence of an error.
  • The message text is a reading of PostgreSQL 17 source; other releases may word it differently.

Sources

  • 7.2. Table Expressions — PostgreSQL Global Development Group, PostgreSQL 17 documentation, no date shown, accessed 3 December 2026
  • SELECT — PostgreSQL Global Development Group, PostgreSQL 17 documentation, no date shown, accessed 3 December 2026
  • 9.21. Aggregate Functions — PostgreSQL Global Development Group, PostgreSQL 17 documentation, no date shown, accessed 3 December 2026
  • 3.5. Window Functions — PostgreSQL Global Development Group, PostgreSQL 17 documentation, no date shown, accessed 3 December 2026
  • psql — PostgreSQL Global Development Group, PostgreSQL 17 documentation, no date shown, accessed 3 December 2026
  • Appendix A. PostgreSQL Error Codes — PostgreSQL Global Development Group, PostgreSQL 17 documentation, no date shown, accessed 3 December 2026
  • parse_agg.c — PostgreSQL source repository, tag REL_17_9, no date shown, accessed 3 December 2026