2201WERRORTier 2 — Caution✅ HIGH confidenceinvalid row count in LIMIT clause
Category: Data ExceptionVersions: All Postgres versions
What this means
SQLSTATE 2201W is raised when a LIMIT clause specifies a row count that is negative. A LIMIT of zero is valid (returns no rows), but negative values are not permitted.
Why it happens
- 1Using a negative integer or a computed negative expression as the LIMIT argument
How to reproduce
Query with a negative LIMIT.
trigger — this will ERROR
SELECT * FROM orders LIMIT -1;ERROR: LIMIT must not be negative
Fix 1: Clamp LIMIT to zero or above
When the LIMIT value is computed dynamically.
fix
SELECT * FROM orders LIMIT GREATEST(computed_limit, 0);Why this works
GREATEST ensures the limit is never negative. Use LIMIT ALL or omit LIMIT to return all rows when no limit is intended.
Sources
📚 Official docs: https://www.postgresql.org/docs/current/errcodes-appendix.html
🔧 Source ref: Class 22 — Data Exception
Confidence assessment
✅ HIGH confidence
Standard SQLSTATE for LIMIT validation. Stable across versions.
See also
📄 Reference pages
SELECTLIMIT Clause
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →