October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

SQL GROUP BY Errors: How to Choose the Right Fix

A GROUP BY error means a selected column has no single defined value within an output group. Fix it by matching the query to the grain you actually need.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

SQL rejects a plain column in a grouped query when that column could have multiple values within an output group. The database cannot know which value you mean. The right fix depends on what each output row should represent—not on adding every selected column to GROUP BY.

Why a column must appear in the GROUP BY clause

GROUP BY combines input rows that share the same grouping values. Each output expression must then have one well-defined value for every resulting group: it must be a grouping expression, an aggregate result, or, where the database supports and can establish it, a value functionally dependent on the grouping keys.

As an Amazon Associate I earn from qualifying purchases.

Consider this query:

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

The query asks for one row per department, and SUM(salary) can produce one salary total for each department. But a department can contain many employees, so employee_name has no single value for that department. The query has not specified which employee name to return.

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

PostgreSQL reports this as a grouping error, commonly with SQLSTATE 42803. Its documentation explains grouped queries and grouping expressions in Table Expressions. Exact error text varies by database and version.

Choose a fix based on the row each result should represent

One row per department

If the question is “What is the salary total for each department?”, remove the employee-level name:

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

Now both selected values describe the same grain: department.

One row per department and employee

If the question is instead about each department-and-employee combination, group by both columns:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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 a different result. Adding employee_name splits each department into smaller groups, so the totals are no longer department-wide totals. Add a column to the grouping list only when that finer grain is what you intend.

Keep employee rows and show a department total

If you want each employee row to remain visible alongside the total for that employee’s department, use a window aggregate rather than collapsing rows with GROUP BY:

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

This is a general SQL pattern, but check your database’s documentation for supported syntax and features.

Calculate one total for the whole table

For a single overall total, select only the aggregate:

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

An aggregate query without GROUP BY returns an overall aggregate; pairing it with an arbitrary row-level field does not give that field a meaningful value.

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

Why SQL databases can behave differently

PostgreSQL

When PostgreSQL raises the grouping error, inspect the selected expressions against the intended grouping keys. A plain value that varies within a group cannot identify itself from an aggregate such as SUM. Do not assume another database’s permissive behavior will be accepted or produce a meaningful result in PostgreSQL. See the PostgreSQL documentation on grouped queries.

MySQL 8.4

The MySQL 8.4 manual says ONLY_FULL_GROUP_BY is enabled by default. With that mode enabled, MySQL rejects nonaggregated selected or referenced expressions unless they are grouped, functionally dependent on grouping columns, or meet documented single-value conditions. If the mode is disabled, MySQL may choose any value from a group; an ORDER BY does not control which value is chosen. MySQL documents ANY_VALUE() for cases where an arbitrary value truly is acceptable, but that is not a general-purpose fix. Details are in the MySQL 8.4 GROUP BY handling documentation.

SQL Server

Microsoft’s SQL Server guidance says that columns used in a nonaggregate expression in the SELECT list must be included in the GROUP BY list. Its documentation also covers grouping extensions such as ROLLUP, CUBE, and grouping sets for subtotal and grouping-combination queries; those features are not needed to fix the basic ambiguity. See Microsoft Learn: GROUP BY (Transact-SQL).

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

A quick way to diagnose the error

  1. Write down the intended grain. Decide whether one output row should represent a department, an employee, a department-and-employee pair, or an original detail row.
  2. Check every selected expression. Each must be a grouping expression, an aggregate, or a value the database can prove is functionally dependent on the grouping keys.
  3. Choose the matching query shape. Use GROUP BY to collapse rows into groups, a window aggregate to retain detail rows with a group-level calculation, or a plain aggregate without a row-level column for a whole-table total.
  4. Verify the engine and version. If an exact fix depends on functional-dependency rules, aliases, or error wording, check the documentation for the database that actually runs the query.

A function such as MAX(employee_name) may make an expression aggregate syntactically, but it returns the maximum name under the database’s comparison rules—not necessarily the employee the question intends. Use an aggregate on a plain column only when that aggregate expresses the value you actually want.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.