• Latest
  • Trending
Use of Aggregate Functions and GroupBy in SQL

Use of Aggregate Functions and GroupBy in SQL

May 23, 2022
Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023
Vice President Dr. Bawumia inaugurates  ICT Hub

Vice President Dr. Bawumia inaugurates ICT Hub

April 2, 2023
Co-Creation Hub’s edtech accelerator puts $15M towards African startups

Co-Creation Hub’s edtech accelerator puts $15M towards African startups

February 20, 2023
Data Leak Hits Thousands of NHS Workers

Data Leak Hits Thousands of NHS Workers

February 20, 2023
EU Cybersecurity Agency Warns Against Chinese APTs

EU Cybersecurity Agency Warns Against Chinese APTs

February 20, 2023
How Your Storage System Will Still Be Viable in 5 Years’ Time?

How Your Storage System Will Still Be Viable in 5 Years’ Time?

February 20, 2023
The Broken Promises From Cybersecurity Vendors

Cloud Infrastructure Used By WIP26 For Espionage Attacks on Telcos

February 20, 2023
Instagram and Facebook to get paid-for verification

Instagram and Facebook to get paid-for verification

February 20, 2023
YouTube CEO Susan Wojcicki steps down after nine years

YouTube CEO Susan Wojcicki steps down after nine years

February 20, 2023
Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
  • Consumer Watch
  • Kids Page
  • Directory
  • Events
  • Reviews
Thursday, 16 July, 2026
  • Login
itechnewsonline.com
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion
Subscription
Advertise
No Result
View All Result
itechnewsonline.com
No Result
View All Result

Use of Aggregate Functions and GroupBy in SQL

by ITECHNEWS
May 23, 2022
in Data Science, Leading Stories
0 0
0
Use of Aggregate Functions and GroupBy in SQL

Introduction on Aggregate functions in SQL

Before going to our main concept, we must first learn- What are function and type?

Function in SQL is a set of SQL statements that performs a specific task. SQL provides a variety of functions which are grouped into two categories:

YOU MAY ALSO LIKE

French Telco Orange Hit by Cyber-Attack

ATC Ghana supports Girls-In-ICT Program

1. Single row function

2. Multiple row function.

Single row functions operate on a single row and one result per row. This function can accept one or more arguments and returns one value for each row. And can be used with SELECT, WHERE and ORDER BY. Whereas the multiple row function is used with multiple rows and gives aggregate value.

This multiple row function is also known as the Aggregate function and Non-Scalar function. In SQL normally we work on single row values but when it comes to working on multiple rows at that time we use an aggregate function. Aggregate functions are used to perform specific types of operations such as counting the number of records in a table, searching maximum and minimum values from a table, finding out the sum and average values from a table, etc. An aggregate function ignores NULL values in a table when performing the calculation except for the COUNT() function.

The aggregate function is used in the SELECT statement.

 Let’s discuss the types of aggregate functions-

Types Of Aggregate function

 SQL provide several types of aggregate functions they are SUM(),AVG(),MAX(),MIN(),COUNT(),FIRST() and LAST(). SUM(), AVG(), MAX(), MIN() these functions are applied on those attributes which has integer type value for example salary of employees, age of employees, marks of students etc.

SUM()- This function is used to find out the sum of a specified column in a set of rows.

Syntax: SELECT SUM(column_name) FROM table_name;
Example: SELECT SUM(Salary) FROM Employee;

 

Types Of Aggregate function

AVG()- This function is used to calculate the average of a specified column in a set of rows.

Syntax: SELECT AVG(column_name) FROM table_name;
Example: SELECT AVG(Salary) FROM Employee;

 

Types Of Aggregate function Image 2

MAX()– This function retrieves the maximum value of a specified column.

Syntax: SELECT MAX(column_name) FROM table_name;
Example: SELECT E_NAME,MAX(SALARY) FROM Employee;

 

Types Of Aggregate function Image 3

MIN()- This function retrieves the minimum value of a specified column.

Syntax: SELECT MIN(column_name) FROM table_name;
Example: SELECT MIN(Salary) FROM Employee;

 

Types Of Aggregate function Image 4

COUNT()– This function returns the number of rows in a database table.

Syntax: SELECT COUNT(*) FROM table_name;
       SELECT COUNT(DISTINCT column_name) FROM table_name;
Example: SELECT COUNT(*) FROM EMPLOYEE ;
        SELECT COUNT(DEPARTMENT) FROM EMPLOYEE WHERE SALARY>30000;

 

Types Of Aggregate function Image 5
Types Of Aggregate function Image 6

When we use COUNT(*), it counts all rows and gives the output as the total number of rows present in the employee table. And in the next example, we use COUNT(DEPARTMENT) with where clause by putting the condition SALARY > 30000, then it counts those DEPARTMENT salaries where employees get more than 30000.

FIRST()-This function returns the first value of a specified column.

Syntax: SELECT FIRST(column_name) FROM table_name;
Example: SELECT FIRST(Name) FROM EMPLOYEE ;

