Using GROUPING SETS for Multiple Groupings in PostgreSQL
📖 Scenario: You are working as a data analyst for a retail company. The company wants to analyze sales data to understand revenue by different combinations of product categories and regions.
🎯 Goal: Build a PostgreSQL query using GROUPING SETS to get total sales grouped by category, by region, and by both category and region together.
📋 What You'll Learn
Create a table named
sales with columns category (text), region (text), and amount (numeric).Insert the exact sales data provided into the
sales table.Write a query using
GROUPING SETS to group sales by category, by region, and by both category and region.Include a column named
total_sales that sums the amount for each grouping.💡 Why This Matters
🌍 Real World
Retail companies often analyze sales data by different dimensions like product categories and regions to make better business decisions.
💼 Career
Data analysts and database developers use GROUPING SETS to efficiently generate multiple grouped summaries in a single query, saving time and resources.
Progress0 / 4 steps