How to Use GROUP BY in SQL (With Examples)

How to Use GROUP BY in SQL (With Examples)

When working with data in SQL, one of the most important skills you need is the ability to summarize data.

This is where the GROUP BY clause comes in.

GROUP BY allows you to group rows based on one or more columns and perform calculations on each group. It is widely used in data analysis, reporting, and business intelligence.

In this guide, you’ll learn how to use GROUP BY in SQL step by step with practical examples.

What Is GROUP BY in SQL?

GROUP BY is used to group rows that have the same values in specified columns.

It is typically used with aggregation functions such as:

  • SUM()
  • COUNT()
  • AVG()
  • MIN()
  • MAX()

This allows you to summarize data instead of viewing individual records.

Basic Syntax

SELECT column_name, AGG_FUNCTION(column_name)
FROM table_name
GROUP BY column_name;

Example 1: Count Records by Category

SELECT product_category, COUNT(*) AS total_products
FROM products
GROUP BY product_category;

Insight:

Shows how many products exist in each category.

Example 2: Total Revenue by Region

SELECT region, SUM(revenue) AS total_revenue
FROM sales
GROUP BY region;

Insight:

Calculates total revenue for each region.

Example 3: Average Salary by Department

SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;

Insight:

Finds the average salary in each department.

Using GROUP BY With Multiple Columns

You can group data by more than one column.

SELECT region, product_category, SUM(revenue) AS total_revenue
FROM sales
GROUP BY region, product_category;

Insight:

Breaks down revenue by both region and product category.

Using GROUP BY With WHERE

The WHERE clause filters data before grouping.

SELECT region, SUM(revenue) AS total_revenue
FROM sales
WHERE order_date >= '2026-01-01'
GROUP BY region;

Insight:

Analyzes revenue only for a specific time period.

Using HAVING With GROUP BY

The HAVING clause filters grouped results.

SELECT region, SUM(revenue) AS total_revenue
FROM sales
GROUP BY region
HAVING SUM(revenue) > 100000;

Insight:

Shows only regions with revenue above 100,000.

Difference Between WHERE and HAVING

  • WHERE filters rows before grouping
  • HAVING filters results after grouping

This is a common concept tested in interviews.

Common Mistakes to Avoid

When using GROUP BY, beginners often make these mistakes:

1. Selecting Columns Not in GROUP BY

-- Incorrect
SELECT region, product_name, SUM(revenue)
FROM sales
GROUP BY region;

This will cause an error in most databases.

2. Forgetting Aggregation Functions

GROUP BY must be used with aggregation when summarizing data.

3. Misusing HAVING Instead of WHERE

Use WHERE for filtering raw data and HAVING for grouped data.

Real-World Use Cases

GROUP BY is widely used in business analysis:

  • Sales reporting (revenue by region)
  • Customer segmentation
  • Financial summaries
  • Performance tracking

It helps analysts turn raw data into meaningful insights.

GROUP BY is one of the most important SQL concepts for data analysis.

It allows you to summarize large datasets, uncover trends, and generate insights that support decision-making.

For data analysts, mastering GROUP BY is essential for writing effective SQL queries and delivering valuable business insights.

FAQs

What is GROUP BY in SQL?

GROUP BY is used to group rows and apply aggregation functions to each group.

Can GROUP BY be used without aggregation?

Technically yes, but it is usually combined with aggregation functions.

What is the difference between WHERE and HAVING?

WHERE filters rows before grouping, while HAVING filters grouped results.

Can I group by multiple columns?

Yes. You can group by one or more columns.

Why is GROUP BY important?

It helps summarize data and generate insights for analysis and reporting.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top