SQL error · GROUP BY and calculations

GROUP BY leaves a column ungrouped

Problem: You are selecting a column that the group key does not determine.

Solution: Group by the right key or remove that column; do not add an aggregate without deciding what it should represent.

Why it happens: GROUP BY leaves a column ungrouped

Non-portable SQL
WITH sales(category, product, amount) AS (
  VALUES ('Audio', 'Headphones', 40),
         ('Audio', 'Speaker', 90),
         ('Office', 'Keyboard', 60)
)
SELECT category,
       product,
       SUM(amount) AS total
FROM sales
GROUP BY category;

The group Audio contains two products. Which one should the column productshow? PostgreSQL generally rejects an ungrouped reference that is not functionally dependent; MySQL with ONLY_FULL_GROUP_BY (on by default) applies an equivalent check. SQLite allows “bare” columns and may pick a value from any row in the group, so accepting the query does not make that product a stable description of the group.

“GROUP BY leaves a column ungrouped” error messages by engine

The exact wording depends on the database. Compare these common messages with the one in your console.

EngineTypical message
PostgreSQLERROR: column "sales.product" must appear in the GROUP BY clause or be used in an aggregate function
MySQLERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'sales.product' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
SQL ServerMsg 8120, Level 16: Column 'sales.product' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
OracleORA-00979: not a GROUP BY expression

SQLite raises no error: it picks an arbitrary value from the ungrouped column, which can hide the problem.

How to fix it in SQL

One row per category

SQL portable
WITH sales(category, product, amount) AS (
  VALUES ('Audio', 'Headphones', 40),
         ('Audio', 'Speaker', 90),
         ('Office', 'Keyboard', 60)
)
SELECT category,
       SUM(amount) AS total
FROM sales
GROUP BY category
ORDER BY category;
One row per category
categorytotal
Audio130
Office60

One row per category and product

If the product is part of the unit you want to analyse, include it in the grouping: GROUP BY category, product. The result will have a different granularity: one row per category-and-product pair.

How to check it

Check whether each row represents a category or a product. The number of groups should match that answer.

How to avoid it next time

A column accepted by a lenient engine can still have an undefined value.

Related cases worth checking

Some engines may allow a column that is not written out in GROUP BY if they can prove it is determined by the grouped columns, for example through a key. That rule does not justify selecting arbitrary columns. For a learner, the safest check is still to ask: “can this column have more than one value within the same group?”.