Tuesday, March 9, 2010

Interesting Join in SQL Server 2005

The following shows that some interesting joins:

IF OBJECT_Id('tempdb..#Table1') IS NOT NULL
DROP TABLE #Table1

IF OBJECT_Id('tempdb..#Table2') IS NOT NULL
DROP TABLE #Table2

CREATE TABLE #Table1(ID int)

CREATE TABLE #Table2(ID int)


INSERT INTO #Table1 VALUES(1)
INSERT INTO #Table1 VALUES(1)
INSERT INTO #Table1 VALUES(1)
INSERT INTO #Table1 VALUES(1)
INSERT INTO #Table1 VALUES(1)


INSERT INTO #Table2 VALUES(1)
INSERT INTO #Table2 VALUES(1)
INSERT INTO #Table2 VALUES(1)
INSERT INTO #Table2 VALUES(1)

SELECT T1.*
FROM #Table1 T1
LEFT JOIN #Table2 T2 ON T1.ID = T2.ID

The output is 20 rows regardless of any type join you use here.

No comments: