MySQL 8.4Lab-verified · 2026-12-01

MySQL ERROR 1055 (42000): Expression is not in GROUP BY clause and contains nonaggregated column

The query shows a column that is neither in GROUP BY nor inside an aggregate, so MySQL cannot tell which row's value to use.

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

Jump to a cause

  1. 01An extra column in SELECT that is not in GROUP BY
  2. 02Trying to get the latest row per group with MAX() plus other columns
  3. 03ORDER BY a column that is not grouped
  4. 04The joined code table has no PRIMARY KEY or UNIQUE key
  5. 05GROUP BY expression differs from the SELECT expression (DATE() vs DATE_FORMAT())

Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-12-01 · container image mysql:8.4

With ONLY_FULL_GROUP_BY, which is in the default sql_mode of MySQL 8.4, every column in SELECT, HAVING or ORDER BY must be in GROUP BY, be aggregated, or depend on the grouped columns (for example via a primary key). Otherwise you get 1055, and the message says which expression (#2 of SELECT list, #1 of ORDER BY clause) is the problem.

The message is in English even with Korean or Japanese lc_messages. Three cases verified on MySQL 8.4.

01An extra column in SELECT that is not in GROUP BY

GROUP BY customer_id with customer_name in SELECT fails: customer_name is a plain column of orders, and MySQL cannot assume it is the same for every row of a customer.

Setup
Setuprun as lab_a
CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, customer_name VARCHAR(30), amount INT);
INSERT INTO orders VALUES (1,10,'Kim',100),(2,10,'Kim',200),(3,20,'Lee',50);
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
SELECT customer_id, customer_name, SUM(amount) AS total
FROM orders GROUP BY customer_id;
✕ ERROR
ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'lab_a.orders.customer_name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
Checkrun as lab_a
SELECT @@sql_mode;
OUTPUT
+-----------------------------------------------------------------------------------------------------------------------+
| @@sql_mode                                                                                                            |
+-----------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

SELECT @@sql_mode shows ONLY_FULL_GROUP_BY is on. If the column has one value per group, add it to GROUP BY (GROUP BY customer_id, customer_name) or wrap it in ANY_VALUE(customer_name). Both return Kim 300 and Lee 50.

After the fixrun as lab_a
SELECT customer_id, customer_name, SUM(amount) AS total
FROM orders GROUP BY customer_id, customer_name;
SELECT customer_id, ANY_VALUE(customer_name) AS customer_name, SUM(amount) AS total
FROM orders GROUP BY customer_id;
✓ SUCCESS
+-------------+---------------+-------+
| customer_id | customer_name | total |
+-------------+---------------+-------+
|          10 | Kim           |   300 |
|          20 | Lee           |    50 |
+-------------+---------------+-------+
2 rows in set (0.00 sec)

+-------------+---------------+-------+
| customer_id | customer_name | total |
+-------------+---------------+-------+
|          10 | Kim           |   300 |
|          20 | Lee           |    50 |
+-------------+---------------+-------+
2 rows in set (0.00 sec)

02Trying to get the latest row per group with MAX() plus other columns

SELECT customer_id, MAX(order_date), amount ... GROUP BY customer_id looks like it returns the amount of the latest order, but amount is not tied to the MAX row, so MySQL rejects it (Expression #3).

Setup
Setuprun as lab_a
CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, order_date DATE, amount INT);
INSERT INTO orders VALUES (1,10,'2026-11-01',100),(2,10,'2026-11-20',200),(3,20,'2026-11-05',50),(4,20,'2026-11-03',70);
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
SELECT customer_id, MAX(order_date) AS last_date, amount
FROM orders GROUP BY customer_id;
✕ ERROR
ERROR 1055 (42000): Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'lab_a.orders.amount' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
Checkrun as lab_a
SELECT customer_id, MAX(order_date) AS last_date FROM orders GROUP BY customer_id;
OUTPUT
+-------------+------------+
| customer_id | last_date  |
+-------------+------------+
|          10 | 2026-11-20 |
|          20 | 2026-11-05 |
+-------------+------------+
2 rows in set (0.01 sec)

