Sub topic 11
Syntax and Limits Across PostgreSQL, MySQL, SQLite, and SQL Server
Assume you know SELECT, WHERE, ORDER BY, JOIN, and basic common table expressions (CTEs). The recursive pattern is similar across major SQL systems, but the keyword and safety limit differ.
The portable shape
A recursive CTE has two parts:
- The anchor member returns the starting rows.
- The recursive member refers to the CTE and returns the next rows.
The two parts are joined with UNION ALL. The recursive member must eventually return no rows.
PostgreSQL, MySQL, and SQLite require the RECURSIVE keyword:
WITH RECURSIVE numbers(i) AS (
SELECT 1
UNION ALL
SELECT i + 1
FROM numbers
WHERE i < 5
)
SELECT i
FROM numbers;This returns the numbers 1 through 5. The query works in all three systems because they use the same basic syntax.
SQL Server omits RECURSIVE:
WITH numbers(i) AS (
SELECT 1
UNION ALL
SELECT i + 1
FROM numbers
WHERE i < 5
)
SELECT i
FROM numbers
OPTION (MAXRECURSION 100);The semicolon before WITH is often needed in SQL Server when another statement comes before the CTE. The MAXRECURSION option belongs to the final SELECT, not inside the CTE.
Recursion limits
A termination condition is part of the query's logic. A recursion limit is an additional safety guard supplied by the database or query.
| Database | Keyword | Typical recursion safeguard |
|---|---|---|
| PostgreSQL | `WITH RECURSIVE` | No fixed recursive-CTE count; use a stopping condition and statement or resource limits |
| MySQL 8.0+ | `WITH RECURSIVE` | `cte_max_recursion_depth`, commonly 1000 |
| SQLite | `WITH RECURSIVE` | No `MAXRECURSION` query clause; use a stopping condition or `LIMIT` |
| SQL Server | `WITH` | `MAXRECURSION`, default 100, maximum 32,767, or 0 for no configured limit |
MySQL's limit can be changed for a session:
SET SESSION cte_max_recursion_depth = 5000;
WITH RECURSIVE numbers(i) AS (
SELECT 1
UNION ALL
SELECT i + 1
FROM numbers
WHERE i < 3000
)
SELECT i
FROM numbers;A larger limit does not fix a faulty recursive query. It only permits more iterations.
SQLite queries can add a row limit as a second guard:
WITH RECURSIVE numbers(i) AS (
SELECT 1
UNION ALL
SELECT i + 1
FROM numbers
WHERE i < 1000000
LIMIT 1000
)
SELECT i
FROM numbers;The LIMIT limits rows added by the recursive CTE. Use it carefully because it can stop a query before the logical condition reaches its intended result.
Choosing a portable strategy
For code that runs on several databases:
- Keep the anchor and recursive member simple.
- Use an explicit
WHEREcondition based on depth, date, or value. - Add a path check when traversing a graph.
- Apply each database's separate safety setting where appropriate.
- Test the limit behavior with a deliberately small example.
Exercise
Which syntax change is required when moving a recursive CTE from PostgreSQL to SQL Server, and what SQL Server option can limit its depth?
Show answer
Remove the RECURSIVE keyword after WITH, and add OPTION (MAXRECURSION n) to the final query when a depth limit is needed.