Saturday, May 12, 2012

SQL RIGHT JOIN


Hello Friends, Welcome to this post, and in this post, I will discuss about the SQL RIGHT JOIN.

SO far we know about the INNER JOIN, LEFT JOIN, so lets get introduced to SQL RIGHT Join.

So we know LEFT JOIN returned the ROWS from the LEFT TABLE ie Table_1, in RIGHT JOIN its just the opposite, it returns the ROWS from the RIGHT TABLE.

Lets have a look at our Table:

Table1: Customer

CID     Name             Designation
222     Whiskey           Moderator
444     Onty                 Member
111     Gaurav             Admin
333     Kumaar            Admin


Table2: Orders

OID      Product         CID
1            Perfume        666
2            Laptop          444
3            Perfume        999
4            Cell Phone    666
5            Toaster         888

Syntax:

SELECT column_name(s)
FROM table_1
RIGHT JOIN table_2
ON table_1.column_name = table_2.column_name
ORDER BY column_name


Again, ORDER BY is optional

So, for our Query:

SELECT Customer.Name, Orders.Product
FROM Customer
RIGHT JOIN Orders
ON Customer.CID = Orders.CID
ORDER BY Customer.Name

Our Result-Set Will Have:

Name            Product
Onty               444
                      666
                      999
                      666
                      888

Yeah Thats what our result set will have, all the records from the 'Orders' Table, and only the Matched Records from the 'Customer' Table.

Thats all for this post, Don't forget to check my next post.

Thank You!

0 comments:

Post a Comment