structured query language. group functions what are group functions ? group functions group...

Post on 02-Jan-2016

226 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Structured Query Language

Group Functions

What are group functions ?

Group Functions• Group functions operate on sets of rows to

give one result per group. These sets may be the whole table or the table split into groups.

What are Group Functions?

DNO SALARY--------- ---------- 10 2450 10 5000 10 1300 20 800 20 1100 20 3000 20 3000 20 2975 30 1600 30 2850 30 1250 30 950 30 1500 30 1250

MAX(SAL)---------- 5000

“maximum Salary in

The Employee table”

Types of Group Functions

AVG COUNT MAX MIN SUM

Group Functions The avg(fieldname) function returns the average value of

a numeric column’s returned values. The min(fieldname) and max(fieldname) functions

return minimum and maximum numeric value of a column respectively.

The count(fieldname) returns an integer representing the count of the number of rows retrieved where value of the fieldname is not null..

The count(*) returns count of all rows, including the null values.

The sum(fieldname) returns sum of a numeric column.

Query 1

Find the maximum salary, the minimum salary, and the average salary among all employees.

SELECT MAX(SALARY), MIN(SALARY), AVG(SALARY)

FROM EMPLOYEE

Query 2

Retrieve the total number of employees in the company

SELECT COUNT (*)

FROM EMPLOYEE

Groups of data

Until now, 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.

GROUPING

In many cases, we want to apply the group functions to subgroups of rows in a table

Each subgroup of rows consists of the set of tuples that have the same value for the grouping attribute(s)

The function is applied to each subgroup independently

SQL has a GROUP BY-clause for specifying the grouping attributes, which must also appear in the SELECT-clause

Query 3

For each department, retrieve the department number, the number of employees in the department, and their average salary

SELECT DNO, COUNT (*), AVG (SALARY)FROM EMPLOYEEGROUP BY DNO

Aggregate Functions

• In the previous query, the EMPLOYEE table rows are divided into groups, each group having the same value for the grouping attribute DNO

• The COUNT and AVG functions are applied to each such group of rows separately

• The SELECT-clause includes only the grouping attribute and the functions to be applied on each group

Excluding Group Results Sometimes we want to retrieve the values of the group

functions for only those groups that satisfy certain conditions

The HAVING-clause is used for specifying a selection condition on groups (rather than on individual rows)

o The having clause is designed for use with the group by clause to restrict the groups that appear. This is very similar to the where clause, except that the where clause filters individual rows, whereas the having clause filters groups.

Excluding Group Results

EMP DEPTNO SAL--------- ---------- 10 2450 10 5000 10 1300 20 800 20 1100 20 3000 20 3000 20 2975 30 1600 30 2850 30 1250 30 950 30 1500 30 1250

DEPTNO MAX(SAL)---------- ----------

10 500020 3000

“maximumsalary

per departmentgreater than

$2900”

5000

3000

2850

Query 4

Display the department numbers, and maximum salary for those departments whose maximum salary is greater than $40500.

SELECT DNO, MAX(SALARY) FROM EMPLOYEE

GROUP BY DNOHAVING MAX(SALARY) > 40500

Query 5

o Display the number of employees, and the average salaries for each

department that have more than one employee working :

Select dno, count(ssn), avg(salary)From employeeGroup by dnoHaving count(ssn) > 1;

Query 6

o Count the number of distinct salary values in the employee table

SELECT COUNT(DISTINCT SALARY)

FROM EMPLOYEE

Query 7

o Retrieve the department number that has more than one location

SELECT dnumber

FROM dept_locations

GROUP BY dnumber

HAVING count(dnumber)>2

Query 8

o Write a query that will display the difference between the highest salary and lowest salary . Label the column

DIFFERENCE

SELECT max(salary)-min(salary) DIFFERENCE

FROM employee

top related