Using NTILE to Distribute Sales Data into Quartiles
📖 Scenario: You work for a retail company that wants to analyze its sales data. The company wants to divide its salespeople into four groups (quartiles) based on their total sales to understand performance distribution.
🎯 Goal: Create a PostgreSQL query that uses the NTILE window function to split salespeople into four groups based on their total sales.
📋 What You'll Learn
Create a table called
sales with columns salesperson (text) and total_sales (integer).Insert the exact sales data for five salespeople:
'Alice' with 5000, 'Bob' with 7000, 'Charlie' with 3000, 'Diana' with 9000, and 'Evan' with 6000.Write a query that selects
salesperson, total_sales, and a quartile number using NTILE(4) ordered by total_sales descending.Name the quartile column
sales_quartile.💡 Why This Matters
🌍 Real World
Companies often want to segment employees or customers into groups based on performance or behavior to target rewards or marketing.
💼 Career
Understanding window functions like NTILE is important for data analysts and database developers to perform advanced data segmentation and reporting.
Progress0 / 4 steps