If you’re preparing for a data analyst role, mastering GROUP BY and HAVING is non-negotiable.
These two clauses appear in almost every SQL interview and are heavily used in real business reporting.
Let’s break them down in the simplest way possible.
What Is GROUP BY in SQL?
GROUP BY is used to group rows that have the same values in specified columns.
It is usually combined with aggregate functions like:
COUNT()SUM()AVG()MIN()MAX()
Simple Example
Suppose you have a sales table.
You want to know total sales per country:
SELECT country, SUM(amount) AS total_sales
FROM sales
GROUP BY country;
What happens here?
- SQL groups rows by country.
- It calculates the sum for each group.
- It returns one row per country.
Important:
Once you use GROUP BY, every column in SELECT must either:
- Be in the GROUP BY clause, or
- Be inside an aggregate function.
GROUP BY with COUNT Example
Count the number of customers per city:
SELECT city, COUNT(*) AS customer_count
FROM customers
GROUP BY city;
This is one of the most common SQL interview questions.
GROUP BY with Multiple Columns
You can group by more than one column:
SELECT country, city, COUNT(*) AS total_customers
FROM customers
GROUP BY country, city;
Now SQL groups by both country and city together.
What Is the HAVING Clause?
Here’s where many beginners get confused.
HAVING is used to filter grouped results.
Think of it like:
WHEREfilters rows before grouping.HAVINGfilters groups after grouping.
HAVING Clause Example
Let’s say you only want countries with total sales above 10,000.
SELECT country, SUM(amount) AS total_sales
FROM sales
GROUP BY country
HAVING SUM(amount) > 10000;
Execution order:
- Rows are grouped by country.
- Total sales are calculated.
- HAVING filters groups where total > 10,000.
WHERE vs HAVING
This is a classic SQL interview question.
Use WHERE for:
Filtering raw rows.
SELECT country, SUM(amount)
FROM sales
WHERE amount > 100
GROUP BY country;
This filters rows before grouping.
Use HAVING for:
Filtering aggregated results.
SELECT country, SUM(amount)
FROM sales
GROUP BY country
HAVING SUM(amount) > 10000;
This filters after grouping.
Real-World Use Case
In business dashboards built in Microsoft Power BI or Tableau, GROUP BY queries are used to calculate:
- Total revenue by month
- Customers by region
- Average order value per category
- Sales per product
HAVING is often used to filter high-performing or low-performing groups.
Common Beginner Mistakes
Using HAVING without GROUP BY
Filtering aggregated values using WHERE
Forgetting to include columns in GROUP BY
Confusing execution order
Remember this:
FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY
Understanding this order prevents many errors.
Interview Tip
If asked:
“What is the difference between WHERE and HAVING?”
A strong answer:
WHERE filters individual rows before aggregation, while HAVING filters grouped results after aggregation.
Keep it short and confident.
GROUP BY helps you summarize data.
HAVING helps you filter summarized data.
Together, they form the foundation of business analytics in SQL.
If you’re aiming to become a strong data analyst, practice:
- GROUP BY with COUNT
- GROUP BY with SUM
- GROUP BY multiple columns
- HAVING with aggregate filters
These are asked repeatedly in interviews.
FAQs
1. What does GROUP BY do in SQL?
It groups rows with similar values and applies aggregate functions to each group.
2. What is the HAVING clause used for?
HAVING filters aggregated results after GROUP BY.
3. Can I use HAVING without GROUP BY?
Technically yes in some databases, but it’s mainly used with GROUP BY.
4. What is the difference between WHERE and HAVING?
WHERE filters rows before grouping. HAVING filters after grouping.
5. Are GROUP BY and HAVING important for interviews?
Yes. They are fundamental SQL concepts frequently tested.