Hi,
Can you help me in understanding Joins between three tables ?
Below are the ddl and queries
/*create the tables, we only need the one column*/
CREATE TABLE [dbo].[TableA]([c1] [tinyint] NOT NULL) ON [PRIMARY]
GO
CREATE TABLE [dbo].[TableB]([c1] [tinyint] NOT NULL) ON [PRIMARY]
GO
CREATE TABLE [dbo].[TableC]([c1] [tinyint] NOT NULL) ON [PRIMARY]
GO
/*populate the tables*/
SET NOCOUNT ON
INSERT INTO [dbo].[TableA] ([c1]) VALUES (1)
INSERT INTO [dbo].[TableA] ([c1]) VALUES (2)
INSERT INTO [dbo].[TableA] ([c1]) VALUES (3)
INSERT INTO [dbo].[TableA] ([c1]) VALUES (5)
INSERT INTO [dbo].[TableB] ([c1]) VALUES (1)
INSERT INTO [dbo].[TableB] ([c1]) VALUES (3)
INSERT INTO [dbo].[TableB] ([c1]) VALUES (4)
INSERT INTO [dbo].[TableB] ([c1]) VALUES (6)
INSERT INTO [dbo].[TableC] ([c1]) VALUES (1)
INSERT INTO [dbo].[TableC] ([c1]) VALUES (2)
INSERT INTO [dbo].[TableC] ([c1]) VALUES (4)
INSERT INTO [dbo].[TableC] ([c1]) VALUES (7)
GO
---Query 1
SELECT A.c1 as [A_val], B.c1 as [B_val], C.c1 as [C_val]
FROM TableA A LEFT JOIN TableB B ON A.c1 = B.c1
LEFT JOIN TableC C ON A.c1 = C.c1
---Query 2
SELECT A.c1 as [A_val], B.c1 as [B_val], C.c1 as [C_val]
FROM TableA A LEFT JOIN TableB B ON A.c1 = B.c1
LEFT JOIN TableC C ON B.c1 = C.c1
---Query 3
SELECT A.c1 as [A_val], B.c1 as [B_val], C.c1 as [C_val]
FROM TableA A inner JOIN TableB B ON A.c1 = B.c1
left JOIN TableC C ON A.c1 = C.c1
---Query 4
SELECT A.c1 as [A_val], B.c1 as [B_val], C.c1 as [C_val]
FROM TableA A INNER JOIN TableB B ON A.c1 = B.c1
LEFT JOIN TableC C ON B.c1 = C.c1
My confusion is, Result of Query 1 & 2 AND 3 & 4 are different and I cant really visualize the table joins.
I can understand all the joins between two tables, but three tables join is confusing.
Can you please give me some more detail or visualization or method of easy understanding for these join between 3 tables with different key columns?
Your help is appreciated.
Regards,
Revathy Menon
Can you help me in understanding Joins between three tables ?
Below are the ddl and queries
/*create the tables, we only need the one column*/
CREATE TABLE [dbo].[TableA]([c1] [tinyint] NOT NULL) ON [PRIMARY]
GO
CREATE TABLE [dbo].[TableB]([c1] [tinyint] NOT NULL) ON [PRIMARY]
GO
CREATE TABLE [dbo].[TableC]([c1] [tinyint] NOT NULL) ON [PRIMARY]
GO
/*populate the tables*/
SET NOCOUNT ON
INSERT INTO [dbo].[TableA] ([c1]) VALUES (1)
INSERT INTO [dbo].[TableA] ([c1]) VALUES (2)
INSERT INTO [dbo].[TableA] ([c1]) VALUES (3)
INSERT INTO [dbo].[TableA] ([c1]) VALUES (5)
INSERT INTO [dbo].[TableB] ([c1]) VALUES (1)
INSERT INTO [dbo].[TableB] ([c1]) VALUES (3)
INSERT INTO [dbo].[TableB] ([c1]) VALUES (4)
INSERT INTO [dbo].[TableB] ([c1]) VALUES (6)
INSERT INTO [dbo].[TableC] ([c1]) VALUES (1)
INSERT INTO [dbo].[TableC] ([c1]) VALUES (2)
INSERT INTO [dbo].[TableC] ([c1]) VALUES (4)
INSERT INTO [dbo].[TableC] ([c1]) VALUES (7)
GO
---Query 1
SELECT A.c1 as [A_val], B.c1 as [B_val], C.c1 as [C_val]
FROM TableA A LEFT JOIN TableB B ON A.c1 = B.c1
LEFT JOIN TableC C ON A.c1 = C.c1
---Query 2
SELECT A.c1 as [A_val], B.c1 as [B_val], C.c1 as [C_val]
FROM TableA A LEFT JOIN TableB B ON A.c1 = B.c1
LEFT JOIN TableC C ON B.c1 = C.c1
---Query 3
SELECT A.c1 as [A_val], B.c1 as [B_val], C.c1 as [C_val]
FROM TableA A inner JOIN TableB B ON A.c1 = B.c1
left JOIN TableC C ON A.c1 = C.c1
---Query 4
SELECT A.c1 as [A_val], B.c1 as [B_val], C.c1 as [C_val]
FROM TableA A INNER JOIN TableB B ON A.c1 = B.c1
LEFT JOIN TableC C ON B.c1 = C.c1
My confusion is, Result of Query 1 & 2 AND 3 & 4 are different and I cant really visualize the table joins.
I can understand all the joins between two tables, but three tables join is confusing.
Can you please give me some more detail or visualization or method of easy understanding for these join between 3 tables with different key columns?
Your help is appreciated.
Regards,
Revathy Menon