The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables.
How does the syntax of the 1) inner join differentiate itself from the 2) outer join?
In SQL, the syntax for Inner Join and Outer Join, while similar in structure, fundamentally differs in how they handle the matching and inclusion of rows from the joined tables. Understanding these differences is crucial for any SQL practitioner.
- Inner Join Syntax:
- Outer Join Syntax:
- Outer Joins are of three types: Left Outer Join, Right Outer Join, and Full Outer Join. They are used to select rows from one table with matching rows from another. If there are no matches, the result set still includes rows from one or both tables (depending on the type of Outer Join), with NULLs in place of missing values.
- Left Outer Join (or Left Join):
- Right Outer Join (or Right Join):
- Full Outer Join (or Full Join):
In summary, the Inner Join is used when you need to return only the rows with matching values in both tables. In contrast, Outer Joins (Left, Right, or Full) are employed when you also need to include rows that do not have matching counterparts in one or both tables, filling in the gaps with NULL values where necessary. Understanding these differences is essential for effective data retrieval and manipulation in SQL.
Early on in this course, we investigated what it meant to have a normalized database and its associated tables. Although normalization is not an SQL statement, it is something that you use when you query a table with SQL. You need to understand the mechanics as they relate to how to use the information in the table and how to build your SQL statements. Joins, specifically the INNER and OUTER join approaches are much the same thing. An INNER join is the most common type of join. It simply returns the rows that match the WHERE clause, and entries that do not have matches will not be included in the results. An OUTER join will show you all of the rows from one or both of the tables, regardless of whether those rows match rows in the other table. In the next lesson, we will look at three types of
OUTER joins:
- LEFT,
- RIGHT, and
- FULL joins.