Site icon IT Tutorial

Oracle SQL Tutorials – Chapter 4

Chapter 4 – Group Functions

 

 

AVG & SUM

SELECT AVG (salary), SUM (salary)

FROM hr.employees;

 

MIN & MAX

SELECT MIN (salary), MAX (salary)

FROM hr.employees;

 

COUNT

SELECT COUNT (*), COUNT (DISTINCT job_id)

FROM hr.employees;

 

DISTINCT

SELECT DISTINCT job_id FROM hr.employees;

 

GROUP BY STATEMENT

 

SELECT department_id, AVG (salary)

FROM hr.employees

GROUP BY department_id

ORDER BY 1;

 

SELECT department_id,

job_id,

AVG (salary),

SUM (salary),

COUNT (*) Employees

FROM hr.employees

GROUP BY department_id, job_id

ORDER BY department_id;

 

HAVING STATEMENT

 

SELECT job_id, AVG (salary)

FROM hr.employees

GROUP BY job_id

HAVING AVG (salary) > 10000;

 

SELECT job_id, SUM (salary)

FROM hr.employees

WHERE job_id NOT LIKE '%REP%'

GROUP BY job_id

HAVING SUM (salary) > 13000

ORDER BY SUM (salary);

 

NESTED GROUP FUNCTIONS

SELECT MAX (AVG (salary))

FROM hr.employees

GROUP BY department_id;

 

SELECT department_id, AVG (salary)

FROM hr.employees

GROUP BY department_id

HAVING (MAX (salary)) > ALL ( SELECT AVG (salary)

FROM hr.employees

GROUP BY department_id);

Exit mobile version