"if you encounter any errors in SAP, send me a screenshot at pramod@learntosap.com, and I will help you resolve the issue."

SQL GROUP BY Statement

SQL TUTORIALS-

SQL GROUP BY Statement

Introduction-


Trulli Trulli

βœ…In a Simple Word The GROUP BY statement in SQL is used to arrange (same) identical data into groups.used with (collection) aggregate functions like COUNT(), SUM(), AVG(), MAX(), or MIN() to perform (working) operations on each group....


πŸ’‘ Note: The SQL GROUP BY must be used with columns that are not aggregated.This is useful for reporting, analytics, and data summarization.

βœ… Syntax Example -



SELECT column_name, AGGREGATE_FUNCTION(column_name)
FROM table_name
GROUP BY column_name;


πŸ’‘ Note: The SQL GROUP BY Operator. Example of Table 1)-Employee
Emoji Examples in HTML

πŸ”Έ Example -)Table: Employee


EmpID Name Department Salary
1 ANNI HR 25000
2 BOULT IT 50000
3 POOJA IT 60000
4 SUNNY SALES 65000

βœ… Example 1:-Total Salary by Department Wise-



SELECT Department, SUM(Salary) AS TotalSalary
FROM Employee
GROUP BY Department;

βœ…Result Show :-

Result β†’

Emoji Examples in HTML


Department TotalSalary
HR 25000
IT 110000
SALES 65000

βœ… Example 2: Average Salary by Department Wise-



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

βœ…Result Show :-

Result β†’

Emoji Examples in HTML


Department TotalSalary
HR 25000
IT 55000
SALES 65000

βœ… Example 3: Count of Employees in Each Department Wise-



SELECT Department, COUNT(*) AS EmployeeCount
FROM Employee
GROUP BY Department;

βœ…Result Show :-

Result β†’

Emoji Examples in HTML


Department EmployeeCount
HR 1
IT 2
SALES 1

βœ… Example 4:Minimum Salary by Department Wise-



SELECT Department, MIN(Salary) AS MinSalary
FROM Employee
GROUP BY Department;

βœ…Result Show :-

Result β†’

Emoji Examples in HTML


Department MinSalary
HR 25000
IT 50000
SALES 65000

Practice - Yes/No Quiz

1.Does the GROUP BY clause always require an aggregate function like SUM() or COUNT()?

2.Can you use GROUP BY without a SELECT column that is not grouped or aggregated?

3.Can we use GROUP BY with the WHERE clause?


May Be Like Important Link-

-Credit Memo and Return PO

-Bill Of Material (BOM)

-How To Create Quotation

-How To Create Debit Memo Request