Number the rows per customer with ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC) and keep rn = 1. You get customer 10: 2026-11-20, 200 and customer 20: 2026-11-05, 50, the real latest orders.

After the fixrun as lab_a
SELECT customer_id, order_date AS last_date, amount FROM (
  SELECT customer_id, order_date, amount,
         ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC) AS rn
  FROM orders) t
WHERE rn = 1;
✓ SUCCESS
+-------------+------------+--------+
| customer_id | last_date  | amount |
+-------------+------------+--------+
|          10 | 2026-11-20 |    200 |
|          20 | 2026-11-05 |     50 |
+-------------+------------+--------+
2 rows in set (0.00 sec)

03ORDER BY a column that is not grouped

The SELECT list is fine, but ORDER BY order_date sorts by a column with several values per customer. The error says Expression #1 of ORDER BY clause.

Setup
Setuprun as lab_a
CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, order_date DATE, amount INT);
INSERT INTO orders VALUES (1,10,'2026-11-01',100),(2,10,'2026-11-20',200),(3,20,'2026-11-05',50);
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
SELECT customer_id, SUM(amount) AS total
FROM orders GROUP BY customer_id
ORDER BY order_date DESC;
✕ ERROR
ERROR 1055 (42000): Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'lab_a.orders.order_date' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
Checkrun as lab_a
SELECT customer_id, MAX(order_date) AS last_date, SUM(amount) AS total FROM orders GROUP BY customer_id;
OUTPUT
+-------------+------------+-------+
| customer_id | last_date  | total |
+-------------+------------+-------+
|          10 | 2026-11-20 |   300 |
|          20 | 2026-11-05 |    50 |
+-------------+------------+-------+
2 rows in set (0.00 sec)

Sort by an aggregate instead: ORDER BY MAX(order_date) DESC. The query returns 10 (300) then 20 (50), by each customer's latest order date.

After the fixrun as lab_a
SELECT customer_id, SUM(amount) AS total
FROM orders GROUP BY customer_id
ORDER BY MAX(order_date) DESC;
✓ SUCCESS
+-------------+-------+
| customer_id | total |
+-------------+-------+
|          10 |   300 |
|          20 |    50 |
+-------------+-------+
2 rows in set (0.00 sec)

04The joined code table has no PRIMARY KEY or UNIQUE key

A common report query: group by department code and show the department name. Even with GROUP BY d.dept_code, MySQL said d.dept_name was not in GROUP BY.

MySQL treats other columns as functionally dependent only when the GROUP BY column is a PRIMARY KEY or a NOT NULL UNIQUE key. depts had no key, so as far as MySQL knew the same code could appear twice.

Setup
Setuprun as lab_a
CREATE TABLE depts (dept_code VARCHAR(10), dept_name VARCHAR(30));
INSERT INTO depts VALUES ('D10','Sales'),('D20','IT');
CREATE TABLE emps (id INT PRIMARY KEY, dept_code VARCHAR(10), salary INT);
INSERT INTO emps VALUES (1,'D10',300),(2,'D10',400),(3,'D20',500);
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.02 sec)

Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
SELECT d.dept_code, d.dept_name, SUM(e.salary) AS total
FROM emps e JOIN depts d ON d.dept_code = e.dept_code
GROUP BY d.dept_code;
✕ ERROR
ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'lab_a.d.dept_name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
Check the causerun as lab_a
SELECT index_name, column_name, non_unique
FROM information_schema.statistics
WHERE table_schema = DATABASE() AND table_name = 'depts';
SELECT dept_code, COUNT(*) AS cnt FROM depts GROUP BY dept_code HAVING COUNT(*) > 1;
OUTPUT
Empty set (0.00 sec)

Empty set (0.00 sec)

depts has no index at all (the first query is an empty set) and no duplicated codes (the second query is empty too). The data is fine, but without a key MySQL cannot know that.

