You are on page 1of 12

Aggregate functions

Created by: Siti Dwi Oktariana Ningrum


SISTEM INFORMASI REGULER 2014
FAKULTAS ILMU KOMPUTER
UNIVERSITAS SRIWIJAYA

Fungsi-Fungsi Agregat

AVERAGE

avg()

: Memperoleh nilai rata-rata pada suatu relasi

MINIMUM

MAXIMUM

max() : Memperoleh nilai terbesar pada suatu relasi

SUMMARY

sum() : Memperoleh jumlah dari nilai-nilai pada suatu relasi

COUNT

DISTINCT

min() : Memperoleh nilai terendah pada suatu relasi

count() : Memperoleh cacah data yang tidak bernilai null


distinct()

: Menghitung setiap nilai yang berbeda saja

Aggregate functions
nr
1
5
7
12

name
John
Sarah
Tom
Anne

salary
100
300
100
null

dept
A
C
A
C

Perhatikan Tabel diatas sebagai table E untuk


Employee

A variation of aggregate operation ()


allows this:
Grouping attribute placed to left
of symbol
Aggregate functions to right of
symbol
EX: DNO COUNT SSN, AVERAGE Salary (EMPLOYEE)

E is any relational-algebra expression

G1, G2 , Gn is a list of attributes on which to group (can be empty)


Each Fi is an aggregate function

Each Ai is an attribute name

Note: Some books/articles use OR g instead of (Calligraphic G)

SUM
SQL
select
sum(salary)
from E

Result
Sum
500

Relational
algebra
Fsum(salary)(E)

Note:

Duplicates are not eliminated.

Null values are ignored.

AVG
SQL
select
avg(salary)
from E

Result
avg
125

Relational
algebra
Favg(salary)(E)

Note:

Duplicates are not eliminated.

Null values are ignored.

MIN
SQL
select
min(salary)
from E

Result
min
100

Relational
algebra
Fmin(salary)(E)

Note:

Duplicates are not eliminated.

Null values are ignored.

MAX
SQL
select
max(salary)
from E

Result
max
300

Relational
algebra
Fmax(salary)(E)
Note:

Duplicates are not eliminated.

Null values are ignored.

COUNT
SQL

Result

select
count(salary)
from E

Count
3

Relational
algebra
Fcount(salary)(E)

Duplicates

are not
eliminated.

Null

values are NOT


counted.

Aggregate Functions and Grouping

Grouping can be combined with Aggregate Functions


Example: For each department, retrieve the DNO, COUNT SSN, and AVERAGE SALARY

Having
Pemakaian HAVING terkait dengan klausa GROUP BY. Kegunaannya adalah untuk menentukan kondisi
bagi GROUP BY. Kelompok yang memenuhi HAVING saja yang akan dihasilkan. Sebagai contoh, anda bisa
mencoba pernyataan sebagai berikut:
SELECT table.One FROM table WHERE table.Four=5 GROUP BY table.One HAVING count (table.Three)>4
Answer: table (Four = 5) G One F count(Three) count(Three) > 4 One

Examples of applying aggregate


functions and grouping

You might also like