Oracle SQL JOIN Types – Part 1

365 Days Of Daily Coding: Day 18

Today’s blog post is rather different. I will not be doing any challenges today but it’s about understanding different ‘JOIN’ types out there. Understanding the workings of ‘JOIN’ types is fundamental to using SQL efficiently.

Firstly, because it’s usage is required frequently if you are required to query database as part of your work. Secondly, also because data is almost always stored in different tables and querying databases from more than one tables requires the knowledge to know which ‘JOIN’ types to use.

Today’s post was inspired by Steve Stedman infographic I stumbled upon in my LinkedIn newsfeed. Check out his site too if you are interested.

This blog posts will include 4 ‘JOIN’ types and the rest 18 ‘JOIN’ types in the other.

Sample Schema

Table 1

IdNamefkfk_Table3
1Steve1NULL
2Aaron3NULL
3Mary2NULL
4Fred1NULL
5Anne5NULL
6Beth81
7JohnnyNULL1
8KerryNULL2
Table 1

Table 2

IdFavorite Color
1Red
2Green
3Blue
4Pink
5Purple
6Mauve
7Orange
8Yellow
1Indigo
Table 2

Table 3

IdData Value
1Pizza
2Burger
3Sushi
Table 3

Inorder to test the queries, I ran them in Oracle Live SQL. A temporary table can be created using ‘WITH’ function.

WITH table1 As(
Select 1 As Id, 'Steve' As Name, 1 As fk, NULL As fk_table3
From Dual UNION ALL
Select 2,       'Aron',          3 ,      NULL                 
From Dual UNION ALL
Select 3,       'Mary',          2,       NULL                 
From Dual UNION ALL
Select 4,       'Fred',          1,       NULL                 
From Dual UNION ALL
Select 5,       'Anne',          5,       NULL                 
From Dual UNION ALL
Select 6,       'Beth',          8,       1                    
From Dual UNION ALL
Select 7,       'Johnny',        NULL,    1                    
From Dual UNION ALL
Select 8,       'Kerry',         NULL,    2                    
From Dual
),
table2 As (
Select 1 As Id, 'Red' As FavoriteColor   From Dual UNION ALL
Select 2,       'Green'                  From Dual UNION ALL
Select 3,       'Blue'                   From Dual UNION ALL
Select 4,       'Pink'                   From Dual UNION ALL
Select 5,       'Purple'                 From Dual UNION ALL
Select 6,       'Mauve'                  From Dual UNION ALL
Select 7,       'Orange'                 From Dual UNION ALL
Select 8,       'Yellow'                 From Dual UNION ALL
Select 1,       'Indigo'                 From Dual 
),
table3 As (
Select 1 As Id, 'Pizza' As DataValue From Dual UNION ALL
Select 2,       'Burger'             From Dual UNION ALL
Select 3,       'Sushi'              From Dual
)
  1. Select from two tables
SELECT *
FROM Table1;
SELECT *
FROM Table2;

This is the classic ‘SELECT’ syntax to display all the columns and rows in Table1 and Table2. Please note that each ‘SELECT’ syntax need to be executed separately.

2. INNER JOIN

SELECT *
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.fk = t2.id 
ORDER BY t1.id;

‘INNER JOIN’ is the default JOIN in Oracle SQL. With this join, all the rows from Table 1 and Table 2 are selected based on the matching values on the common columns.

Output of the above query:

IdNamefkfk_Table3IdFavorite Color
1Steve11Red
1Steve11Indigo
2Aron33Blue
3Mary22Green
4Fred11Red
4Fred11Indigo
5Anne55Purple
6Beth818Yellow
Output of ‘INNER JOIN’

If you compare this with Table 1, you will find Mary’s record missing since the fk id for Mary is missing.

3. LEFT OUTER JOIN

SELECT *
FROM Table1 t1
LEFT OUTER JOIN Table2 t2
ON t1.fk = t2.id;

‘LEFT OUTER JOIN’ selects all the rows from an inner join and also all values in the left table that do not match to the right table.

Output of the above query:

IdNamefkfk_Table3IdFavorite Color
1Steve11Red
1Steve 11Indigo
2Aaron33Blue
3Mary22Green
4Fred11Red
4Fred11Indigo
5Anne55Purple
6Beth818Yellow
7Johnny1
8Kerry2
Output of the ‘LEFT OUTER JOIN’

In the above table, we can see that all the records from the ‘INNER JOIN’ and the record for Mary’s which didn’t have matching id between ‘Table 1’ and ‘Table 2’ were returned.

4. RIGHT OUTER JOIN

SELECT *
FROM Table1 t1
RIGHT OUTER JOIN Table2 t2
ON t1.fk = t2.id;

‘RIGHT OUTER JOIN’ selects all the rows from an inner join and also all values in the right table that do not match to the left table.

Output of the above query:

IdNamefkfk_Table3IdFavorite Color
1Steve11Red
1Steve11Indigo
2Aron313Blue
3Mary22Green
4Fred121Red
4Fred121Indigo
5Anne55Purple
6Beth818Yellow
4Pink
6Purple
7Orange
Output of the ‘RIGHT OUTER JOIN’

In the above table, we can see that all the records from the ‘INNER JOIN’ and the record for Table 2 were returned.

One response to “Oracle SQL JOIN Types – Part 1”

  1. […] with the Oracle SQL Join Types- Part 1 series, I have listed down the other 6 ‘JOIN’ types in […]

    Like

Leave a comment