Tuesday, May 24, 2011

Inner join


Inner join is one type of join in SQL server to retrieve data from two tables.

Inner join syntax is as below:



Select column1[, column2 …]
from Table1
     inner join Table2 on Table1.Column1 = Table2.Column4 [ and Table1.Column6 = Table2.ColumnN]

Inner join returns only those data which are matched with condition in both tables. Inner join will exclude the data from table1 and table2 which are not met the condition.

Let’s take an example:

We have two tables Employee and Projects. Employee table have 10 employee records and Project table have 5 projects information.

Out of 10 employees, 2 employees are not allocated into any project. So when you tried to execute inner join on that table then inner join will return 8 records (except the 2 employees which are not allocated into the project).


Syntax:

Select *
    from EmployeeMaster
         inner join ProjectMaster on EmployeeMaster.ProjectId = ProjectMaster.ProjectId


Employee records (which are not allocated into the project) are not met the condition. So that data will be excluded from the output.

No comments:

Post a Comment