JOINS in SQL Server: Types with Examples

โšก Smart Summary

JOINS in SQL Server combine rows from two or more tables based on a related column, using INNER, LEFT OUTER, RIGHT OUTER, and FULL OUTER joins to control which matching and non-matching rows appear in the result.

  • ๐Ÿ”— Purpose: A JOIN retrieves related data from two or more tables by matching a common column, usually a key.
  • ๐ŸŽฏ INNER JOIN: INNER JOIN returns only the rows where the join condition is true in both tables.
  • โฌ…๏ธ LEFT OUTER JOIN: LEFT OUTER JOIN returns every row from the left table, filling unmatched right-table columns with NULL.
  • โžก๏ธ RIGHT OUTER JOIN: RIGHT OUTER JOIN returns every row from the right table, filling unmatched left-table columns with NULL.
  • ๐Ÿ”„ FULL OUTER JOIN: FULL OUTER JOIN returns all rows from both tables, using NULL wherever the condition is not met.
  • ๐Ÿ”‘ ON condition: The ON clause names the columns that link the tables, most often a primary-key and foreign-key pair.

JOINS in SQL Server: INNER, LEFT, RIGHT, and FULL OUTER JOIN Examples

What are Joins in SQL Server?

Data can be retrieved from more than one table using the JOIN statement in SQL Server. A JOIN combines rows from two or more tables based on a related column between them, most often a primary key matched to a foreign key. There are mainly four types of JOINs in SQL Server:

  • INNER JOIN / simple join
  • LEFT OUTER JOIN / LEFT JOIN
  • RIGHT OUTER JOIN / RIGHT JOIN
  • FULL OUTER JOIN

The table below summarizes which rows each JOIN type returns:

JOIN type Rows returned
INNER JOIN Only rows where the join condition matches in both tables.
LEFT OUTER JOIN All rows from the left table, plus matching right-table rows (NULLs where none match).
RIGHT OUTER JOIN All rows from the right table, plus matching left-table rows (NULLs where none match).
FULL OUTER JOIN All rows from both tables, with NULLs wherever the condition is not met.

The examples below use two tables, Students and Fee, joined on their common admission column. Each section explains one JOIN type with its syntax, a worked query, and the result.

INNER JOIN

This type of SQL Server JOIN returns rows from all tables in which the join condition is true. It takes the following syntax:

SELECT columns
FROM table_1 
INNER JOIN table_2
ON table_1.column = table_2.column;

We will use the following two tables to demonstrate this. The Students table below lists each student and admission number:

Students table listing admission, firstName, and lastName columns

The Fee table below records the amount each admission number has paid:

Fee table listing admission numbers and the amount_paid column

The following command demonstrates an INNER JOIN in SQL Server with an example:

SELECT Students.admission, Students.firstName, Students.lastName, Fee.amount_paid
FROM Students
INNER JOIN Fee
ON Students.admission = Fee.admission

The command returns the following result, showing only admission numbers present in both tables:

INNER JOIN result showing only students whose admission appears in both tables

We can tell which students have paid their fee. We used the column with common values in both tables, which is the admission column.

LEFT OUTER JOIN

This type of join returns all rows from the left-hand table plus the records in the right-hand table with matching values. For example:

SELECT Students.admission, Students.firstName, Students.lastName, Fee.amount_paid
FROM Students
LEFT OUTER JOIN Fee
ON Students.admission = Fee.admission

The code returns the following result, keeping every student even when no fee record matches:

LEFT OUTER JOIN result keeping every Students row with NULL for unmatched fees

The records without matching values are replaced with NULLs in the respective columns.

RIGHT OUTER JOIN

This type of join returns all rows from the right-hand table and only those with matching values in the left-hand table. For example:

SELECT Students.admission, Students.firstName, Students.lastName, Fee.amount_paid
FROM Students
RIGHT OUTER JOIN Fee
ON Students.admission = Fee.admission

The statement for OUTER JOINS in SQL Server returns the following result:

RIGHT OUTER JOIN result keeping every Fee row and matching Students rows

The reason for the above output is that all rows in the Fee table are available in the Students table when matched on the admission column.

FULL OUTER JOIN

This type of join returns all rows from both tables, with NULL values wherever the JOIN condition is not true. For example:

SELECT Students.admission, Students.firstName, Students.lastName, Fee.amount_paid
FROM Students
FULL OUTER JOIN Fee
ON Students.admission = Fee.admission

The code returns the following result for FULL OUTER JOIN queries in SQL, combining every row from both tables:

FULL OUTER JOIN result combining all Students and Fee rows with NULLs for gaps

FAQs

A CROSS JOIN returns the Cartesian product of two tables, pairing every row of the first table with every row of the second. It has no ON condition, so a 10-row and 5-row table produce 50 rows.

You use a SELF JOIN: the table is listed twice with different aliases, then joined on a related column to compare rows within the same table. It is common for hierarchical data, such as matching each employee to their manager.

A JOIN combines columns from related tables side by side, matching rows on a condition. UNION stacks the results of two SELECT queries vertically, adding rows. JOIN widens the result; UNION lengthens it.

Yes. The ON clause can test several columns joined with AND, for example ON a.col1 = b.col1 AND a.col2 = b.col2. Composite conditions are common when a single column does not uniquely identify a row.

The ON clause defines how the tables are matched and is applied while the join runs. The WHERE clause filters the combined result afterward. With OUTER JOINs, moving a condition between them can change the rows returned.

Yes. Writing JOIN without a prefix defaults to INNER JOIN, so it returns only matching rows. Adding LEFT, RIGHT, or FULL before OUTER JOIN changes which non-matching rows are kept in the result.

Yes. GitHub Copilot can draft INNER, LEFT, RIGHT, and FULL OUTER JOIN queries from a natural-language prompt. Always review the ON condition and join type so the result set matches what you intended.

AI and machine-learning assistants suggest the correct join type, generate multi-table ON conditions, and flag missing keys or accidental Cartesian products. They can also recommend indexes for join columns. The developer verifies each suggestion before running it.

Summarize this post with: