Using Multiple CTEs in One PostgreSQL Query
📖 Scenario: You work in a small bookstore's database team. The store wants to analyze its sales data to find the total sales per author and then identify authors with sales above a certain threshold.
🎯 Goal: Build a PostgreSQL query using multiple Common Table Expressions (CTEs) to first calculate total sales per book, then total sales per author, and finally select authors with sales above a threshold.
📋 What You'll Learn
Create a CTE named
book_sales that sums sales per book.Create a second CTE named
author_sales that sums sales per author using book_sales.Use a final SELECT statement to get authors with total sales greater than a threshold.
Use exact CTE names and column names as specified.
💡 Why This Matters
🌍 Real World
Bookstores and many businesses use multiple CTEs to organize complex queries for reporting and analysis.
💼 Career
Knowing how to write multiple CTEs is important for data analysts and database developers to write clear and efficient SQL queries.
Progress0 / 4 steps