GROUP BY and HAVING
Explore how to use SQL GROUP BY to summarize data by categories and HAVING to filter these grouped results. Understand the differences between WHERE and HAVING filters, and practice writing queries that reveal meaningful business insights like departmental salaries and employee counts. This lesson equips you to perform grouped data analysis and conditional filtering with confidence.
Step 1: Start with a question
In the last lesson, you asked: What is the total salary cost for the whole company?
Now, let’s ask something smarter: “What’s the total salary cost for each department?” That’s where GROUP BY comes in; it summarizes data by group, not as a single total.
Prompt: Write a SQL query that shows each department and its total salary cost from the employees table.
Step 2: Run and observe
AI will likely produce:
Run it. This is your first grouped ...