Home › Databases

ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause ... incompatible with sql_mode=only_full_group_by (MySQL) — how to fix

ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'shop.orders.created_at' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

Quick fix: Fix the query: add the column to GROUP BY, wrap it in an aggregate (MAX(created_at)), or use ANY_VALUE(created_at) if any value is fine. Only as a last resort, remove the mode for the session: SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''));

What it means

GROUP BY customer_id collapses all rows of one customer into one result row. If you also select created_at, that customer may have many different created_at values, and the query doesn't say which one to show.

SELECT customer_id, created_at, COUNT(*)
FROM orders
GROUP BY customer_id;          -- ERROR 1055

With ONLY_FULL_GROUP_BY (default since MySQL 5.7.5, also in 8.x) MySQL rejects such queries instead of returning an arbitrary value. Older MySQL silently picked one, which is why upgraded apps suddenly break. The PostgreSQL equivalent is "column must appear in the GROUP BY clause".

Check the active mode:

SELECT @@GLOBAL.sql_mode, @@SESSION.sql_mode;

Fix 1: aggregate the column

Decide what you actually want per group:

SELECT customer_id,
       MAX(created_at) AS last_order,
       COUNT(*)        AS orders
FROM orders
GROUP BY customer_id;

MIN, MAX, SUM, AVG, COUNT and GROUP_CONCAT are all valid.

Fix 2: add it to GROUP BY

If you want one row per combination, group by both:

SELECT customer_id, status, COUNT(*)
FROM orders
GROUP BY customer_id, status;

MySQL also accepts columns that are functionally dependent on the grouped columns. If you group by a table's primary key, you may select any other column of that table:

SELECT c.id, c.name, COUNT(o.id)
FROM customers c
JOIN orders o ON o.customer_id = c.id
GROUP BY c.id;                 -- c.name is allowed: c.id is the PK

Fix 3: ANY_VALUE() when any row is fine

If all values in the group are the same anyway, or you really don't care which one you get:

SELECT customer_id, ANY_VALUE(customer_email), COUNT(*)
FROM orders
GROUP BY customer_id;

This keeps ONLY_FULL_GROUP_BY on for everything else.

Fix 4: you want the whole latest row per group

"Latest order per customer" is a greatest-n-per-group problem, not a GROUP BY one. On MySQL 8 use a window function:

SELECT *
FROM (
  SELECT o.*,
         ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY created_at DESC) AS rn
  FROM orders o
) t
WHERE rn = 1;

Fix 5: disable the mode (legacy code only)

This restores the old, nondeterministic behaviour. Prefer fixing queries.

For the current session:

SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''));

Permanently, in my.cnf under [mysqld], list the modes you want without it (copy the rest from SELECT @@GLOBAL.sql_mode), then restart MySQL:

[mysqld]
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

SET GLOBAL sql_mode = ... works too but is lost on restart.

Laravel

Laravel's MySQL connection sets a strict sql_mode that includes ONLY_FULL_GROUP_BY when 'strict' => true in config/database.php. Rather than setting strict to false (which also disables other checks), fix the query or set an explicit 'modes' => [...] list without ONLY_FULL_GROUP_BY.

Long queries are easier to check with the SQL formatter.

Last updated 2026-11-29.