SQL Inner join more than two tables SQL Inner join more than two tables sql sql

SQL Inner join more than two tables


SELECT * FROM table1 INNER JOIN table2      ON table1.primaryKey=table2.table1IdINNER JOIN table3      ON table1.primaryKey=table3.table1Id


Here is a general SQL query syntax to join three or more table. This SQL query should work in all major relation database e.g. MySQL, Oracle, Microsoft SQLServer, Sybase and PostgreSQL :

SELECT t1.col, t3.col FROM table1 join table2 ON table1.primarykey = table2.foreignkey                                  join table3 ON table2.primarykey = table3.foreignkey

We first join table 1 and table 2 which produce a temporary table with combined data from table1 and table2, which is then joined to table3. This formula can be extended for more than 3 tables to N tables, You just need to make sure that SQL query should have N-1 join statement in order to join N tables. like for joining two tables we require 1 join statement and for joining 3 tables we need 2 join statement.


SELECT eb.n_EmpId,   em.s_EmpName,   deg.s_DesignationName,   dm.s_DeptNameFROM tbl_EmployeeMaster em INNER JOIN tbl_DesignationMaster deg ON em.n_DesignationId=deg.n_DesignationId INNER JOIN tbl_DepartmentMaster dm ON dm.n_DeptId = em.n_DepartmentId INNER JOIN tbl_EmployeeBranch eb ON eb.n_BranchId = em.n_BranchId;