Sie sind auf Seite 1von 13

CHAPTER 5

GROUP FUNCTIONS

Unlike single-row functions operate on sets of rows to give one


result per group. These sets may be the whole table or the table
split into groups.

Types of Group Functions:


 AVG
 CUONT
 MAX
 MIN
 STDDEV
 SUM
 VARIANCE

Using the AVG and SUM functions:

SQL> select avg(salary), max(salary), min(salary), sum(salary)


from Rox where id like'%r344%';

AVG(SALARY)MAX(SALARY)MIN(SALARY)SUM(SALARY

------------- ------------- ------------- -------------


16000 16000 16000 16000

Using the MIN and MAX Functions:

SQL> select min (DOB), max (DOB) from Rox;


MIN(DOB) MAX(DOB)
--------- ---------
14-FEB-09 08-JUL-09

Using the COUNT Function:


COUNT(+) returns the number of rows in a table.

SQL> select COUNT(*) from Rox where dept_id='10';


COUNT(*)
----------
2

SQL> select count(salary) from Rox where dept_id='10';


COUNT(SALARY)
---------------
2

Using the DISTINCT Keyword:


Use DISTINCT keyword to suppress the counting of any duplicate values
within a column.

SQL> select count(distinct dept_id) from Rox;

COUNT(DISTINCTDEPT_ID)

---------------------- ----------
4

SQL> select AVG(salary) from Rox;

AVG(SALARY)

-------------
24200

USING THE NVL FUNCTION WITH GROUP


FUNCTION

SQL> select avg(nvl(salary,0)) from Rox;

AVG(NVL(SALARY,0))
------------------
16600

USING THE GROUP BY CLAUSE


SQL> select name, avg(salary) from Rox group by name;

NAME AVG(SALARY)
---------- -----------
hulk 24000
sid 16000
sam 19000
yummy 34000
hummy 21000

SQL> select avg(salary) from Rox group by id;

AVG(SALARY)
-----------
24000
16000
19000
34000
21000
ILLEGAL QUERIES

SQL> select * from deptt;

DEPT_ID DEPT_NAME LOCATION_ID


---------- ---------- -----------
56 admin 456
45 manager 678
57 marketing 789
45 shipping 509
23 fishing 677

SQL> select id, count(name) from Rox ;


select id, count(name) from nikhil
*
ERROR at line 1:
ORA-00937: not a single-group group function

SQL> select id, avg(salary) from Rox where avg(salary) >8000 group by
id;
select id, avg(salary) from Roxwhere avg(salary) >8000 group by id
*
ERROR at line 1:
ORA-00934: group function is not allowed here

USING THE HAVING CLAUSE


SQL> select id, max(salary) from Roxgroup by id having
max(salary)>10000;

ID MAX(SALARY)
---------- -----------
13000
y76 34000
r34 21000
r56 19000

USING THE HAVING CLAUSE


SQL> select id, sum(salary) payroll from Roxwhere id not like 's%' group
by id having sum(salary)>13000 order by sum(salary);

ID PAYROLL
---------- ----------
y76 34000
r34 21000

NESTING GROUP FUNCTIONS


SQL> select max(avg(salary)) from Roxgroup by id;

MAX(AVG(SALARY))
----------------
34000
CHAPTER-4
CARTESIAN PRODUCTS:
A Cartesian product is formed when:
- A join condition is omitted.
-A join condition is invalid.
-All rows in the first table are joined to all rows in the second table
To avoid a Cartesian product, always include a valid join condition in a
WHERE clause.

Generating a Cartesian Product:


A Cartesian product is generated if a join condition is omitted.
SQL> select * from xxx;

DEPT_ID DEPT_NAME LOCATION_ID


---------- ---------- -----------
56 admin 456
57 marketing 789
45 manager 678
45 shipping 509
23 fishing 677
SQL> select name, dept_name d_n from Rox, xxx;
NAME D_N
---------- ----------
mac admin
owen hart admin
yummy admin
hummy admin
sam admin
mac marketing
owen hart marketing
yummy marketing
hummy marketing
sam marketing
mac shipping

NAME D_N
---------- ----------
Owen hart shipping
Yummy shipping
hummy shipping
sam shipping
mac fishing
owen hart fishing
yummy fishing
hummy fishing
sam fishing
20 rows selected.

Joining more than two tables:

Let us consider the table ‘loca’ as under


SQL> select * from loca;
LOCATION_ID CITY
----------- ----------
45 ioc colony
9435 lahore
8796 fzr
SQL> select e. name,p. name,l.city from nikhil e,ppp p,loca l where e.
name=p. name and l.city=p. address
NAME NAME CITY
---------- ---------- ----------
yummy yummy Lahore

Retrieving records with Non-equijoins:


A non-equijoin is a join condition containing something other than an
equality operator.
For this we need a table named ’grade’ as under
SQL> select * from grade;
GRA LOWEST_SAL HIGHEST_SAL
---------- ---------- -----------
A 30000 40000
B 20000 30000
C 10000 20000

SQL> select e. name, e. salary, g.gra from Rox e, grade g where


e.e_salary between g.lowest_sal and g.highest_sal
NAME SALARY GRA
---------- ---------- ----------
yummy 34000 A
hummy 21000 B
owen hart 12000 C
mac 11000 C

Using OUTER Joins:


We use the outer join to also see rows that do not meet the join
condition.the outer join operator is the plus sign(+).
SQL> select ppp. name, xxx.dept_name from ppp, xxx where ppp.
name(+)=xxx.dept_name;
NAME DEPT_NAME
---------- ----------
fishing
admin
marketing
shipping

SQL> select ppp. name, xxx.dept_name from ppp,xxx where ppp.


name=xxx.dept_name(+);
NAME DEPT_NAME
---------- ----------
hummy
yummy
sam
mac
owen hart

Creating CROSS Joins:


The cross join clause produces the cross-product of two twbles. This is the
same as a Cartesian product between the two tables.

SQL> select name, dept_name from ppp CROSS JOIN xxx;


NAME DEPT_NAME
---------- ----------
mac admin
owen hart admin
hummy admin
yummy admin
sam admin
mac marketing
owen hart marketing
hummy marketing
yummy marketing
sam marketing
mac shipping

NAME DEPT_NAME
---------- ----------
Owen hart shipping
hummy shipping
yummy shipping
nikhil shipping
mac fishing
owen hart fishing
hummy fishing
yummy fishing
sam fishing
20 rows selected.

Creating NATURAL Joins:


The natural join clause is based on all columns in the two tables that have
the same name. It selects rows from the two tables that have equal values in
all matched columns. If the columns having the same names have different
datatypes, an error is returned.

SQL> select e_name, id, location_id,dept_id from ppp NATURAL JOIN


xxx;

NAME ID LOCATION_ID DEPT_ID


---------- ---------- ----------- ----------
mac m78 456 56
owen hart n72 456 56
hummy r34 456 56
yummy y76 456 56
sam r56 456 56
mac m78 789 57
owen hart n72 789 57
hummy r34 789 57
yummy y76 789 57
sam r56 789 57
mac m78 789 45

NAME ID LOCATION_ID DEPT_ID


---------- ---------- ----------- ----------
Owen hart n72 678 45
hummy r34 678 45
yummy y76 678 45
sam r56 678 45
mac m78 678 45
owen hart n72 678 45
hummy r34 678 45
yummy y76 678 45
sam r56 678 45

20 rows selected.

Creating joins with the USING clause:

Using clause is used to match only one column when more than one
column matches. The natural join and using clauses are mutually exclusive.
SQL> select p. id,p. name, x.location_id from ppp p JOIN xxx x USING
(dept_id);
ID NAME LOCATION_ID
---------- ---------- -----------
r34 hummy 456
r56 sam 456
n72 owen hart 789

Creating joins with the ON clause:

The join condition for the natural join is basically an equijoin of all
columns with the same name.
SQL> select p. id,p. name,p.dept_id ,x.dept_id, x.location_id from ppp p
JOIN xxx x ON (p.dept_id=x.dept_id);
ID NAME DEPT_ID DEPT_ID LOCATION_ID
---------- ---------- ---------- ---------- -----------
r34 hummy 56 56 456
r56 sam 56 56 456
n72 owen hart 57 57 789
LEFT OUTER JOIN:

SQL> select p. name, p.dept_id, x.dept_name from ppp p left outer join
xxx x on (p.dept_id=x.dept_i
d);
NAME DEPT_ID DEPT_NAME
---------- ---------- ----------
sam 56 admin
hummy 56 admin
owen hart 57 marketing
mac 45
yummy 45

RIGHT OUTER JOIN:

SQL> select p. name, p.dept_id, x.dept_name from ppp p right outer join
xxx x on (p.dept_id=x.dept_
NAME DEPT_ID DEPT_NAME
--------- ---------- ----------
Owen hart 57 marketing
hummy 56 admin
sam 56 admin
fishing
Shipping

FULL OUTER JOIN:


SQL> select p. name, p.dept_id, x.dept_name from ppp p full outer join
xxx x on (p.dept_id=x.dept_i
NAME DEPT_ID DEPT_NAME
---------- ---------- ----------
sam 56 admin
hummy 56 admin
owen hart 57 marketing
mac 45
yummy 45
fishing
shipping
7 rows selected.

Additional conditions:
SQL> select p. id,p. name,p.dept_id, x.dept_id,x.location_id from ppp p
JOIN xxx x ON (p.dept_id=x
.dept_id) and p.dept_id='57';
ID NAME DEPT_ID DEPT_ID LOCATION_ID
---------- ---------- ---------- ---------- -----------
n72 owen hart 57 57 789

Das könnte Ihnen auch gefallen