Bird
0
0

You wrote this SQL query but it throws an error:

medium📝 Debug Q14 of 15
SQL - Common Table Expressions (CTEs)
You wrote this SQL query but it throws an error:
SELECT * FROM (
  SELECT id, name FROM users
) AS user_sub WHERE user_sub.age > 30;
What is the most likely cause of the error?
AThe subquery alias 'user_sub' is missing
BThe WHERE clause should be inside the subquery
CThe subquery does not include the 'age' column needed in WHERE
DSubqueries cannot be used in FROM clause
Step-by-Step Solution
Solution:
  1. Step 1: Check subquery columns

    The subquery selects only 'id' and 'name' columns, but the outer query filters on 'age' which is not selected.
  2. Step 2: Understand SQL column visibility

    Since 'age' is not in the subquery result, the outer WHERE clause referencing 'age' causes an error.
  3. Final Answer:

    The subquery does not include the 'age' column needed in WHERE -> Option C
  4. Quick Check:

    Missing column in subquery causes error [OK]
Quick Trick: Ensure subquery selects all columns used outside [OK]
Common Mistakes:
  • Forgetting to alias subquery
  • Thinking subqueries can't be in FROM
  • Moving WHERE inside subquery unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes