SQL - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

SQL Group By

SQL Group By

shape Description

To divide the data of a table into groups based on single column use SQL Group By clause. Some aggregate function like SUM(),AVG(),COUNT(), MIN(),MAX() can also be kept in these Group by clauses.

shape Conceptual figure

shape Syntax

Select <column_name>,Group <function1>,Group <function2> from <table_name> Group by <column_name>; Table_name=>Any accurate table. column_name => The condition that one can perform on a column by using Group by clause.

SQL Group By

shape Examples

By viewing the below example, the concept of group by clause can be easily understand. [c]sql> select * from employee; +--------+-------+-------+--------+ | emp_id | ename |salary | deptno | +--------+-------+-------+--------+ | 1001 | mike | 12000 | 10 | | 1002 | rambo | 13000 | 20 | | 1003 | kate | 14000 | 10 | | 1003 | jeo | 14000 | 20 | | 1003 | finn | 14000 | 30 | +--------+-------+-------+--------+ 5 rows in set (0.00 sec) sql> select deptno,max(salary) from employee group by deptno; +--------+-------------+ | deptno | max(salary) | +--------+-------------+ | 10 | 14000 | | 20 | 14000 | | 30 | 14000 | +--------+-------------+ 3 rows in set (0.00 sec) sql> select deptno,min(salary) from employee group by deptno; +--------+-------------+ | deptno | min(salary) | +--------+-------------+ | 10 | 12000 | +--------+-------------+ 1 rows in set (0.00 sec) [/c] The above example tells that, when group by clause operation is performed on a column name like deptno and salary, then it automatically displays the entire details of employees based on maximum salary of employee.

Summary

shape Key Points

  • SQL GROUP BY Clause - Is utilized to divide the information of a table into groups.