19
Structured Query Language

Structured Query Language

Embed Size (px)

DESCRIPTION

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 --------- ---------- - PowerPoint PPT Presentation

Citation preview

Page 1: Structured Query Language

Structured Query Language

Page 2: 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.

Page 3: Structured Query Language

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”

Page 4: Structured Query Language

Types of Group Functions

AVG COUNT MAX MIN SUM

Page 5: Structured Query Language

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.

Page 6: Structured Query Language

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

Page 7: Structured Query Language

Query 2

Retrieve the total number of employees in the company

SELECT COUNT (*)

FROM EMPLOYEE

Page 8: Structured Query Language

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.

Page 9: Structured Query Language

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

Page 10: Structured Query Language

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

Page 11: Structured Query Language

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

Page 12: Structured Query Language
Page 13: Structured Query Language

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.

Page 14: Structured Query Language

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

Page 15: Structured Query Language

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

Page 16: Structured Query Language

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;

Page 17: Structured Query Language

Query 6

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

SELECT COUNT(DISTINCT SALARY)

FROM EMPLOYEE

Page 18: Structured Query Language

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

Page 19: Structured Query Language

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