Bird
0
0

What is wrong with this CTE usage?

medium📝 Debug Q7 of 15
PostgreSQL - Common Table Expressions
What is wrong with this CTE usage?
WITH cte AS (SELECT * FROM orders) SELECT * FROM cte WHERE order_date > '2023-01-01' AND cte.customer_id;
AThe condition 'cte.customer_id' is incomplete and invalid
BCTE cannot be used with WHERE clause
CMissing parentheses around WHERE conditions
DCTE name conflicts with table name
Step-by-Step Solution
Solution:
  1. Step 1: Analyze WHERE clause conditions

    The condition 'cte.customer_id' alone is invalid; it needs a comparison or boolean expression.
  2. Step 2: Confirm CTE usage is valid

    CTEs can be used with WHERE clauses; no parentheses are required around conditions, and no naming conflict is indicated.
  3. Final Answer:

    The condition 'cte.customer_id' is incomplete and invalid -> Option A
  4. Quick Check:

    WHERE conditions must be complete expressions = A [OK]
Quick Trick: WHERE conditions need full boolean expressions [OK]
Common Mistakes:
  • Thinking CTEs can't have WHERE clauses
  • Assuming parentheses are mandatory around WHERE
  • Confusing CTE name with table name conflicts

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes