Skip to content

Missing GROUP BY: non-aggregated column in aggregate query

Quick summary

Your query mixes aggregated and non-aggregated columns without grouping — the database cannot determine which values to show.

Why this happens

  • A column is selected alongside aggregate functions but is not in the GROUP BY clause

  • The GROUP BY clause is missing entirely when aggregate functions are used with other columns

  • A subquery or view uses aggregates without proper grouping

Minimal example

✗ Broken code
await db.query('SELECT department, name, COUNT(*) FROM employees GROUP BY department');
// Error: column name must appear in GROUP BY or be used in an aggregate
✓ Fixed code
await db.query('SELECT department, COUNT(*) FROM employees GROUP BY department');
// Or: SELECT department, MAX(name), COUNT(*) FROM employees GROUP BY department;

Either remove the non-aggregated column or add it to GROUP BY so the database knows how to group the results.

How to diagnose

  • Read the error message for the column that must appear in GROUP BY

  • Check each non-aggregated column in the SELECT list and verify it is in GROUP BY

  • Review the query to determine if the column should be aggregated or grouped

How to fix

  • Add the non-aggregated columns to the GROUP BY clause

  • Wrap the column in an aggregate function like MAX() or MIN() if it should be aggregated

  • Remove the column from the SELECT list if it is not needed in the result

How to prevent

  • Always include all non-aggregated SELECT columns in GROUP BY when using aggregate functions

  • Use a query builder or ORM that validates GROUP BY requirements

Related Resources

Related Glossary

Related Lessons

← Back to language