You are on page 1of 6

You cannot use an aggregate function to limit rows in a WHERE clause

SELECT city, AVG(salary)


2
FROM employee
3
WHERE AVG(salary) > 20
4
GROUP BY city;
WHERE AVG(salary) > 20
*
ERROR at line 3:
ORA-00934: group function is not allowed here
===========================================================================
Incorrect Usage of Aggregate Function Calls
SELECT id, AVG(salary)
2 FROM employee;
SELECT id, AVG(salary)
*
ERROR at line 1:
ORA-00937: not a single-group group function
===========================================================================
Using Groups of Rows with Aggregate Functions
SELECT city, AVG(salary) FROM employee GROUP BY city;
===========================================================================
Grouping Rows with the GROUP BY Clause to get some information on those groups o
f rows
SELECT city, sum(salary) FROM employee GROUP BY city;
===========================================================================
GROUP by with NULL value
SELECT city, AVG(salary) FROM employee GROUP BY city;
CITY
AVG(SALARY)
---------- ----------1788.78
New York
6110.28
Toronto
1234.56
Vancouver 5180.44667
===========================================================================
SUM(x) adds all the values in x and returns the total
SELECT SUM(salary) FROM employee;
========================================================================
Group By CUBE
Type
Dog
Cat
Turtle
Dog
Cat
Dog
Turtle

Store
Miami
Miami
Tampa
Tampa
Naples
Naples
Naples

Number
12
18
4
14
9
5
1

SELECT Type, Store, SUM(Number) as Number


FROM Pets
GROUP BY type,store
WITH CUBE

Type
Cat
Cat
Cat
Dog
Dog
Dog
Dog
Turtle
Turtle
Turtle
NULL
NULL
NULL
NULL

Store
Miami
Naples
NULL
Miami
Naples
Tampa
NULL
Naples
Tampa
NULL
NULL
Miami
Naples
Tampa

Number
18
9
27
12
5
14
31
1
4
5
63
30
15
18

SELECT Type, Store, SUM(Number) as Number


FROM Pets
GROUP BY type,store
WITH ROLLUP
Type
Store Number
Cat
Miami 18
Cat
Naples 9
Cat
NULL
27
Dog
Miami 12
Dog
Naples 5
Dog
Tampa 14
Dog
NULL
31
Turtle Naples 1
Turtle Tampa 4
Turtle NULL
5
NULL
NULL
63
==============================================================================
Sum with null value
When null is used in a mathematical expression in SQL, the result is null, as fo
r example in:
select 5+7+null+9 from dual;
5+7+NULL+9
---------However, if a sum() is created over some numbers, nulls are disregarded as the f
ollowing example shows:
create table group_by_sum (
a number,
b number
);
insert
insert
insert
insert
insert
insert

into
into
into
into
into
into

group_by_sum
group_by_sum
group_by_sum
group_by_sum
group_by_sum
group_by_sum

values
values
values
values
values
values

(
1, 1);
( 10, 1);
( 100, 1);
( 1000, 1);
(
2, 2);
( 20, 2);

insert into group_by_sum values ( 200, 2);


insert into group_by_sum values ( 2000, 2);
insert into group_by_sum values ( null 2);
select sum(a), b from group_by_sum
group by b;
SUM(A)
B
---------- ---------1111
1
2222
2
================================================================================
==
Sum column for a certain time period
select sum(amount)
from sales
where time_id between to_date('2006-02-01', 'YYYY-MM-DD') and to_date('2006-02
-28', 'YYYY-MM-DD');
================================================================================
======================
Sum() function and having clause
SELECT emp_id, pro_id
2 FROM server_usage
3 WHERE pro_id = 1001 OR pro_id=1002
4 GROUP BY emp_id, pro_id
5 HAVING SUM(hours_logged) > 20;
================================================================================
==================
MIN(x) gets the minimum values for x.
SELECT MIN(salary) FROM employee;
MIN(SALARY)
----------1232.78
================================================================================
==============
Use MIN() with strings
select min(first_name) from employee;
MIN(FIRST_
---------Alison
================================================================================
==========
Use MIN() with dates
select min(start_date) from employee;
MIN(START
--------21-MAR-76

================================================================================
================
MAX(x) gets the maximum values for x.
SELECT MAX(salary) FROM employee;
MAX(SALARY)
----------7897.78
================================================================================
============
Use MAX() with strings
select max(first_name) from employee;
MAX(FIRST_
---------Robert
================================================================================
===
Use MAX() with dates
select max(start_date) from employee;
MAX(START
--------17-SEP-96
================================================================================
==========
max(total_price) - min(total_price)
select max(total_price) - min(total_price)
2
from gift;
MAX(TOTAL_PRICE)-MIN(TOTAL_PRICE)
--------------------------------112.17
================================================================================
========
Who have the max value
select gift_id, total_price
2
from gift
3 where total_price =
4
(select max(total_price) from gift);
GIFT_ID TOTAL_PRICE
---------- ----------1
123.12
================================================================================
================
Count distinct column value
select count(deptno), count(distinct deptno)
2 ,
avg(comm),
avg(nvl(comm,0))

3 from

employees;

COUNT(DEPTNO) COUNT(DISTINCTDEPTNO) AVG(COMM) AVG(NVL(COMM,0))


------------- --------------------- ---------- ---------------10
4
550
220
================================================================================
=====================
Compare the difference between count(*) and count(distinct course)
select trainer
2 ,
count(distinct course)
3 ,
count(*)
4 from course_schedule
5 group by trainer;
TRAINER COUNT(DISTINCTCOURSE) COUNT(*)
---------- --------------------- ---------1
1
1
2
1
1
3
1
1
4
1
1
5
1
1
6
1
1
7
1
1
8
2
2
9
1
1
10
1
1
2
2
11 rows selected.
================================================================================
============
count(*) - count(onhand)
select count(*) - count(onhand) from product; 4-2
COUNT(*)-COUNT(ONHAND)
---------------------2
================================================================================
===========
Count all employees by even/odd employee id
select case
2
3
4
5 ,
6 from
7 group

mod(empno,2)
when 0 then 'EVEN '
else 'ODD '
end as empno
sum(sal)
emp
by mod(empno,2);

EMPNO SUM(SAL)
----- ---------EVEN
14025
ODD
14850
================================================================================
==========

Using avg() function in having clause


select to_char(register_date, 'YYYY/MM') "Month",
2
avg(total_price) "Avg Sales by Month"
3
from gift
4 group by to_char(register_date, 'YYYY/MM')
5 having avg(total_price) > 35;
Month Avg Sales by Month
------- -----------------1999/02
57.51
================================================================================
========

You might also like