Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

Aggregate functions are not allowed in WHERE: error 1111 and how to rewrite

ErrorERROR 1111 (HY000): Invalid use of group function

Short answer

WHERE filters rows before they are grouped, so it cannot contain COUNT(), SUM(), MAX() and other aggregate functions; MySQL answers with error 1111. Move the test to HAVING when it is about a group, or compare against a scalar subquery when it is about every row.

  • Applies to MySQL 8.0, MySQL 8.4 LTS
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find the aggregate call (COUNT, SUM, AVG, MIN, MAX) in your WHERE clause, and decide whether you are filtering groups or single rows.

    grep -n -i -E "where.*(count|sum|avg|min|max)\(" query.sql
  2. 2Match what you see, then jump to its fix

    1. 1WHERE ... COUNT(*) > 1 or WHERE SUM(total) > 100 with a GROUP BY.A group condition was put in WHERE instead of HAVINGFix
    2. 2WHERE price = MAX(price), or WHERE salary > AVG(salary), with no GROUP BY.The condition compares each row with an aggregate of the whole tableFix
    3. 3The error says Unknown column 'cnt' in 'where clause' after an alias was defined as COUNT(*) AS cnt.A column alias of an aggregate was used in WHEREFix

What does the error look like?

  • ERROR 1111 (HY000): Invalid use of group function
  • ERROR 1054 (42S22): Unknown column 'cnt' in 'where clause'

Cause 1 of 3

A group condition was put in WHERE instead of HAVING

1Check

WHERE can use any function except aggregate (group) functions. HAVING holds conditions on groups, formed by GROUP BY, and is applied nearly last.

SELECT customer_id, COUNT(*) AS cnt
FROM orders
WHERE COUNT(*) > 1
GROUP BY customer_id;

2Fix

Keep row filters in WHERE and move the aggregate test to HAVING.

SELECT customer_id, COUNT(*) AS cnt
FROM orders
WHERE status = 'paid'
GROUP BY customer_id
HAVING COUNT(*) > 1;

3Verify

The query returns one row per customer with more than one paid order.

-- run the corrected query above

Confirmed by dev.mysql.com

Did this fix it?

Cause 2 of 3

The condition compares each row with an aggregate of the whole table

1Check

There is no group to apply HAVING to; you want every row compared with one computed value. MySQL's manual shows this as a scalar subquery comparison.

SELECT * FROM products WHERE price = MAX(price);

2Fix

Compute the aggregate in a subquery that returns a single value.

SELECT * FROM products
WHERE price = (SELECT MAX(price) FROM products);

3Verify

The rows returned all have the highest price.

SELECT MAX(price) FROM products;

Confirmed by dev.mysql.com

Did this fix it?

Cause 3 of 3

A column alias of an aggregate was used in WHERE

1Check

Standard SQL disallows column aliases in WHERE, because the value is not known until after rows are selected and grouped. MySQL lets GROUP BY, ORDER BY and HAVING use the alias.

SELECT id, COUNT(*) AS cnt FROM tbl_name WHERE cnt > 0 GROUP BY id;

2Fix

Use the alias in HAVING.

SELECT id, COUNT(*) AS cnt FROM tbl_name
GROUP BY id HAVING cnt > 0;

3Verify

The query runs without an unknown-column error.

-- run the corrected query above

Confirmed by dev.mysql.com

Did this fix it?

How do I stop it coming back?

  • Read WHERE as 'which rows go in' and HAVING as 'which groups come out'.
  • Keep non-aggregate filters in WHERE so fewer rows reach the grouping step.
  • Compute table-wide values in a subquery and compare against that.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.