The SQL "join" refers to using the
JOIN keyword in a SQL statement in order to query data from two tables.
When you perform a SQL join, you specify one column from each table to join on. These two columns contain data that is shared across both tables.
You can use multiple joins in the same SQL statement to query data from as many tables as you like.
Depending on your requirements, you can do an "inner" join or an "outer" join. These are different in a subtle way
- Inner join
- This will only return rows when there is at least one row in both tables that match the join condition.
- Left outer join (or left join)
- This will return rows that have data in the left table (left of the
JOINkeyword), even if there's no matching rows in the right table.
- Right outer join (or right join)
- This will return rows that have data in the right table (right of the
JOINkeyword), even if there's no matching rows in the left table.
- Full outer join (or full join)
- This will return all rows, as long as there's matching data in one of the tables.
Example Inner Join Statement
Note: We could use table aliases instead of the full table name. This will keep our statement shorter. For example:
The next few lessons cover each type of join and show examples of usage.
Bonus! See my SQL joins tutorial over at Database.Guide for a visual guide to the different joins.