SQL Group By Clause

Let’s understand the working of SQL Group By Clause. In SQL, Group By clause can be used in a SELECT statement to collect data across multiple records and group the results by one or more columns.

 

The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.

SQL Syntax :

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name
GROUP BY column_name;

 

SQL Group By Clause Example

We have discussed, Group By is used to filter the records with Group Functions.

 

Query –  Calculate the Total Salary given to each Department.

SQL> Select Sum(Salary), Department_Id from Employees Group By Department_Id;

 

Query –  Find the maximum salary from each department.

SQL> Select  Max(Salary), Department_ID from Employees Group By Department_Id;

 

Query –  Find Average Salary of each Department.

SQL> Select Avg(Salary), Department_Id from Employees Group By Department_Id;

 

0 Comment

Leave a Reply