You are on page 1of 2

Aggregate Function or Group Function:

(i) Group by is an optional clause of the select statement


(ii)Group by clause follows the where clause in a select statement and precedes the
order by clause

select department_id from employees where department_id between 10 and 40 group by


department_id;

(i) we can only group by what we select


(ii) use aggregate functions(max,min,avg,sum,count)

select * from employees group by first_name;


select first_name from employees group by department_id; ---not a GROUP BY
expression

select department_id from employees group by department_id;


select first_name from employees group by first_name; ---(i)

select count(first_name) from employees group by department_id; ---(ii)

select first_name,last_name from employees group by first_name,last_name; -- more


than one column

select dept_id,salary from t1 group by dept_id,salary order by id,dept_id,salary;

100 4000
100 5000
200 1000
200 2000
we can only group by what we select and we can order by what we grouped.

select dept_id,salary from t1 group by dept_id,salary order by id,dept_id,salary;

Group function:

i. MAX
ii. MIN
iii. AVG
iv. SUM
v. COUNT

Arguments:

Refers a value that is passed into a function.

(a way for you to provide more information to a function.)

Max()
(i) It accept one argument.
(ii) It used to return maximum value in the column.

select MAX(salary) from employees;


select MAX(salary) from employees group by department_id;
select MAX(salary) from employees where department_id IN(10,20,30,40,50) group by
department_id;
select MAX(salary) from employees where department_id IN(10,20,30,40,50) group by
department_id having max(salary)>1000;
select MAX(salary) from employees where department_id IN(10,20,30,40,50) group by
department_id having max(salary)>1000 order by department_id asc;

select MIN(salary) from employees;


select MIN(salary) from employees group by department_id;
select MIN(salary) from employees where department_id IN(60,70,80,90,100) group by
department_id;
select MIN(salary) from employees where department_id IN(60,70,80,90,100) group by
department_id having MIN(salary)<1500;
select MIN(salary) from employees where department_id IN(10,20,30) group by
department_id having max(salary)<5000 order by department_id asc;

SUM()

(i) It accept one argument.


(ii) It used to display total value of a column.

You might also like