Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Aggregating with GROUP BY and HAVING in SQL


In SQL, the GROUP BY clause is used to group rows that have the same values in specified columns into summary rows, such as finding the total, average, or count of values in each group. The HAVING clause is used to filter these groups based on a condition, typically applied to the result of an aggregate function like COUNT, SUM, AVG, MIN, or MAX.

1. GROUP BY Syntax

The basic syntax for using the GROUP BY clause in SQL is:

      SELECT column1, column2, aggregate_function(column3)
      FROM table_name
      WHERE condition
      GROUP BY column1, column2;
    

In this syntax:

2. Example of GROUP BY with COUNT

The COUNT function is often used to find the number of rows in each group. For example, you might want to know how many employees work in each department:

      SELECT Department, COUNT(EmployeeID) AS EmployeeCount
      FROM Employees
      GROUP BY Department;
    

This query groups employees by Department and then counts how many employees belong to each department using COUNT.

3. Example of GROUP BY with SUM

The SUM function can be used to calculate the total sum of a numeric column for each group. For instance, to calculate the total sales for each store:

      SELECT StoreID, SUM(SalesAmount) AS TotalSales
      FROM Sales
      GROUP BY StoreID;
    

This query groups the sales data by StoreID and calculates the total sales for each store using SUM.

4. Example of GROUP BY with AVG

The AVG function calculates the average of a column for each group. For example, if you want to know the average salary in each department, you can write:

      SELECT Department, AVG(Salary) AS AverageSalary
      FROM Employees
      GROUP BY Department;
    

This query groups the data by Department and calculates the average salary in each department using AVG.

5. Using HAVING to Filter Grouped Data

While the WHERE clause is used to filter rows before they are grouped, the HAVING clause is used to filter groups after the GROUP BY operation has been applied. It is particularly useful for filtering based on the results of aggregate functions.

Example of Using HAVING with COUNT

If you want to find the departments that have more than 5 employees, you can use the HAVING clause:

      SELECT Department, COUNT(EmployeeID) AS EmployeeCount
      FROM Employees
      GROUP BY Department
      HAVING COUNT(EmployeeID) > 5;
    

This query groups employees by Department and counts how many employees belong to each department. It then filters the results to show only departments with more than 5 employees.

Example of Using HAVING with SUM

If you want to find stores that have total sales greater than $1000, you can use the following query:

      SELECT StoreID, SUM(SalesAmount) AS TotalSales
      FROM Sales
      GROUP BY StoreID
      HAVING SUM(SalesAmount) > 1000;
    

This query groups the sales data by StoreID, calculates the total sales for each store, and then filters the stores to only include those where the total sales exceed $1000.

6. Using GROUP BY with Multiple Conditions

You can combine multiple conditions in the HAVING clause. For example, to find categories where the number of products is greater than 10 and the average price is greater than $50:

      SELECT Category, COUNT(ProductID) AS ProductCount, AVG(Price) AS AveragePrice
      FROM Products
      GROUP BY Category
      HAVING COUNT(ProductID) > 10 AND AVG(Price) > 50;
    

This query groups the products by Category, counts the number of products in each category, and calculates the average price. It then filters the categories to show only those with more than 10 products and an average price greater than $50.

7. Key Points to Remember

8. Conclusion

The GROUP BY and HAVING clauses are powerful tools in SQL that allow you to group data based on specific columns and then apply conditions to the aggregated results. By combining these clauses with aggregate functions, you can efficiently summarize and filter large datasets. Whether you're counting, summing, averaging, or finding the minimum and maximum values of data, GROUP BY and HAVING provide the flexibility needed to perform complex analysis on your data.



Advertisement

Advertisement

Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java