0% found this document useful (0 votes)
3 views

Aggregate functions

Datadase application
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Aggregate functions

Datadase application
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

AGGREGATE FUNCTIONS USING GROUP BY, ORDER BY AND

HAVING CLAUSE

EX.NO: 4
DATE:

AIM:
To execute the aggregate fuctions using group for Attendance Management System.
 COUNT
 SUM
 AVG
 MAX
 MIN
TABLE CREATION:
SQL> create table Empll(employee_ID varchar(20),emp_name varchar(20),emp_age
number,emp_gender varchar(20));

Table created.
SQL>insert into Empll values(1,’ram’,20,’male’);
1 row created.
SQL> insert into Empll values(2,'kumar',23,'male');
1 row created.

SQL> insert into Empll values(3,'sheetha',20,'female');


1 row created.

SQL> insert into Empll values(4,'ragu',25,'male');


1 row created.

SQL> insert into Empll values(5,'vishwa',25,'male');


1 row created.

SQL> insert into Empll values(6,'vidhya',22,'female');


1 row created.
SQL> insert into Empll values(7,'hari',22,'male');
1 row created.

SQL> insert into Empll values(8,'kesavan',28,'male');


1 row created.

SQL> insert into Empll values(9,'anbu',29,'male');


1 row created.

SQL> insert into Empll values(10,'anjali',28,'female');


1 row created.

SQL> select * from Empll;

EMPLOYEE_ID EMP_NAME EMP_AGE EMP_GENDER


----------------------- -------------------- ----------------- ---------------------
1 ram 20 male
2 kumar 23 male
3 sheetha 20 female
4 ragu 25 male
5 vishwa 25 male
6 vidhya 22 female
7 hari 22 male
8 kesavan 28 male
9 anbu 29 male
10 anjali 28 female

10 rows selected.
1. COUNT:
SQL> select count(*) from Empll;

COUNT(*)

-------------
10
SQL> select count(distinct emp_age)from Empll;

COUNT(DISTINCTEMP_AGE)

-----------------------------------
6
SQL> select count(emp_age) from Empll;

COUNT(EMP_AGE)
------------------------
10
SQL> select count(*) from Empll where employee_ID<5;

COUNT(*)
---------------
4

USING WHERE :
SQL> select count(*) from Empll where employee_ID between 5 and 9;

COUNT(*)
----------------
5
SQL> select count(*) from Empll where employee_ID not between 4 and 9;

COUNT(*)
---------------
4

GROUP BY :
SQL> select emp_age,count(*) from Empll group by emp_age;

EMP_AGE COUNT(*)
---------------- --------------
25 2
22 2
28 2
29 1
20 2
23 1

SQL> select emp_name,count(*) from Empll group by emp_name;

EMP_NAME COUNT(*)
----------------- ---------------
ram 1
kumar 1
ragu 1
sheetha 1
vishwa 1
hari 1
anjali 1
kesavan 1
anbu 1
vidhya 1
ORDER BY :

SQL> select emp_age,count(employee_ID) from Empll group by emp_age order by


count(employee_ID);

EMP_AGE COUNT(EMPLOYEE_ID)
----------------- --------------------------------
23 1
29 1
20 2
22 2
25 2
28 2
SQL> select emp_name,count(emp_age) from Empll group by emp_name order by
count(emp_age);

EMP_NAME COUNT(EMP_AGE)
--------------------- ---------------------------
ram 1
kumar 1
ragu 1
sheetha 1
vidhya 1
hari 1
anjali 1
kesavan 1
anbu 1
vishwa 1
HAVING CLAUSE :
SQL> select emp_age,count(*) from Empll group by emp_age having count(*)>1;

EMP_AGE COUNT(*)
----------------- --------------
25 2
22 2
28 2
20 2
SQL> select emp_age,count(*) from Empll group by emp_age having count(*)<=1;

EMP_AGE COUNT(*)
----------------- -------------
29 1
23 1

2. SUM :
SQL> select sum(emp_age) from Empll;

SUM(EMP_AGE)
-----------------------
242
SQL> select sum(employee_ID) from Empll;

SUM(EMPLOYEE_ID)
-------------------------------
55
USING WHERE :
SQL> select sum(emp_age) from Empll where employee_ID>=5;

SUM(EMP_AGE)
-----------------------
154
SQL> select sum(emp_age) from Empll where employee_ID<=5;

SUM(EMP_AGE)
-----------------------
113

GROUP BY:
SQL> select sum(emp_age) from Empll where employee_ID>3 group by emp_name;

SUM(EMP_AGE)
-----------------------
25
25
22
28
28
29
22

