0
779

# Group By in SQL With Example [ COUNT, AVG, MAX, MIN ,SUM ] Video

Learning Group By in SQL is a little bit tricky for a newbie programmer. In today’s lesson, I will give in-depth discussion about Group By in SQL with Example using Aggregate functions.

## Few Questions and Answers in learning SQL Group By Functions

What is Group By in SQL?

Group functions in sql are mathematical functions to operate on sets of rows to give one result per set. ex: “Find the number of employee in each Department”.

What Group By Does in SQL?

It arrange identical data into groups with the help of some aggregate functions.

What is Aggregate Functions in SQL?

is a function where the values of multiple rows are grouped together to form a single summary value.

Where Group By Having in SQL?

The HAVING clause is used to specify which groups are to be displayed, and thus, you further restrict the groups on the basis of aggregate information.

## Lesson Objectives

Here’s the main objectives after completing this lesson, you will be able to do the following:

• Identify the available Group Functions
• Describe the use of group functions
• Aggregate data using GROUP BY clause
• Include or Exclude grouped rows by using the HAVING clause

Types of group functions

The GROUP BY statement is often used with aggregate functions (AVG, COUNT, MAX, MIN, and SUM) to group the result-set by one or more columns.

Group by in SQL Aggregate Function

• COUNT

Calculate the number of rows in a set

• AVG

Calculates the average of the specified columns in a set of rows.

• MAX

Returns the largest value of the selected column.

• MIN

Returns the smallest value of the selected column.

• SUM

Returns the total sum of a numeric column.

## Sample table used in this lesson

Watch the Video demonstration here:

## Count with group by statement

The following SQL statement finds the number of Employee by Department:

## SQL Group by using AVG

The following SQL statement finds the average SALARY of all Employee:

## AVG with Group By in SQL Example

The following SQL statement finds the average SALARY of all Employee by Department:

## Query using MAX AND MIN With group by statement

The following SQL statement finds the Maximum and Minimum Salary by Department:

## Query using SUM With Group by statement

The following SQL statement finds the Payroll by Department:

## Use the HAVING clause to restrict groups:

1. Rows are grouped.
2. The group function is applied.
3. Groups matching the Having clause are displayed.

## Syntax of Group by HAVING in SQL

Example: The following SQL statement finds the Total Payroll exceeding 80,000.00 by Department.

## Results of using Having Clause

Based on the example of using HAVING Clause, it displays the DEPT_NAME and PAYROLL that exceeds 80000. You will also notice that the EXECUTIVE department has been excluded and the list is sorted by Department Name.

NOTE: You can use the HAVING clause to specify which groups are to be displayed, and thus, you further restrict the groups on the basis of aggregate information.

## Summary

In this lesson, you should have learned how to:

• Use the functions AVG, COUNT, MAX, MIN, SUM
• Write queries that use the Group By clause
• Write queries that use the Having Clause

## Inquiries

I hope that you have learned something new today. If you have any questions or suggestions about the Group by in SQL with examples, please feel free to leave a comment below.

#### Looking for more source code? Type your keyword here here!

Hello Itsourcecoders, welcome to itsourcecode.com. I'm Joken Villanueva, MIT a passionate Blogger, Programmer and a Hobbyist. I started Itsourcecode because I wanted to give back and Share all the learnings and knowledge I've learned in my career and I believe through this website I would be able to help and assist those newbie programmers in enhancing their skills from different programming languages. So let us all help each other by sharing our ideas!

This site uses Akismet to reduce spam. Learn how your comment data is processed.