Open In App

SQL Server Group Functions

Last Updated : 28 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The group function in SQL Server provides a powerful tool for performing calculations on groups of rows, allowing you to group data based on specific criteria. This function is important when you want to analyze and summarize information from multiple records in a data structure. The basic group functions are COUNT, SUM, AVG, MAX, and MIN.

Syntax:

The basic syntax for using the grouping function involves a GROUP BY clause with the custom aggregation function. Here’s the whole process:

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

  • column1: The group of columns in which you want to pass the data.
  • aggregate_function(column2): The group function (e.g., COUNT, SUM, AVG, MAX, MIN) applied to the new columns of each group.
  • table_name: The name of the table containing the data.

Primary Grouping Functions

  • COUNT(): Counts the number of rows in each group.

SELECT column1, COUNT(*) as row_count
FROM table_name
GROUP BY column1;

  • SUM(): Calculates the sum of values in a column for each group.

SELECT column1, SUM(column2) as total_sum
FROM table_name
GROUP BY column1;

  • AVG(): Computes the average of values in a column for each group.

SELECT column1, AVG(column2) as average_value
FROM table_name
GROUP BY column1;

  • MAX(): Retrieves the maximum value in a column for each group.

SELECT column1, MAX(column2) as max_value
FROM table_name
GROUP BY column1;

  • MIN(): Retrieves the minimum value in a column for each group.

SELECT column1, MIN(column2) as min_value
FROM table_name
GROUP BY column1;

Grouping Functions Concept

  • The GROUP BY clause is used to group rows by the specified column(s).
  • Aggregate functions (SUM, COUNT, AVG, etc.) perform calculations on each group.

Examples of Grouping Functions in SQL Server

Let’s create a simple example with an “employees” table and demonstrate the use of grouping functions.

Query

-- Step 1: Create the employees table
CREATE TABLE employees (
   employee_id INT PRIMARY KEY,
   employee_name VARCHAR(50),
   department VARCHAR(50),
   salary INT
);
-- Step 2: Insert sample data into the employees table
INSERT INTO employees (employee_id, employee_name, department, salary)
VALUES
   (1, 'John Doe', 'HR', 50000),
   (2, 'Jane Smith', 'IT', 60000),
   (3, 'Bob Johnson', 'HR', 55000),
   (4, 'Alice Williams', 'IT', 65000),
   (5, 'Charlie Brown', 'Finance', 70000)

Example 1: Using COUNT with GROUP BY

-- Step 3: Use Grouping Functions with the inserted data
SELECT department, COUNT(*) as employee_count
FROM employees
GROUP BY department;

Output:

Output

Output

Explanation:

  • Groups the employees table by the department column.
  • Counts the number of employees in each department using COUNT(*).

Example 2: Using AVG with GROUP BY

-- Step 3: Use Grouping Functions with the inserted data
SELECT department, AVG(salary) as avg_salary
FROM employees
GROUP BY department;

Output:
Output

Explanation:

  • Groups the employees table by the department column.
  • Calculates the average salary (AVG(salary)) for each department.

Example Output:

  1. In the output screenshot, you can see the result of the SQL query with grouped data and calculated values.
  2. Remember, the specific query and output will depend on your database schema and the data you have.

Conclusion

To sum up, grouping functions in SQL Server are essential for data analysis and summarization because they let you calculate values for sets of rows based on predefined standards. The principal grouping functions—COUNT, SUM, AVG, MAX, and MIN, among others offer strong instruments for compiling and drawing conclusions from your data.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads