SQL Server > Joins

Inner join more than one tables in SQL Server

How to inner join more than one table and get the results in SQL server?


Inner join or Join is used to join more than one tables and get only those records whose linked columns are present in both tables.

More than one table can be joined and get the results in following ways

First Approach

Run following SQL statement in Query window by selecting our database.

SELECT pd.PersonalDetailsId, pd.FirstName, pd.LastName, pd.Age,
ac.Salary, ac.PPFDeduction FROM PersonalDetails pd, Accounts ac
WHERE pd.PersonalDetailsId = ac.PersonalDetailsId

Here, we are joining PersonalDetails and Accounts table. “pd” is the alias of “PersonalDetails” table and “ac” is the alias of “Accounts” table.

In the above Sql statement, we are instructing the database to select PersonalDetailsId, FirstName, LastName, Age from PersonalDetials table and Salary, PPFDeduction from Accounts table by where the value of PersonalDetails.Personal DetailsID is equal to Accounts.PersonalDetailsId.

Similarly, more than two tables can also be joined provided at least one column is common in any of two tables.

Second Approach

The same can be achieved using JOIN or INNER JOIN keyword also.

SELECT pd.PersonalDetailsId, pd.FirstName, pd.LastName, pd.Age,
ac.Salary, ac.PPFDeduction FROM PersonalDetails pd
JOIN Accounts ac on pd.PersonalDetailsId = ac.PersonalDetailsId

SELECT pd.PersonalDetailsId, pd.FirstName, pd.LastName, pd.Age,
ac.Salary, ac.PPFDeduction FROM PersonalDetails pd
INNER JOIN Accounts ac on pd.PersonalDetailsId = ac.PersonalDetailsId

Both of above statements would result the same output as we are using INNER JOIN instead of JOIN.

Here, we are getting the same output columns from both tables however the tables are being joined using JOIN or INNER JOIN keyword. Because of JOIN or INNER JOIN keywords, the approach of specifying there equality of common field “PersonalDetailsId” is different. Here we do not specify the common table in WHERE clause but using “on” keyword.

Both of above approaches would filter the same records from the database, so functionally all of three SQL Select statements are same.

 Views: 2563 | Post Order: 35



Write for us