Beruflich Dokumente
Kultur Dokumente
JOINS
===================================
-- Joins Means as a name combined somthing.
-- In The Oracle SQL Join means combined Two or More Tables Together.
--------------------------TYPES OF JOINS:---------------------------1.Natural Joins.: This Natural Joins Two table based On Same Column Name.
2.Inner Join / Simple Join
3.Equijoins
4.NONEquijoins
5.Outer Join
i. Left Outer Join
ii. Right Outer Join
iii. Full Outer Join
6.Self Joins.
7.Cross Join
-------------------------1. Natural Joins:
-------------------------You can Join Table Autometicaly based on the coloumn in the Two Tables that have
matching datatypes and names. You this by Using NATURAL JOIN by This keyword.
E.g.1->Join Two Tables using Natural Joins
SQL>desc employees;
SQL>desc departments;
SQL>select employee_id, job_id, salary, department_id, manager_id,
department_name from employees NATURAL JOIN departments;
E.g.2->Join More than Two Tables Using Natural Joins
SQL>desc employees;
SQL>desc departments;
SQL>desc locations;
SQL>desc countries;
SQL>select employee_id, job_id, salary, department_id, manager_id,
department_name, street_address, city, country_name from employees
NATURAL JOIN DEPARTMENTS
NATURAL JOIN LOCATIONS
NATURAL JOIN COUNTRIES;
E.g.4-> Natural Joins With "WHERE" CLAUSE.
SQL> select employee_id, job_id, salary, department_id, manager_id,
department_name, street_address, city, country_name from employees
NATURAL JOIN DEPARTMENTS
NATURAL JOIN LOCATIONS
NATURAL JOIN COUNTRIES
WHERE department_ID IN(20,50);
E.g.5.i-> Join Tables using Natural Join and use the "USING" Clause.
Note: a. USING Clause can be used to specifie Only those coloumns that should be
used for an equijoins.
b. We can use only those columns who are same or comun for both table in
using clause.
c. If one Table have tow comon column with another table if i mention only
one column in USING clause and this column not mention in select statemnt then it
will give
the ambigusly defined error. so you have to mention only those
column name which you want to use in USING clause.
SQL> select employee_id, job_id, salary, manager_id, department_name,
street_address, city, country_name from employees
JOIN DEPARTMENTS
USING(manager_id)
JOIN LOCATIONS
USING(location_id)
JOIN COUNTRIES
USING (country_id);
E.g.5.ii->
SQL> select employee_id, job_id, salary, manager_id,department_id,
department_name, street_address, city, country_name from employees
JOIN DEPARTMENTS
USING(manager_id, department_id)
JOIN LOCATIONS
USING(location_id)
JOIN COUNTRIES
USING (country_id);
E.g.6-> CREATING JOINS WITH "ON" CLAUSE.
use ON Clause to specified a join Conditions. With This you can specified join
condition seprate from any search or Filter Condition in the WHERE clause.
SQL>select e.employee_id, e.job_id, e.salary, e.manager_id,d.manager_id,
e.department_id, d.department_id, d.department_name, l.street_address, l.city,
c.country_name from
employees e
JOIN DEPARTMENTS d
ON(e.department_id=d.department_id)
JOIN LOCATIONS l
ON (d.location_id=l.location_id)
JOIN COUNTRIES c
ON (l.country_id=c.country_id);
E.g.7-> You can use "ON" CLAUSE and "USING" CLAUSE at One Query.
SQL>select e.employee_id, e.job_id, e.salary, e.manager_id,d.manager_id,
e.department_id, d.department_id, d.department_name, l.street_address, l.city,
c.country_name from
employees e
JOIN DEPARTMENTS d
ON(e.department_id=d.department_id)
JOIN LOCATIONS l
USING (location_id)
JOIN COUNTRIES c
USING (country_id);
E.g.8-> Use AND CLAUSE or WHERE CLAUSE to apply Additional conditions
SQL>
select e.first_name, e.last_name, e.salary, d.department_name,
d.location_id from employees e
join departments d
on (e.department_id = d.department_id)
and e.manager_id = 149;
or
SQL>
select e.first_name, e.last_name, e.salary, d.department_name,
d.location_id from employees e
join departments d
on (e.department_id = d.department_id)
where e.manager_id = 149;
-----------------------------4.NONEQUIJOINS:
-----------------------------A NONEQUIJOINS is a Join Condition containing somethig other than Equality
Operetor.
Example :
Supose we have EMPLOYEES TABLE and JOB_GRADES table like bellow Structure.:
SQL> desc employees;
Name
Null?
Type
--------------------------------------------EMPLOYEE_ID
NUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOT NULL VARCHAR2(25)
EMAIL
NOT NULL VARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOT NULL DATE
JOB_ID
NOT NULL VARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
SQL> desc job_grades;
Name
Null?
------------------------------- ------------GRADE_LEVEL
LOWEST_SAL
HIGHEST_SAL
Type
------------------VARCHAR2(3)
NUMBER
NUMBER
HIGHEST_SAL
--D
C
B
A
------------------------------------------500
5000
5001
10000
10001
15000
15001
30000
from employees e
RIGHT OUTER JOIN departments d
ON(e.manager_id=d.manager_id)
RIGHT OUTER JOIN locations l
USING(location_id)
RIGHT OUTER JOIN countries c
USING(country_id);
FULL OUTER JOIN:
--------------------------------SQL>desc employees;
SQL>desc departments;
SQL>desc locations;
SQL>desc countries;
SQL>select e.employee_id, e.job_id,e.salary, e.department_id, d.department_id,
e.manager_id, d.manager_id, d.department_name, l.street_address, l.city,
c.country_name
from employees e
FULL OUTER JOIN departments d
ON(e.manager_id=d.manager_id)
FULL OUTER JOIN locations l
USING(location_id)
FULL OUTER JOIN countries c
USING(country_id);
------------------6.Self Joins.
------------------Self Join is a join of a Table Itself.
is used to join a table to itself as if the table were two tables, temporarily renaming
at least one table in the SQL statement.
SQL> select e.employee_id EMP#, e.last_name EMPNAME, m.employee_id MGR#,
m.last_name MANAGER from employees e join employees m
ON (m.employee_id=e.manager_id);
-------------------7.Cross Join:
-------------------Cartision Products:
When a join condition is Invalid or Omitted completely, The result is Cartision
Product in which all combinition of all rows are displayed.
All Rows In the First Table are joined to all Rows In the Second Table.
SQL> select first_name, last_name, salary, department_name from employees cross
join departments;
*************************************************************************************