Beruflich Dokumente
Kultur Dokumente
GROUP FUNCTIONS
AVG(SALARY)MAX(SALARY)MIN(SALARY)SUM(SALARY
COUNT(DISTINCTDEPT_ID)
---------------------- ----------
4
AVG(SALARY)
-------------
24200
AVG(NVL(SALARY,0))
------------------
16600
NAME AVG(SALARY)
---------- -----------
hulk 24000
sid 16000
sam 19000
yummy 34000
hummy 21000
AVG(SALARY)
-----------
24000
16000
19000
34000
21000
ILLEGAL QUERIES
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
ID MAX(SALARY)
---------- -----------
13000
y76 34000
r34 21000
r56 19000
ID PAYROLL
---------- ----------
y76 34000
r34 21000
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.
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.
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.
20 rows selected.
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
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
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
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