Bird
0
0

Why does this query cause an error?

medium📝 Debug Q7 of 15
PostgreSQL - Joins in PostgreSQL
Why does this query cause an error?
SELECT a.id, b.val FROM a JOIN LATERAL (SELECT val FROM b WHERE b.a_id = a.id) ON true;
ACorrelated subquery cannot reference outer table
BMissing alias for the LATERAL subquery
CTable b is not defined in FROM clause
DUsing ON true without alias is invalid
Step-by-Step Solution
Solution:
  1. Step 1: Check LATERAL subquery aliasing

    The subquery after LATERAL must have an alias to be valid.
  2. Step 2: Confirm error cause

    Query lacks alias after subquery, causing syntax error despite ON true.
  3. Final Answer:

    Missing alias for the LATERAL subquery -> Option B
  4. Quick Check:

    LATERAL subquery must have alias even with ON true [OK]
Quick Trick: Alias every LATERAL subquery to avoid syntax errors [OK]
Common Mistakes:
  • Forgetting alias after LATERAL subquery
  • Assuming ON true fixes missing alias
  • Thinking correlated subqueries can't reference outer tables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes