Lecture No 5: Using Group Functions
Lecture No 5: Using Group Functions
Lecture No 4 at a Glance
Objectives
The maximum
salary in
the EMPLOYEES
table.
• AVG
• COUNT
• MAX
• MIN
• STDDEV
• SUM
• VARIANCE
You can use MIN and MAX for any data type.
• Date Data Type
Example
Example
SELECT COUNT(commission_pct)
FROM employees
WHERE department_id = 80;
SELECT AVG(commission_pct)
FROM employees;
EMPLOYEES
4400
9500
The
average
3500 salary
in
EMPLOYEES
6400 table
for each
10033
department.
“Add up the
salaries in
the EMPLOYEES
table
for each job,
grouped by
department.
Illegal Queries
Using Group Functions
SELECT
SELECT department_id,
department_id, COUNT(last_name)
COUNT(last_name)
FROM
FROM employees;
employees;
SELECT
SELECT department_id,
department_id, COUNT(last_name)
COUNT(last_name)
**
ERROR
ERROR at
at line
line 1:
1:
ORA-00937:
ORA-00937: not
not aa single-group
single-group group
group function
function
Column
Column missing
missing in
in the
the GROUP BY clause
GROUP BY clause
Illegal Queries
Using Group Functions
The maximum
salary
per department
when it is
greater than
$10,000
…
SELECT MAX(AVG(salary))
FROM employees
GROUP BY department_id;
Summary