Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

That error means your query is mixing a grouped result with a column that may have several values inside each group. Decide what one output row should represent, then select only values that are defined for that row: grouping keys, aggregates, or—in database-specific cases—columns functionally dependent on the keys.

Why SQL rejects the column

GROUP BY collapses input rows into one output row for each distinct combination of grouping values. Every expression in the SELECT list must therefore have one well-defined value for each resulting group.

Consider this query:

SELECT department_id, employee_name, SUM(salary)
FROM employees
GROUP BY department_id;

A department may contain several employees. department_id identifies the group and SUM(salary) summarizes it, but employee_name could refer to multiple people. There is no single name to return for a department unless the query defines a rule for choosing one. PostgreSQL commonly reports this as a grouping error, with SQLSTATE 42803; exact wording varies by database and version. See the PostgreSQL documentation on table expressions.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the fix that matches the result you want

Do not add every selected column to GROUP BY automatically. Adding a column changes the result grain—the meaning of one output row—and can split groups and change totals.

One row per department

If the desired result is one row for each department, omit the individual employee name:

SELECT department_id, SUM(salary) AS total_salary
FROM employees
GROUP BY department_id;

Now each output row represents a department, and both selected values make sense at that level.

One row per department and employee

If each employee should have a separate row, include the name in the grouping keys:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
SELECT department_id, employee_name, SUM(salary) AS total_salary
FROM employees
GROUP BY department_id, employee_name;

This is not merely a syntax correction: the result now has a row for each distinct department-and-name combination, rather than one row per department. If multiple source rows share that combination, their salaries are still summed together.

Keep employee rows and show the department total

If you want each employee’s detail row alongside the total for that employee’s department, use a window aggregate instead of collapsing rows with GROUP BY:

SELECT department_id,
       employee_name,
       SUM(salary) OVER (PARTITION BY department_id) AS department_total
FROM employees;

This pattern preserves the input rows while calculating a total over each department’s partition. Check your database engine’s documentation for its supported window-function syntax.

Return one total for the whole table

For a single overall total, select the aggregate without an unrelated row-level column or a GROUP BY:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
SELECT SUM(salary) AS total_salary
FROM employees;

Without grouping keys, this aggregate returns one overall summary. Adding a name or another detail field would again require a meaningful rule for which value represents the result.

Use an aggregate only when its meaning is right

An aggregate such as MAX(employee_name) may silence the error, but it returns the maximum name according to the database’s comparison rules—not necessarily a representative, latest, or otherwise relevant employee. Use an aggregate for a plain column only when that aggregate expresses the question you intend to ask.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why the message depends on the database

The underlying issue is the same, but databases differ in which grouped expressions they accept and what happens when a selected value is ambiguous. Do not assume that a query accepted by one engine will behave identically in another.

PostgreSQL

PostgreSQL reports a grouping error when a selected expression is neither grouped, aggregated, nor covered by a supported functional-dependency rule. The database documents grouped-query behavior in its table expressions reference. If you need exact wording or behavior for a particular release, check the documentation for that PostgreSQL version.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

MySQL 8.4

The MySQL 8.4 manual says ONLY_FULL_GROUP_BY is enabled by default. With that mode enabled, MySQL rejects nonaggregated expressions that are not grouped, functionally dependent on the grouping columns, or restricted to a single value under documented conditions. When the mode is disabled, MySQL may select an arbitrary value from a group; sorting the result with ORDER BY does not control which value is chosen. ANY_VALUE() explicitly allows an arbitrary value when it truly does not matter which one, but that is a MySQL-specific option, not a general SQL fix. See MySQL 8.4’s GROUP BY handling documentation.

SQL Server

Microsoft’s SQL Server documentation states: “However, you must include each table or view column in the GROUP BY list if you use it in any nonaggregate expression in the <select> list.” Read the Microsoft Learn GROUP BY reference for SQL Server’s rules and its separate grouping extensions, including ROLLUP and CUBE.

A quick way to diagnose your query

  1. State the intended grain. Finish the sentence “One output row represents …” with an answer such as a department, an employee, or the entire table.
  2. Check each selected expression. It must be a grouping expression, an aggregate result, or a value the database can prove is functionally dependent on the grouping keys.
  3. Pick the matching operation. Use GROUP BY to collapse rows into summaries; use a window aggregate when the detail rows should remain visible.
  4. Confirm the engine and version. Functional-dependency handling, accepted syntax, and error wording are not universal. Look up the specific database’s rules if the query’s behavior is unclear.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.