Using the OVER Clause with PARTITION BY in SQL
📖 Scenario: You work for a retail company that wants to analyze sales data. The company wants to see the total sales amount for each salesperson, but also wants to keep the details of each sale.
🎯 Goal: Build an SQL query that uses the OVER clause with PARTITION BY to calculate the total sales amount for each salesperson alongside each individual sale record.
📋 What You'll Learn
Create a table called
sales with columns sale_id, salesperson, and amount.Insert the exact sales data provided.
Write a query that selects all columns and adds a new column
total_sales which shows the total sales amount per salesperson using SUM(amount) OVER (PARTITION BY salesperson).💡 Why This Matters
🌍 Real World
Sales teams often need to see individual sales along with total sales per person to understand performance.
💼 Career
Knowing how to use window functions like OVER with PARTITION BY is valuable for data analysts and database developers to write efficient and insightful queries.
Progress0 / 4 steps