You are on page 1of 6

===================================

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;

---------------------------------------------2. Inner Join / Simple Join:


---------------------------------------------Inner join is the simplest and most common Type Of join. It is also called as simple
join.
It returns all Rows From multiple Tables where the Join condition is Met.
E.g.1->Join More than Two Tables. in Bellow command i tryed to join Four Tables
Together.
Note : We can use "ON"clasue and "USING" clause in one query.
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
INNER JOIN departments d
ON(e.department_id=d.department_id)
INNER JOIN locations l
USING(location_id)
INNER JOIN countries c
USING(country_id);
----------------------3.EQUIJOINS:

-----------------------Equi joion is a join condition containing an Equality operetor (=).


E.g.1-> Two Tables Joins
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, d.location_id from
employees e,
departments d where (e.department_id =
d.department_id);
E.g.2-> More Than TWO Tables
SQL> select e.first_name, e.last_name, e.salary, d.department_name, d.location_id,
l.city from employees e, departments d, locations l
where e.department_id=d.department_id
AND d.location_id=l.location_id;

-----------------------------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

Data Of Job Grades :GRA LOWEST_SAL

Type
------------------VARCHAR2(3)
NUMBER
NUMBER

HIGHEST_SAL

--D
C
B
A

------------------------------------------500
5000
5001
10000
10001
15000
15001
30000

SQL>select e.last_name, e.salary, j.grade_level from employees e JOIN job_grades j


ON e.salary BETWEEN j.lowest_sal AND j.highest_sal;
----------------------5. Outer Join:
----------------------i. Left Outer Join : returns all rows from the left table, even if there are
no matches in the right table.
ii. Right Outer Join: returns all rows from the Right table, even if there
are no matches in the left table.
iii. Full Outer Join : returns rows when there is a match in one of the
tables.
Outer Join:- Outer Join is the Similier to Equijoins. but it gets also the non match
Rows From tha table.
It ic Categorized in Left Outer join, Right Outer Join, Full Outer Join by
Oracle 9I ANSI/ISO 1999 standreds.
i. Left 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
LEFT OUTER JOIN departments d
ON(e.manager_id=d.manager_id)
LEFT OUTER JOIN locations l
USING(location_id)
LEFT OUTER JOIN countries c
USING(country_id);
ii. Right 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
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;
*************************************************************************************

You might also like