Fixrun as lab_a
ALTER TABLE depts ADD PRIMARY KEY (dept_code);
OUTPUT
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

Only ALTER TABLE depts ADD PRIMARY KEY (dept_code); was run, and the unchanged query then succeeded. Giving the code table a key fixes the cause, rather than adding the name to GROUP BY.

After the fixrun as lab_a
SELECT d.dept_code, d.dept_name, SUM(e.salary) AS total
FROM emps e JOIN depts d ON d.dept_code = e.dept_code
GROUP BY d.dept_code;
✓ SUCCESS
+-----------+-----------+-------+
| dept_code | dept_name | total |
+-----------+-----------+-------+
| D10       | Sales     |   700 |
| D20       | IT        |   500 |
+-----------+-----------+-------+
2 rows in set (0.00 sec)

05GROUP BY expression differs from the SELECT expression (DATE() vs DATE_FORMAT())

Daily sales were grouped with GROUP BY DATE(created_at) but displayed with DATE_FORMAT(created_at, '%Y-%m-%d'). They look like the same day, but MySQL sees two different expressions, so it reported that created_at is not in GROUP BY.

Setup
Setuprun as lab_a
CREATE TABLE orders (id INT PRIMARY KEY, created_at DATETIME, amount INT);
INSERT INTO orders VALUES (1,'2026-12-01 09:10:00',100),(2,'2026-12-01 15:30:00',200),(3,'2026-12-02 11:00:00',50);
OUTPUT
Query OK, 0 rows affected (0.01 sec)

Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
SELECT DATE_FORMAT(created_at, '%Y-%m-%d') AS day, SUM(amount) AS total
FROM orders
GROUP BY DATE(created_at);
✕ ERROR
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'lab_a.orders.created_at' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
Checkrun as lab_a
SELECT DATE(created_at) AS grouped_by, DATE_FORMAT(created_at, '%Y-%m-%d') AS selected FROM orders;
OUTPUT
+------------+------------+
| grouped_by | selected   |
+------------+------------+
| 2026-12-01 | 2026-12-01 |
| 2026-12-01 | 2026-12-01 |
| 2026-12-02 | 2026-12-02 |
+------------+------------+
3 rows in set (0.00 sec)

Querying both expressions side by side shows the same values, but MySQL compares expressions, not results. Grouping by the SELECT alias, GROUP BY day, returned 300 for 12-01 and 50 for 12-02.

After the fixrun as lab_a
SELECT DATE_FORMAT(created_at, '%Y-%m-%d') AS day, SUM(amount) AS total
FROM orders
GROUP BY day;
✓ SUCCESS
+------------+-------+
| day        | total |
+------------+-------+
| 2026-12-01 |   300 |
| 2026-12-02 |    50 |
+------------+-------+
2 rows in set (0.00 sec)

FAQ

How do I fix this is incompatible with sql_mode=only_full_group_by?

Add the column to GROUP BY, wrap it in an aggregate such as MAX() or ANY_VALUE(), or rewrite with a window function when you need a specific row.

How do I check if ONLY_FULL_GROUP_BY is enabled?

Run SELECT @@sql_mode;. On MySQL 8.4 the default value starts with ONLY_FULL_GROUP_BY.

How do I select the latest row per group in MySQL 8?

Use ROW_NUMBER() OVER (PARTITION BY group_col ORDER BY date_col DESC) in a subquery and filter rn = 1.

What does Expression #2 of SELECT list mean?

It is the position of the problem column in your SELECT list (counting from 1). ORDER BY problems are reported as Expression #N of ORDER BY clause.

I group by the joined table's code column and still get 1055. Why?

Check that the GROUP BY column has a PRIMARY KEY or NOT NULL UNIQUE key in that table. In the run above, the code table had no key, and adding a PRIMARY KEY made the same query succeed.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary users created for the test, each with full rights on a database of the same name (its default database); root is the administrator. MySQL 8.4.13