Tuesday, 15 March 2016

SQL Join with 3 tables?

SQL JOIN With 3 Tables Example :
Example#1
SELECT  t1.column1, t2.column2, t3.column3
FROM table1 t1 INNER JOIN
table2 t2 ON t1.column1 = t2.column2  AND t1.column2 = t2.column3
LEFT OUTER JOIN  table3 t3 ON t2.column2= t3.column2 AND t2.column3 = t3.column3
WHERE  t1.column1=45;

Example#2
SELECT  t1.column1, t2.column2, t3.column3
FROM table1 t1 INNER JOIN
table2 t2 ON t1.column1 = t2.column2  AND t1.column2 = t2.column3
INNER JOIN  table3 t3 ON t2.column2= t3.column2 AND t2.column3 = t3.column3
WHERE  t1.column1=45;
Note: SQL join works from left to right. In this case, it will first join table1 with table2 and then result of this will get joined with table3



No comments:

Post a Comment