3288
MariaDBERRORNotableCTEsHIGH confidence

Recursive CTE recursive member cannot use aggregation

Production Risk

Low — query is rejected.

How to reproduce
trigger — this will error
trigger — this will error
WITH RECURSIVE cte AS (SELECT 1 AS n UNION ALL SELECT COUNT(*) FROM cte WHERE n < 10) SELECT * FROM cte;

expected output

ERROR 3288 (HY000): Recursive CTE 'cte' cannot contain aggregation in recursive query block.

Fix

Remove aggregation from recursive part

Remove aggregation from recursive part
WITH RECURSIVE cte AS (SELECT 1 AS n UNION ALL SELECT n+1 FROM cte WHERE n < 10) SELECT COUNT(*) FROM cte;

Why this works

Aggregate the final result set in the outer query, not inside the recursive member.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 3288 ER_CTE_RECURSIVE_FORBIDS_AGGREGATION

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

← All MariaDB errors