Understanding Window Functions vs GROUP BY in SQL
📖 Scenario: You work at a bookstore that tracks sales data. You want to analyze sales by each book and also see the total sales across all books. You will learn how to use GROUP BY to get totals per book and how to use window functions to get totals alongside each row.
🎯 Goal: Build SQL queries that first group sales by book using GROUP BY, then use a window function to show total sales per book and overall total sales in the same result.
📋 What You'll Learn
Create a table called
sales with columns book (text) and copies_sold (integer) with given dataWrite a
GROUP BY query to get total copies sold per bookAdd a variable to hold the overall total copies sold
Write a query using a window function
SUM() OVER() to show total copies sold per book and overall total on each row💡 Why This Matters
🌍 Real World
Bookstores and many businesses analyze sales data to understand product performance and overall revenue.
💼 Career
Knowing when to use GROUP BY versus window functions is key for data analysts and database developers to write efficient and insightful queries.
Progress0 / 4 steps