Bird
0
0

Why does this query cause an error?

medium📝 Debug Q7 of 15
PostgreSQL - Subqueries in PostgreSQL
Why does this query cause an error?

SELECT u.id, o.amount FROM users u, LATERAL (SELECT amount FROM orders WHERE user_id = u.id) o WHERE o.amount > 100;
AFiltering on LATERAL subquery column in WHERE causes error
BLATERAL cannot be used with comma joins
CSubquery must be aliased differently
DNo error, query is valid
Step-by-Step Solution
Solution:
  1. Step 1: Understand WHERE filtering on LATERAL

    Filtering on a column from LATERAL subquery in WHERE clause can cause errors if subquery returns no rows (NULL).
  2. Step 2: Analyze the error cause

    Because LATERAL subquery may return no rows, filtering in WHERE excludes those rows, causing unexpected behavior or error.
  3. Final Answer:

    Filtering on LATERAL subquery column in WHERE causes error -> Option A
  4. Quick Check:

    Filter on LATERAL output in WHERE can cause errors [OK]
Quick Trick: Filter LATERAL results in JOIN ON, not WHERE [OK]
Common Mistakes:
  • Using WHERE to filter LATERAL subquery columns
  • Thinking LATERAL disallows comma joins
  • Confusing aliasing rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes