3193
MariaDBERRORNotableQuery SyntaxHIGH confidence

Invalid use of aggregate function

Production Risk

Low — caught at parse time.

Why it happens
  1. 1Using an aggregate function in a context where it is not allowed.
  2. 2Nesting aggregate functions without proper subquery structure.
How to reproduce
trigger — this will error
trigger — this will error
SELECT SUM(SUM(val)) FROM t1 GROUP BY id;

expected output

ERROR 3193 (HY000): Invalid use of aggregate function.

Fix

Use a subquery for nested aggregates

Use a subquery for nested aggregates
SELECT SUM(subtotal) FROM (SELECT SUM(val) AS subtotal FROM t1 GROUP BY id) sub;

Why this works

Aggregate functions cannot be directly nested; use a derived table.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 3193 ER_INVALID_AGGREGATE_FUNCTION

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MariaDB errors