Using FILTER Clause for Conditional Aggregation in PostgreSQL
📖 Scenario: You are working with a sales database for a small store. The store wants to analyze sales data to see how many items were sold in different categories and how many sales were made on weekends.
🎯 Goal: Build a SQL query that uses the FILTER clause to count sales conditionally based on product category and day of the week.
📋 What You'll Learn
Create a table called
sales with columns id (integer), category (text), amount (integer), and sale_date (date).Insert the exact rows provided into the
sales table.Write a query that counts total sales and counts sales only for the category 'Books' using the
FILTER clause.Add a conditional count for sales made on weekends using the
FILTER clause.💡 Why This Matters
🌍 Real World
Stores and businesses often want to analyze sales data by different conditions, such as product category or day of the week, to make better decisions.
💼 Career
Knowing how to use conditional aggregation with FILTER clauses is useful for data analysts and database developers to write efficient and clear SQL queries.
Progress0 / 4 steps