Bird
0
0

You want to calculate the total sales per customer and then find customers with sales over 1000. Which approach using CTEs is best?

hard📝 Application Q8 of 15
PostgreSQL - Common Table Expressions
You want to calculate the total sales per customer and then find customers with sales over 1000. Which approach using CTEs is best?
AUse a CTE to sum sales per customer, then filter in the main query
BWrite a single query without CTEs using nested subqueries
CCreate a temporary table instead of a CTE
DUse multiple CTEs without filtering
Step-by-Step Solution
Solution:
  1. Step 1: Understand the problem requirements

    You need to aggregate sales per customer and then filter those with totals over 1000.
  2. Step 2: Evaluate CTE usage for clarity and efficiency

    Using a CTE to calculate sums first, then filtering in the main query, makes the query clear and maintainable.
  3. Final Answer:

    Use a CTE to sum sales per customer, then filter in the main query -> Option A
  4. Quick Check:

    CTEs help separate aggregation and filtering = A [OK]
Quick Trick: Use CTEs to separate calculation and filtering steps [OK]
Common Mistakes:
  • Overcomplicating with multiple CTEs
  • Avoiding CTEs and writing nested subqueries
  • Using temporary tables unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes