Inner Join vs. Outer Join

Inner Join vs. Outer Join
Download PDF

A SQL Server inner join is a type of join that combines records from two or more tables in a database based on the values of the joined columns from each table. Inner joins only return rows that have matching values in the joined columns. In contrast, an outer join will return all rows from one table, and any matching rows from the other table(s).

Inner joins are useful when you want to return only the records that have a match in both tables, while outer joins can be useful when you want to include records from one table even if there is no matching record in the other table(s).

For example, let’s say you have a customers table and an orders table. An inner join between these two tables would return only those customers who have placed an order, while an outer join could return all customers, including those who have not placed any orders.

Here’s an example of a SQL query that uses an inner join:

SELECT *

FROM customers

INNER JOIN orders ON customers.customer_id = orders.customer_id

And here’s an example of a query that uses an outer join:

SELECT *

FROM customers

LEFT JOIN orders ON customers.customer_id = orders.customer_id

The main difference between the two queries is the use of the INNER JOIN and LEFT JOIN keywords, which specify the type of join to use. The inner join returns only the rows that have matching values in the joined columns, while the outer join returns all rows from the left table (customers) and any matching rows from the right table (orders).

Want to learn more about join types?

JOIN Types

http://SteveStedman.com/joins

 

More from Stedman Solutions:

SteveStedman5
Steve and the team at Stedman Solutions are here for all your SQL Server needs.
Contact us today for your free 30 minute consultation..
We are ready to help!

Leave a Reply

Your email address will not be published. Required fields are marked *

*