Friday, 18 May 2012

SQL LEFT JOIN:



The LEFT JOIN keyword returns all rows from the left table (table1), even if there are no matches in the right table (table2).
Note that : the  left and  Right  joins  are  refered  as  OUTER  JOIN.
Syntax
SELECT column_name(s) FROM tablename1 as t1 LEFT JOIN tablename2 as t2 ON t1.column_name=t2.column_name

SQL LEFT JOIN Example:
The table “Customer” is,
Id
Name
Address
City
1
Vaishnavi
 10,Anna Nagar
Bangalore
2
Shajil
 23,T.Nagar
Chennai
3
Vinoth
 20,Sivaji Street
Chennai
The  table “Order” is,
O_Id
OrderNo
Id
1
77895
3
2
44678
3
3
22456
1
4
24562
1
5
34764
15
Query:
SELECT C.Name, C.City, O.OrderNo FROM Customer as C LEFT JOIN Orders as O ON C.Id=O.Id
The result-set will be:
Name
City
OrderNo
Vaishnavi
Bangalore
22456
Vaishnavi
Bangalore
24562
Vinoth
Chennai
77895
Vinoth
Chennai
44678
Shajil
Chennai
The LEFT JOIN keyword returns all the rows from the left table (Customers), even if there are no matches in the right table (Orders).

No comments:

Post a Comment