How To Use Group By Clause In OraclePLSQL
Description
Group By clause is used in a select statement. Group by clause collects all the data, multiple records and group a result into one or more columns. Group by clause can only be used with aggregate function like Sum, Count, Max, Min and Avg.
Syntax
- Select
- Expression Aggregate function()
- from
- table_name
- where
- condition
- Group by
- expression
Example
The example, given below, uses Sum function-
- select
- Emp_name,
- Sum(salary) As "Total salary"
- From
- Employee
- group by
- Emp_name;
Example
The example, given below, uses Count function-
- select
- Emp_name,
- count(*) As "Total salary"
- from
- Employee
- where
- salary > 20000
- group by
- Emp_name;
Example
The example, given below, uses Min function-
- select
- Emp_name,
- Min(salary) As "Lowest Salary"
- from
- Employee
- Group By
- Emp_name;
Example
The example, given below, uses Max function-
- select
- Emp_name,
- Max(salary) As "Lowest Salary"
- from
- Employee
- Group By
- Emp_name;
Example
The example, given below, uses AVG function-
- select
- Emp_name,
- Max(salary) As "Lowest Salary"
- from
- Employee
- Group By
- Emp_name;
Summary
Thus, we learnt, Group By Clause is used in a select statement. Group by clause can only be used with an aggregate function like Sum, Count, Max, Min and Avg. We learnt, how to use this clause in Oracle with the examples.