Categories
MSSQL Server

MSSQL JOIN Selection Queries

INNER JOIN: Only records with joing fields in both tables.
LEFT OUTER JOIN: All records from table 1 and only matching fields from table 2 that match on join field.
RIGHT OUTER JOIN: All records from table 2 and only matchin records from table 2 that match on join field

Below an example…

SELECT DISTINCT [Database].[dbo].[Table1].[Field1] AS [Table1-Field1]
,[Database].[dbo].[Table1].[Field2] AS [Table1-Field2]
,[Database].[dbo].[Table2].[Field3] AS [Table2-Field3]
FROM [Database].[dbo].[Table1]
INNER JOIN [Database].[dbo].[Table2] ON [Database].[dbo].[Table2].[Field0] = [Database].[dbo].[Table1].[Field0]
WHERE [Database].[dbo].[Table1].[Field1]  [Database].[dbo].[Table2].[Field3]
ORDER BY [Database].[dbo].[Table1].[Field1],[Database].[dbo].[Table1].[Field1];