MySQL 8.4Lab-verified · 2026-11-29

MySQL ERROR 1111 (HY000): Invalid use of group function

An aggregate function (SUM, MAX, COUNT...) is used where MySQL cannot evaluate it.

MySQL 8.4.13
1111 (HY000): Invalid use of group function

Jump to a cause

  1. 01SUM() in WHERE instead of HAVING
  2. 02Nested aggregate: MAX(SUM(...))
  3. 03UPDATE ... WHERE price = MAX(price)

Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-11-29 · container image mysql:8.4

Aggregate functions such as SUM, MAX and COUNT are computed per group, after rows have been filtered. MySQL raises ERROR 1111 when an aggregate is placed somewhere it cannot be computed: in WHERE, inside another aggregate, or in the WHERE of an UPDATE.

The three runs below each failed with the same message and were fixed by moving the aggregate: to HAVING, into a derived table, or into a joined subquery.

01SUM() in WHERE instead of HAVING

... WHERE SUM(amount) >= 100 GROUP BY customer_id failed with ERROR 1111. WHERE filters single rows before grouping, so the sum does not exist yet.

Without the condition, customer 1 totalled 150.00 and customer 2 70.00. Moving the condition to HAVING SUM(amount) >= 100 returned only customer 1.

Setup
Setuprun as lab_a
CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, amount DECIMAL(10,2));
INSERT INTO orders VALUES (1, 1, 100.00), (2, 1, 50.00), (3, 2, 70.00);
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 WHERE SUM(amount) >= 100 GROUP BY customer_id;
✕ ERROR
ERROR 1111 (HY000): Invalid use of group function
Checkrun as lab_a
SELECT customer_id, SUM(amount) AS total FROM orders GROUP BY customer_id;
OUTPUT
+-------------+--------+
| customer_id | total  |
+-------------+--------+
|           1 | 150.00 |
|           2 |  70.00 |
+-------------+--------+
2 rows in set (0.00 sec)
After the fixrun as lab_a
SELECT customer_id, SUM(amount) AS total FROM orders GROUP BY customer_id HAVING SUM(amount) >= 100;
✓ SUCCESS
+-------------+--------+
| customer_id | total  |
+-------------+--------+
|           1 | 150.00 |
+-------------+--------+
1 row in set (0.00 sec)

02Nested aggregate: MAX(SUM(...))

SELECT MAX(SUM(amount)) FROM orders GROUP BY customer_id failed with ERROR 1111. MySQL does not allow an aggregate inside another aggregate.

Computing the sums in a derived table first and taking MAX(total) from it — FROM (SELECT customer_id, SUM(amount) AS total ... GROUP BY customer_id) AS t — returned 150.00.

Setup
Setuprun as lab_a
CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, amount DECIMAL(10,2));
INSERT INTO orders VALUES (1, 1, 100.00), (2, 1, 50.00), (3, 2, 70.00);
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 MAX(SUM(amount)) AS best FROM orders GROUP BY customer_id;
✕ ERROR
ERROR 1111 (HY000): Invalid use of group function
Checkrun as lab_a
SELECT customer_id, SUM(amount) AS total FROM orders GROUP BY customer_id;
OUTPUT
+-------------+--------+
| customer_id | total  |
+-------------+--------+
|           1 | 150.00 |
|           2 |  70.00 |
+-------------+--------+
2 rows in set (0.00 sec)
After the fixrun as lab_a
SELECT MAX(total) AS best FROM (SELECT customer_id, SUM(amount) AS total FROM orders GROUP BY customer_id) AS t;
✓ SUCCESS
+--------+
| best   |
+--------+
| 150.00 |
+--------+
1 row in set (0.00 sec)

03UPDATE ... WHERE price = MAX(price)

UPDATE products SET is_top = 1 WHERE price = MAX(price) failed with ERROR 1111.

The maximum price was 900.00. Joining a subquery that computes it — UPDATE products p JOIN (SELECT MAX(price) AS max_price FROM products) m ON p.price = m.max_price SET p.is_top = 1 — changed 1 row, and only product 2 (900.00) got is_top = 1.

Setup
Setuprun as lab_a
CREATE TABLE products (id INT PRIMARY KEY, price DECIMAL(10,2), is_top TINYINT DEFAULT 0);
INSERT INTO products (id, price) VALUES (1, 500.00), (2, 900.00), (3, 300.00);
OUTPUT
Query OK, 0 rows affected (0.01 sec)

Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
UPDATE products SET is_top = 1 WHERE price = MAX(price);
✕ ERROR
ERROR 1111 (HY000): Invalid use of group function
Checkrun as lab_a
SELECT MAX(price) AS max_price FROM products;
OUTPUT
+-----------+
| max_price |
+-----------+
|    900.00 |
+-----------+
1 row in set (0.00 sec)
After the fixrun as lab_a
UPDATE products p JOIN (SELECT MAX(price) AS max_price FROM products) m ON p.price = m.max_price SET p.is_top = 1;
SELECT * FROM products ORDER BY id;
✓ SUCCESS
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

+----+--------+--------+
| id | price  | is_top |
+----+--------+--------+
|  1 | 500.00 |      0 |
|  2 | 900.00 |      1 |
|  3 | 300.00 |      0 |
+----+--------+--------+
3 rows in set (0.00 sec)

FAQ

How do I fix Invalid use of group function?

Move the aggregate to where it can be computed: conditions on SUM/COUNT go in HAVING, nested aggregates go through a derived table, and UPDATEs join a subquery that computes the value.

What is the difference between WHERE and HAVING?

WHERE filters rows before GROUP BY; HAVING filters groups after the aggregates are computed. That is why HAVING SUM(amount) >= 100 worked and the WHERE version did not.

Can I use MAX(SUM(x)) in MySQL?

No, it fails with ERROR 1111. Put the SUM in a derived table and take MAX of it, as in the second case.

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