GROUP BY with multiple columns in MySQL
📖 Scenario: You work for a small bookstore that wants to analyze its sales data. The store records each sale with the book's genre, the month of sale, and the number of copies sold.The manager wants to see the total copies sold grouped by both genre and month to understand which genres sell best in which months.
🎯 Goal: Create a MySQL query that groups sales data by both genre and month, and calculates the total copies sold for each group.
📋 What You'll Learn
Create a table called
book_sales with columns genre (VARCHAR), month (VARCHAR), and copies_sold (INT).Insert the exact sales data provided into the
book_sales table.Write a SELECT query that groups the data by
genre and month.Calculate the total copies sold for each group using
SUM(copies_sold).Order the results by
genre ascending and month ascending.💡 Why This Matters
🌍 Real World
Grouping sales data by multiple columns helps businesses analyze trends across different categories and time periods.
💼 Career
Understanding GROUP BY with multiple columns is essential for data analysis roles, report generation, and database querying in many jobs.
Progress0 / 4 steps