LAST()-This function returns the last value of a specified column.

Syntax: SELECT LAST(column_name) FROM table_name;
Example: SELECT  LAST(Name) FROM EMPLOYEE ;

We can use aggregate functions in a particular column. If there is any query that we have to calculate or count a group of values from a specified table to resolve this kind of issue in SQL we use the GROUP BY clause with aggregate functions. For that reason, the Aggregate function is also known as the GROUP function. Let’s discuss how to implement GROUP BY with aggregate function in SQL.

GROUP BY

The GROUP BY clause is a SQL command used to group rows with the same values. The GROUP BY clause is used in the SELECT statement. Also, we use the WHERE statement with GROUP BY.WHERE clause is used before GROUP BY. To represent a group of values in a particular manner ORDER BY clause is also used with GROUP BY and  GROUP BY placed before the ORDER BY.

Syntax: 
SELECT column_name FROM table_name 
WHERE [condition] 
GROUP BY column_name
ORDER BY column_name;
Example:
SELECT DEPARTMENT, SUM(SALARY) FROM EMPLOYEE 
GROUP BY DEPARTMENT
ORDER BY DEPARTMENT;
Types Of Aggregate function Image 7

Like the SUM() function we can also use other aggregate functions(AVG(),MAX(),MIN(),COUNT(*)) with GROUP BY.

Example:
SELECT DEPARTMENT, AVG(SALARY) FROM EMPLOYEE GROUP BY DEPARTMENT
ORDER BY DEPARTMENT;
Types Of Aggregate function Image 8
Example:

SELECT DEPARTMENT, MIN(SALARY) FROM EMPLOYEE

GROUP BY DEPARTMENT

ORDER BY DEPARTMENT;
Types Of Aggregate function Image 9

After using this query it displays the minimum salary of each department.

Example:
SELECT DEPARTMENT, MAX(SALARY) FROM EMPLOYEE
GROUP BY DEPARTMENT
ORDER BY DEPARTMENT;
Types Of Aggregate function Image 10

After using this query it displays the maximum salary of each department.

Example:

SELECT DEPARTMENT, COUNT(E_NAME) FROM EMPLOYEE
GROUP BY DEPARTMENT
ORDER BY DEPARTMENT;
Types Of Aggregate function Image 11

This query counts the number of employees working in each department.

We can also use the GROUP BY without applying the Aggregate function. In this case GROUP BY works like a DISTINCT clause which never shows any duplicate value in the result set.

Example:
SELECT E_NAME, Salary FROM EMPLOYEE 
GROUP BY DEPARTMENT;

 

Types Of Aggregate function Image 12

In the above example, we use the GROUP BY clause without an aggregate function. In the EMPLOYEE table, there are a total of 8 rows but it returns only 4-row values i.e one value from each department (Tech, HR, IT, Sales) because the rest of the row values belongs to those departments. It displays the first value of each department and avoids the rest values. Here GROUP BY works like the DISTINCT clause which avoids duplicate rows from the result set.

HAVING

 Sometimes we do not want to see the whole output produced by the GROUP BY. To filter the group of data based on a specified list of conditions the HAVING clause is often used with the GROUP BY. HAVING clause is placed after GROUP BY. Both WHERE and HAVING clauses can be used in the same query. As the WHERE clause can not be used with aggregate functions like SUM(), AVG(), MIN(), etc… for that reason HAVING clause is used with an aggregate function to filter the group of a result set.

Syntax:
SELECT column 1,column 2
FROM table
WHERE [conditions]
GROUP BY column 1
HAVING [condition]
ORDER BY column 1;
Example:
SELECT DEPARTMENT, AVG(SALARY)FROM EMPLOYEE
GROUP BY DEPARTMENT 
HAVING AVG(SALARY)>28000;
Types Of Aggregate function Image 12

In the above example, the query returns those departments’ average salary whose average salary is more than 28000.

Types Of Aggregate function Image 13

In the above example, we use the WHERE clause with aggregate functions instead of the HAVING clause and it returns the syntax error message.

Conclusion on Aggregate functions

The aggregate function is one of the most powerful concepts of SQL. This article has seen all the important concepts of the aggregate function with proper examples of how they work with queries

Source: Sonali Dash
Via: analyticsvidhya
Tags: Use of Aggregate Functions and GroupBy in SQL
ShareTweet

Get real time update about this post categories directly on your device, subscribe now.

Unsubscribe

Search

No Result
View All Result

Recent News

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023

About What We Do

itechnewsonline.com

We bring you the best Premium Tech News.

Recent News With Image

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025

Recent News

  • Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa July 29, 2025
  • French Telco Orange Hit by Cyber-Attack July 29, 2025
  • ATC Ghana supports Girls-In-ICT Program April 25, 2023
  • Vice President Dr. Bawumia inaugurates ICT Hub April 2, 2023
  • Home
  • InfoSec
  • Opinion
  • Africa Tech
  • Data Storage

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In

Add New Playlist

No Result
View All Result
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
Go to mobile version