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

Aggregate Function

(i) Group by is an optional clause of the select statement that follows the where clause and precedes the order by clause. It is used to group the results of a query by one or more columns or expressions. (ii) Aggregate functions such as MAX, MIN, AVG, SUM, COUNT can be used with the group by clause to perform calculations on groupings of rows. These functions allow you to return aggregate data, like minimum, maximum, average values per group. (iii) Arguments refer to values passed into functions to provide more information. Aggregate functions like MAX accept a single column argument and return the maximum value in that column for each group.

Uploaded by

kvigneshk2001
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Aggregate Function

(i) Group by is an optional clause of the select statement that follows the where clause and precedes the order by clause. It is used to group the results of a query by one or more columns or expressions. (ii) Aggregate functions such as MAX, MIN, AVG, SUM, COUNT can be used with the group by clause to perform calculations on groupings of rows. These functions allow you to return aggregate data, like minimum, maximum, average values per group. (iii) Arguments refer to values passed into functions to provide more information. Aggregate functions like MAX accept a single column argument and return the maximum value in that column for each group.

Uploaded by

kvigneshk2001
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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