Bird
0
0

In PostgreSQL 11, you have a query with nested CTEs and subqueries. Which strategy best balances readability and performance?

hard📝 optimization Q9 of 15
PostgreSQL - Common Table Expressions
In PostgreSQL 11, you have a query with nested CTEs and subqueries. Which strategy best balances readability and performance?
AUse only subqueries and never CTEs.
BUse subqueries for small, frequently filtered parts and CTEs for complex reusable parts.
CAvoid subqueries entirely and use only CTEs.
DConvert all subqueries to CTEs for consistency.
Step-by-Step Solution
Solution:
  1. Step 1: Consider performance traits

    Subqueries optimize better but can be harder to read if complex.
  2. Step 2: Consider readability and reuse

    CTEs improve readability and reuse but may slow queries due to materialization.
  3. Final Answer:

    Use subqueries for small, frequently filtered parts and CTEs for complex reusable parts. -> Option B
  4. Quick Check:

    Balance readability and performance with mixed usage [OK]
Quick Trick: Mix subqueries and CTEs for best balance [OK]
Common Mistakes:
  • Converting all subqueries to CTEs blindly
  • Avoiding subqueries completely
  • Using only one approach regardless of context

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes