Bird
0
0

Find the error in this SQL snippet:

medium📝 Debug Q7 of 15
SQL - Common Table Expressions (CTEs)
Find the error in this SQL snippet:
WITH cte AS (SELECT id, name FROM users) SELECT * FROM cte WHERE id = (SELECT id FROM cte WHERE name = 'Alice');
ANo error, query is valid
BCTE cannot be referenced inside its own definition
CMissing alias for subquery
DSubquery inside WHERE cannot reference CTE
Step-by-Step Solution
Solution:
  1. Step 1: Understand CTE scope

    CTE is defined once and can be referenced multiple times in the main query.
  2. Step 2: Analyze subquery usage

    The subquery inside WHERE references the CTE, which is allowed in the main query scope.
  3. Final Answer:

    No error, query is valid -> Option A
  4. Quick Check:

    CTE can be referenced multiple times [OK]
Quick Trick: CTEs can be reused multiple times in main query [OK]
Common Mistakes:
  • Thinking CTE can't be referenced multiple times
  • Assuming subqueries can't use CTE
  • Expecting alias on scalar subquery

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes