Syntax Error
Cannot combine LATERAL derived tables and common table expressions
What This Error Means
This error occurs when you try to combine a LATERAL derived table with a common table expression (CTE) in a single query. This is not allowed in SQL because it can lead to ambiguous joins and unpredictable results.
Why It Happens
This error typically happens when a developer tries to use LATERAL derived tables and CTEs together without realizing the SQL syntax restrictions. It can also occur when a complex query is rewritten without properly considering the SQL syntax rules.
How to Fix It
- 1To fix this error, you can refactor the query to use either LATERAL derived tables or CTEs, but not both in the same query. Alternatively, you can break the query into multiple subqueries to achieve the desired result. For example:
Example Code Solution
❌ Before (problematic code)
SQL
WITH cte AS (
SELECT * FROM table1
)
SELECT * FROM table2, LATERAL (
SELECT * FROM table3
) as lateral_table;✅ After (fixed code)
SQL
WITH cte AS (
SELECT * FROM table1
)
SELECT * FROM table2
JOIN (
SELECT * FROM table3
) as lateral_table ON table2.id = lateral_table.id;Fix for Cannot combine LATERAL derived tables and common table expressions
Browse Related Clusters
Related SQL Errors
Related SQL Blog Articles
Have a different error? Get an instant explanation.
Explain Another Error