All this computer hacking is making me thirsty

Inner vs Outer Joins

Posted by: ptilley125 on: March 28, 2009

An Inner Join returns only rows that match the given criteria whereas an Outer Join returns the rows with matching values and those that don’t have matching values.

To perform an inner join, use one of the following SQL statements:

SELECT * FROM table1, table2 WHERE expression
This is the old style that returns matching values from both tables that meet the expression.

SELECT * FROM table1 NATURAL JOIN table2
This is the natural join that returns matching values from both tables that have the same column names and data types.

SELECT * FROM table1 JOIN table2 USING column
This is the “join using” style that returns matching values from both tables that have the same column name.

SELECT * FROM table1 JOIN table2 ON expression
This is the “join on” style that returns matching values from both tables that meet the expression. This is the new-style for an inner join.

To perform an outer join, use the following SQL statement:

SELECT * FROM table1 [LEFT/RIGHT/ FULL] OUTER JOIN table2 ON expression

A left outer join returns all matching values between the two tables and those from table1 that do not have matching values. A right outer join returns all matching values between the two tables and those from table2 that do not have matching values. A full outer join returns all matching values between the two tables and those from both tables that do not have matching values. Do not include the brackets and choose only left, right, or full.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.