Using the LEAD Function to Access Next Row Data
📖 Scenario: You work in a sales department and have a table of monthly sales figures for different products. You want to compare each month's sales with the next month's sales to see how sales are changing over time.
🎯 Goal: Build a SQL query that uses the LEAD function to show each product's sales for a month and the sales for the next month side by side.
📋 What You'll Learn
Create a table called
monthly_sales with columns product (text), month (integer), and sales (integer).Insert the exact data rows specified.
Write a query that selects
product, month, sales, and the next month's sales using the LEAD function.Order the results by
product and month.💡 Why This Matters
🌍 Real World
Sales analysts often need to compare current and future sales data to identify trends and make forecasts.
💼 Career
Knowing how to use window functions like LEAD is valuable for data analysts and database developers to write efficient and insightful queries.
Progress0 / 4 steps