You are on page 1of 4

Practical No.

Aim: To implement aggregate function commands in SQL

Theory:

An SQL group function or aggregate functions performs an operation on a group of


rows and returns a single result. You may want retrieve group of item-prices and
return total- price. This type of scenario is where you would use a group functions.
The following table is summary of some SQL group function & query examples.

Function Description Query Example

Returns average value of a SELECT avg(price)


AVG (fieldname)
column FROM inventory;
Returns number of items SELECT count
COUNT (fieldname)
in Table or queried items (product_id) Product;
Returns number of items SELECT count(*) FROM
COUNT (*)
in Table or queried items product;
Returns maximum value SELECT max(price)
MAX (fieldname)
of Column FROM inventory;
Returns minimum value of SELECT min(price)
MIN(fieldname)
Column FROM inventory;
Returns total value of SELECT sum(price)
SUM(fieldname)
Column FROM inventory;

To use a group function in a SQL query, list the function name followed by numeric
column name within parentheses. AVG averages the column; COUNT counts the
number of items, MAX returns maximum number of the column, and MIN returns
minimum number of the column.

Page 1 of 4
Consider following table: Employee

ENAME CNAME SALARY JDATE


------------ ----------------- ------------------ -------------
ANIL ACC 1500 01-MAY-89
SHANKAR TATA 2000 10-JUL-90
JAYA CMC 1800 07-3UN-91
SUNIL CMC 1700 01-JAN-88
VIJAY TATA 5000 03-JAN-88
PRAKASH TATA 3000 27-MAY-89
AJAY ACC 8000 30-APR-95
AMOL ACC 1000 17-MAR-95

1. AVG

SQL>SELECT avg(salary) FROM employee;

Result: - AVG (SALARY)


-------------------
3000

2. COUNT

SQL>SELECT count(salary) FROM employee;

Result: - COUNT (SALARY)


-------------------
8

3. MAX

SQL>SELECT max(salary) FROM employee;

Result: - MAX (SALARY)


-------------------
8000
4. MIN

SQL>SELECT min(salary) FROM employee;

Result: - MIN (SALARY)


-------------------
1000

Page 2 of 4
5. SUM

SQL>SELECT sum(salary) FROM employee;

Result: - SUM (SALARY)


-------------------
24000

6. Maximum salary of employee of ACC

SAL> select max(salary) from employee where cname = 'ACC';

Result: - MAX (SALARY)


-------------------
8000

Lab Exercise for Practical No. 3

1. List total loan

2. List total Deposit

3. List total loan taken from ANDHERI branch

4. List total deposit of customer having account date later thanv1-JAN-96

5. List total deposit of customers living in city NAGPUR

6. Count total number of customer

7. Count total number of branches

8. Give maximum loan from branch VRCE

9. List total deposit of customers having branch city DELHI

10. Count total number of customer cities

Page 3 of 4
Page 4 of 4

You might also like