• Group functions are built-in SQL functions that operate on groups of rows and return one value for the entire group. • Unlike single-row functions, group functions operate on sets of rows to give one result per group. Types of Group Functions •AVG •COUNT •MAX •MIN •STDDEV •SUM • VARIANCE Using Group Functions
SELECT [column, ] group_function(column)
FROM table [WHERE condition] AVG and SUM Functions
• AVG and SUM functions are used for numeric
data. Example: SELECT AVG(column_name) , SUM(column_name) FROM emp MIN and MAX Functions • You can use MIN and MAX for any datatype. For numeric datatype: SELECT min(column_name) , max(column_name) FROM emp For date datatype: SELECT MIN(column_name having date values) , MAX(column_name having date values) FROM emp For char datatype: SELECT MIN(column_name having char values) , MAX(column_name having char values) FROM emp COUNT Function • This function returns the number of rows in the table. • The COUNT function has two formats: • COUNT(*) • COUNT(expr) COUNT(*) COUNT(*) returns the number of rows in a table, including duplicate rows and rows containing null values in any of the columns. Syntax: SELECT COUNT(*) From table COUNT(expr) COUNT(expr) returns the number of non NULL rows in a table, i.e. number of rows that satisfies expression . Syntax: SELECT COUNT(columnname) FROM table Group Functions and Null Values
• All group functions except COUNT ignore null
values in the column. • Example: AVG(column_name) returns an average value calculated upon the rows in the table where a valid value is stored . Group data by using Group By clause
All group functions have treated the table as one
large group of information. At times, you need to divide the table of information into smaller groups. • This can be done by using the GROUP BY clause. Syntax: SELECT column_name, group_function(column_name) FROM emp GROUP BY column_name ; More detailed syntax SELECT column, group_function (column) FROM table [WHERE condition] [GROUP BY group by] [ORDER BY column]; Grouping by More Than One Column
• SELECT deptno, job, SUM(sal)
• FROM emp • GROUP BY deptno • ORDER BY deptno ; Restriction on Groups • You cannot use the WHERE clause to restrict groups. • You use the HAVING clause to restrict groups. Example: SELECT deptno, AVG(sal) FROM emp WHERE AVG(sal) > 2000 GROUP BY deptno; Wrong statemnet SELECT deptno, AVG(sal) FROM emp GROUP BY deptno HAVING AVG(sal) > 1000 Right Statement Hence WHERE clause is used to restrict the rows and HAVING clause is used to restrict groups. Excluding Group Results: HAVING Clause • Use the HAVING clause to restrict groups • Rows are grouped. • The group function is applied. • Groups matching the HAVING clause are displayed. Syntax: SELECT column, group_ function FROM table [WHERE condition] [GROUP BY group_by_expression] [ HAVING group_condition ] [ORDER BY column]; • SELECT deptno, max(sal) FROM emp GROUP BY deptno HAVING max(sal) > 2900; Exercise: • Write the sql statement for displaying salary of the lowest paid employee group by department number.Exclude groups where the minimum salary is less than 11,000. Sort the output in descending order of deptno.