GROUP BY Single and Multiple Columns in PostgreSQL
📖 Scenario: You are working with a sales database for a small bookstore. The database has a table called sales that records each book sale with the book's title, the genre, and the number of copies sold.Your task is to learn how to group sales data by single and multiple columns to find total copies sold per book and per genre.
🎯 Goal: Build SQL queries using GROUP BY to calculate total copies sold grouped by a single column (title) and by multiple columns (genre and title).
📋 What You'll Learn
Create a
sales table with columns title (text), genre (text), and copies_sold (integer).Insert specific sales data into the
sales table.Write a SQL query to group total copies sold by
title.Write a SQL query to group total copies sold by both
genre and title.💡 Why This Matters
🌍 Real World
Grouping sales data helps businesses understand which books or genres sell best, aiding inventory and marketing decisions.
💼 Career
Database professionals often write GROUP BY queries to summarize and analyze data for reports and dashboards.
Progress0 / 4 steps