GROUP BY 🗃️ SQL

Column must appear in the GROUP BY clause / isn't in GROUP BY (MySQL 1055 only_full_group_by)

You selected a column that is neither grouped nor aggregated, so the result would be ambiguous.

Seen on: MySQL PostgreSQL SQL

Meaning

Each output row of a GROUP BY represents many input rows. Non-aggregated columns must be in the GROUP BY (or functionally dependent on it). MySQL 5.7+ enforces this with ONLY_FULL_GROUP_BY (error 1055); PostgreSQL always has.

Common causes

  • SELECT includes a column not in GROUP BY and not aggregated
  • Code written for old MySQL defaults that silently picked a random row
  • ORDER BY a non-grouped column

⚡ Quick fix

  1. Add the column to GROUP BY, or wrap it in an aggregate (MAX, MIN, ANY_VALUE)
  2. Group by the primary key when selecting many columns from that table
  3. Avoid disabling ONLY_FULL_GROUP_BY — it hides real bugs

Detailed fix by platform

SQL

  1. Aggregate or group explicitly:
    sql
    -- ❌ which order's created_at should be shown per customer?
    SELECT customer_id, created_at, COUNT(*) FROM orders GROUP BY customer_id;
    -- ✅
    SELECT customer_id, MAX(created_at) AS last_order, COUNT(*) AS orders
    FROM orders GROUP BY customer_id;

How to diagnose

  1. Column — Which column is named?
  2. Intent — Which value do you actually want per group?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.