Bird
0
0

Given the query:

medium📝 query result Q13 of 15
PostgreSQL - Common Table Expressions
Given the query:
WITH cte AS (SELECT id, name FROM users WHERE active = true) SELECT * FROM cte WHERE id < 5;

What will this query return?
AAll users with id less than 5 regardless of active status
BAll users with active = true and id less than 5
CAll users with active = false and id less than 5
DSyntax error due to incorrect WHERE clause
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the CTE definition

    The CTE named 'cte' selects users where active = true, so only active users are included.
  2. Step 2: Analyze the main query filtering

    The main query selects from 'cte' where id < 5, so it filters active users with id less than 5.
  3. Final Answer:

    All users with active = true and id less than 5 -> Option B
  4. Quick Check:

    CTE filters active=true, main query filters id<5 [OK]
Quick Trick: CTE filters first, main query filters second [OK]
Common Mistakes:
  • Ignoring CTE filter and only applying main WHERE
  • Assuming all users regardless of active status
  • Thinking syntax error due to WHERE

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes