T-SQL Tutorial

T-SQL Table Joins


This article provides an introduction to what T-SQL joins are and how to use them in the SQL Server database.
T-SQL Joins are used to return records from two or more tables in a SQL Server database.
A SQL join consists of a query that uses multiple tables.

Inner Join

The T-SQL Inner Join returns rows from two tables when a match is found.

SELECT a.column1, b.column1, b.column2
FROM table_A a, table_B b
WHERE a.column1 = b.column1
AND b.column2 > 100 ;


Left Join

The T-SQL Left Join returns all rows from the left table, even if there are no matches with the right table.

SELECT a.column1, a.column2, b.column1, b.column2
FROM table_A a LEFT JOIN table_B b
ON a.column1 = b.column1
ORDER BY a.column1;


Right Join

The T-SQL Right Join returns all rows from the right table, even if there are no matches with the left table.

SELECT a.column1, b.column1, b.column2
FROM table_A a RIGHT JOIN table_B b
ON a.column1 = b.column1
ORDER BY a.column1;


Self Join

The T-SQL Self Join is used to join a table to itself.

SELECT a.column1, b.column1, b.column2
FROM table_A a, table_A b
ON a.column1 = b.column1
ORDER BY a.column1;