ORDER BY :
SQL> select emp_name,sum(emp_age) from Empll group by emp_name order by
count(employee_ID);
EMP_NAME SUM(EMP_AGE)
------------------ ------------------------
ram 20
kumar 23
ragu 25
sheetha 20
vidhya 22
hari 22
anjali 28
kesavan 28
anbu 29
vishwa 25

HAVING CLAUSE :
SQL> select employee_ID,sum(emp_age) from Empll group by employee_ID having
sum(emp_age)>=28;

EMPLOYEE_ID SUM(EMP_AGE)
---------------------- ------------------------
8 28
10 28
9 29

3. AVG :
SQL> select avg(emp_age) from Empll;

AVG(EMP_AGE)
-----------------------
24.2
SQL> select avg(employee_ID) from Empll;

AVG(EMPLOYEE_ID)
-------------------------------
5.5

USING WHERE :
SQL> select avg(emp_age) from Empll where employee_ID>6;

AVG(EMP_AGE)
-----------------------
26.75

SQL> select avg(emp_age) from Empll where employee_ID<4;

AVG(EMP_AGE)
-----------------------
21

GROUP BY :
SQL> select avg(employee_ID) from Empll where emp_age>24 group by employee_ID;

AVG(EMPLOYEE_ID)
------------------------------
8
10
5
9
4
SQL> select avg(employee_ID) from Empll where emp_age<27 group by employee_ID;

AVG(EMPLOYEE_ID)
-----------------------------
1
3
6
5
7
2
4

ORDER BY :
SQL> select employee_ID,avg(emp_age) from Empll group by employee_ID order by
count(emp_age);

EMPLOYEE_ID AVG(EMP_AGE)
---------------------- -----------------------
1 20
3 20
6 22
8 28
4 25
5 25
7 22
9 29
2 23
10 28

HAVING CLAUSE :
SQL> select employee_ID,avg(employee_ID) from Empll group by employee_ID having
avg(emp_age)<24;
EMPLOYEE_ID AVG(EMPLOYEE_ID)
---------------------- ------------------------------
1 1
3 3
6 6
7 7
2 2
SQL> select employee_ID,avg(employee_ID) from Empll group by employee_ID having
avg(emp_age)>24;

EMPLOYEE_ID AVG(EMPLOYEE_ID)
---------------------- -------------------------------
8 8
10 10
5 5
9 9
4 4

4. MAX :
SQL> select max(employee_ID) from Empll;

MAX(EMPLOYEE_ID)
------------------------------
9

SQL> select max(emp_age) from Empll;

MAX(EMP_AGE)
------------------------
29
SQL> select max(employee_ID*emp_age) from Emplll;

MAX(EMPLOYEE_ID*EMP_AGE)
-----------------------------------------------
280

SQL> select max(emp_age) from Emplll where employee_ID>3;

MAX(EMP_AGE)
-----------------------
29

SQL> select max(employee_ID) from Emplll where emp_age between 20 and 25;

MAX(EMPLOYEE_ID)
------------------------------
7

SQL> select max(employee_ID) from Emplll where emp_age not between 20 and 25;

MAX(EMPLOYEE_ID)
------------------------------
9

SQL> select max(emp_age) from Emplll where employee_ID<6 group by employee_ID;


MAX(EMP_AGE)
-----------------------
20
20
25
23
25

SQL> select max(emp_age) from Emplll where employee_ID=4;

MAX(EMP_AGE)
-----------------------
25

5.MIN:
SQL> select min(employee_ID) from Empll;

MIN(EMPLOYEE_ID)
----------------------------
1

SQL> select min(emp_age) from Empll;

MIN(EMP_AGE)
---------------------
20
SQL> select min(emp_age) from Emplll where employee_ID>3;

MIN(EMP_AGE)
---------------------
22
SQL> select min(employee_ID) from Emplll where emp_age between 22 and 26;

MIN(EMPLOYEE_ID)
-----------------------------
2

SQL> select min(employee_ID) from Emplll where emp_age not between 22 and 26;

MIN(EMPLOYEE_ID)
-----------------------------
1

SQL> select min(employee_ID) from Emplll where emp_age<25 group by employee_ID;

MIN(EMPLOYEE_ID)
-----------------------------
1
3
6
7
2

SQL> select min(emp_age) from Emplll where emp_age=22;

MIN(EMP_AGE)
----------------------
22
Conduct of Experiment(20)

Observation(10)

Record(10)

Viva(10)

Total(50)

RESULT:
Thus the result of Aggregate Functions using Group By,Order By and Having Clause for
Attendance Management System has been executed successfully.

You might also like