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

GROUP BY Clause in SQL


The GROUP BY clause in SQL is used to arrange identical data into groups. It is typically used with aggregate functions like COUNT, SUM, AVG, MIN, and MAX to perform calculations on each group of data. This allows you to summarize your data in a way that is easier to analyze and understand.

1. Syntax of GROUP BY Clause

The basic syntax for using the GROUP BY clause is as follows:

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

In this syntax:

2. Example of Using GROUP BY with COUNT

The COUNT function is often used with GROUP BY to count the number of rows in each group. For example, to count how many products are available in each category, you can write the following query:

      SELECT Category, COUNT(ProductID) AS ProductCount
      FROM Products
      GROUP BY Category;
    

This query groups the products by Category and then counts the number of products in each category using the COUNT function.

3. Example of Using GROUP BY with SUM

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

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

This query groups the data by StoreID and calculates the sum of SalesAmount for each store using the SUM function.

4. Example of Using GROUP BY with AVG

The AVG function calculates the average value of a numeric column for each group. For example, to find the average price of products in each category, you can write:

      SELECT Category, AVG(Price) AS AveragePrice
      FROM Products
      GROUP BY Category;
    

This query groups the products by Category and calculates the average price in each category using the AVG function.

5. Example of Using GROUP BY with MIN and MAX

The MIN and MAX functions are used to find the minimum and maximum values of a column for each group. For example, to find the minimum and maximum prices of products in each category, you can write:

      SELECT Category, MIN(Price) AS MinPrice, MAX(Price) AS MaxPrice
      FROM Products
      GROUP BY Category;
    

This query groups the products by Category and calculates the minimum and maximum price for each category using the MIN and MAX functions.

6. GROUP BY with HAVING Clause

In some cases, you might want to filter the results of your grouped data. You can use the HAVING clause to filter groups based on aggregate function results. The HAVING clause is similar to the WHERE clause, but it works after the grouping is done.

Example of Using GROUP BY with HAVING

      SELECT Category, COUNT(ProductID) AS ProductCount
      FROM Products
      GROUP BY Category
      HAVING COUNT(ProductID) > 5;
    

This query groups products by Category, counts the number of products in each category, and then filters the results to only show categories that have more than 5 products.

7. Conclusion

The GROUP BY clause is an essential tool in SQL that allows you to group data based on one or more columns and perform aggregate functions on those groups. It is especially useful when you need to summarize data and perform calculations like counting, summing, averaging, or finding the minimum or maximum values. Additionally, you can filter grouped results using the HAVING clause, which gives you more control over the data you retrieve.



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