SQL Joins Basically


SQL Join:-
Sql Join is used to retrieve data from one or more tables joined by common fields.The most common field is Primarykey from one table and foreign key in another table.

EX:-
select A.Empno,A.Empname,B.Salary,B.Department from Emp1 A,Emp2 B
where A.Empno=B.Empno

There are two types of joins in SQL
1)Inner joins-Inner join select rows from both tables.Usually join condition is equality of two columns one from table A and other from table B

Syntax


SELECT <column list>
FROM <left joined table>
[INNER] JOIN <right joined table>
ON <join condition>

2)Outer joins
The outer joins have two subtypes
i)Left outer join
II)right outer join

Outer join extends the functionality of inner join. It returns following rows:
the same rows as inner join i.e. rows from both tables, which matches join condition and
rows from one or both tables, which do not match join condition along with NULL values in place of other table's columns.

Outer join syntax is as follows: -

SELECT column list
FROM left joined table
LEFT|RIGHT|FULL [OUTER] JOIN <right joined table>
ON join condition

Cross Join:-

Cross join is used to return all records where each row from first table is combined with each row in second table.
cross join is also called as Cartesian Product join.

Sql Cross Join Syntax:-

Select * FROM TABLE 1 CROSS JOIN TABLE 2

OR

Select * FROM TABLE 1, TABLE 2



...............................................................................................................................................................


Basically,there are two types of join
 but further these are divided in to parts as fellow
1. Inner Join
  1. Equi Join
  2. Natural join
  3. Cross Join
2. Outer join
  1. Left outer
  2. Right outer
  3. full outer Join
Join: Join is nothing but retrieve the data from multiple table .
Joines are classified into 4 types.
1) Inner join.
2)Outer join.
3)Cross join.
4)Self join.

1) Inner join: Inner join is used for select matched rows as well as unmatched rows from both the tables
Ex :
select empname,empid from emp table inner join on dept table where emptable.empid = depttable.deptid
2) Outer join:
It will retrive only matched records from both the tables .it is again classified into two types
a)Left outer:Left side table select all record and right side table select only matched records.
Ex:
select empname,empid from emp table Left join on dept table where emptable.empid = depttable.deptid
b)Right outer: Reverse of Left join
3)Cross join: cross join is used for the cross the two table data.
4) self join:joining it self .
  

0 comments:

Post a Comment