Summary: in this tutorial, you will learn various kind of Oracle joins that allow you to query data from two or more related tables.
Oracle join is used to combine columns from two or more tables based on values of the related columns. The related columns are typically the primary key column(s) of the first table and foreign key column(s) of the second table.
Oracle supports inner join, left join, right join, full outer join and cross join.
Note that you can join a table to itself to query hierarchical data using an inner join, left join, or right join. This kind of join is known as self-join.
Feb 26, 2020 SQL join tables with group by and order by Last update on February 26 2020 08:07:43 (UTC/GMT +8 hours) In this page, we are going to discuss the usage of GROUP BY and ORDER BY clause within a join.
We will create two new tables with the same structure for the demonstration:
The tables have some common colors such as Red
and Green
. Let’s call the palette_a
the left table and palette_b
the right table:
The following statement joins the left table to the right table using the values in the color
column:
Here is the output:
As can be seen clearly from the result, the inner join returns rows from the left table that match with the rows from the right table.
The following Venn diagram illustrates an inner join when combining two result sets:
The following statement joins the left table with the right table using a left join (or a left outer join):
The output is shown as follows:
The left join returns all rows from the left table with the matching rows if available from the right table. If there is no matching row found from the right table, the left join will have null values for the columns of the right table:
The following Venn diagram illustrates the left join:
Sometimes, you want to get only rows from the left table that do not exist in the right table. To achieve this, you use the left join and a WHERE
clause to exclude the rows from the right table.
For example, the following statement shows colors that only available in the palette_a
but not palette_b
:
Here is the output:
The following Venn diagram illustrates the left join with the exclusion of rows from the right table:
The right join or right outer join is a reversed version of the left join. The right join makes a result set that contains all rows from the right table with the matching rows from the left table. If there is no match, the left side will have nulls.
The following example use right join to join the left table to the right table:
Here is the output:
The following Venn diagram illustrates the right join:
Likewise, you can get only rows from the right table but not the left table by adding a WHERE clause to the above statement as shown in the following query:
Here is the output:
The following Venn diagram illustrates the right join with the exclusion of rows from the left table:
Oracle full outer join or full join returns a result set that contains all rows from both left and right tables, with the matching rows from both sides where available. If there is no match, the missing side will have nulls.
The following example shows the full outer join of the left and right tables:
The following picture illustrates the result set of the full outer join:
Note that the OUTER
keyword is optional.
The following Venn diagram illustrates the full outer join:
To get a set of rows that are unique from the left and right tales, you perform the same full join and then exclude the rows that you don’t want from both sides using a WHERE
clause as follows:
Here is the result:
The following Venn diagram illustrates the above operation:
In this tutorial, you have learned how to use various kinds of Oracle joins to query data from two tables.