You are on page 1of 3

=============================== Day 6 ========================

Joins:-
-----

Equi joins:-
------------

Inner join in Implicit method:-


---------------
ex:-
select
a.cust_id,
a.cust_name,
a.mob_no,
a.email,
a.country_id,
b.country_name
from customer, country
where customer.country_id=country.country_id and b.country_name='INDIA';
(or)
select
a.cust_id,
a.cust_name,
a.mob_no,
a.email,
a.country_id,
b.country_name
from customer a, country b
where a.country_id=b.country_id and b.country_name='INDIA';

left join :-
---------
--> we have to make '+' within brackets to right hand side table
select
a.cust_id,
a.cust_name,
a.mob_no,
a.email,
a.country_id,
b.country_name
from customer a, country b
where a.country_id=b.country_id(+);

--> But in ANSI method you can directly mention left join or left outer join in the
query.

Right join:-
----------
--> we have to make '+' within brackets to left hand side table
select
a.cust_id,
a.cust_name,
a.mob_no,
a.email,
b.country_id,
b.country_name
from customer a, country b
where a.country_id(+)=b.country_id;
--> But in ANSI method you can directly mention right join or right outer join in
the query.

Full outer join:-


---------------
--> In implicit method we have to do union on left join and right join then we will
get full join.
--> But in ANSI method we can direclty mention full outer join or full join.
ex:-
select
a.cust_id,
a.cust_name,
a.mob_no,
a.email,
a.country_id,
b.country_name
from customer a, country b
where a.country_id=b.country_id(+);
union
select
a.cust_id,
a.cust_name,
a.mob_no,
a.email,
a.country_id,
b.country_name
from customer a, country b
where a.country_id(+)=b.country_id;

Inner join in ANSI method:-


-------------------------
ex:-
select
a.cust_id,
a.cust_name,
a.mob_no,
a.email,
a.country_id,
b.country_name
from customer a inner join country b
on a.country_id=b.country_id
where b.country_name='INDIA';

--> In realtime mostly we use ANSI methond, performance wise ANSI method gives
better result
--> While joining 3 or more tables select all the required columns from all tables
and make aliases to the tables then use where condition.
--> You cannot be able to mention more than 2 tables in from clause in ANSI method,
if you want to join more than 2 tables in ANSI method then
you have to do join on first two tables and then the next table and so on.

Cross join or Cartesian product:-


-------------------------------
--> If you forget to mention join condition in a query, it will gives the product
of all the columns from both the tables.
ex:- table_1 have 10 records and table_2 also have 5 records, the cross join of
these two tables is 10*5=50.
--> In ANSI method we can directly mention as cross join.
--> Sometimes if you want to load some dummy data into the tables you can go for
cross join.

Self join:-
---------
--> Doing join on one table of same data is called self join
ex:-
select
e1.employee_id,e1.employee_name,
'Reports to',
e2.employee_id,e2.first_name
from employees e1, employees e2
where e1.manager_id=e2.employee_id order by 1;

Non-equi join:-
-------------
--> Whenever you are making join with non-equal(<,>,<=,>=,!=) condition is called
non-equi join.
--> It's same as cross join but here you won't get the matched records from the
table, you only get the non matched records.
--> Normally we do not write any non equi joins in realtime. we will write only
equi joins. Discussed joins above all are equi joins.
ex:-
select
e.emplouee_id,
e.first_name,
e.email,
e.salary,
e.deparmemt_id,
d.department_name,
d.location_id
from employees e, departments d
where e.deparment_id!=d.department_id;

You might also like