Filtering Groups with HAVING in PostgreSQL
📖 Scenario: You are managing a small bookstore database. You want to find authors who have sold more than a certain number of books in total.
🎯 Goal: Build a query that groups sales by author and uses HAVING to filter authors with total sales above a threshold.
📋 What You'll Learn
Create a table called
book_sales with columns author (text) and copies_sold (integer).Insert the exact sales data provided.
Create a variable
min_sales to set the minimum total copies sold to filter authors.Write a query that groups sales by
author and sums copies_sold.Use
HAVING to filter groups where total copies sold is greater than min_sales.💡 Why This Matters
🌍 Real World
Filtering grouped data is common in sales reports, analytics, and business intelligence to find top performers or categories.
💼 Career
Understanding how to use HAVING helps database analysts and developers write efficient queries that summarize and filter grouped data.
Progress0 / 4 steps