Updated March 8, 2023
Definition of Redshift join
Redshift join is used to combine multiple tables using a single query and also used to fetching data from multiple table which was we have used in the join query. We can join the two table on the basis of column, we need a column name from both the table to join the table. As per types of join, we are retrieving data from table, inner join is most usable and preferable in redshift. Inner join, left outer join, right outer join, full outer join, and cross join are the types of joins available.
Syntax:
Below is the syntax of redshift join are as follows.
1) Join the redshift table using inner join
Select name_of_table1.column_name1, name_of_table1.column_name2, …, name_of_table1.column_nameN name_of_table2.column_name1, name_of_table2.column_name2, …., name_of_table2.column_nameN from name_of_table1 INNER JOIN name_of_table2 ON name_of_table1.column_name = name_of_table2.column_name;
2) Join the redshift table using left outer join
Select name_of_table1.column_name1, name_of_table1.column_name2, …, name_of_table1.column_nameN name_of_table2.column_name1, name_of_table2.column_name2, …., name_of_table2.column_nameN from name_of_table1 LEFT OUTER JOIN name_of_table2 ON name_of_table1. column_name = name_of_table2.column_name;
3) Join the redshift table using right outer join
Select name_of_table1.column_name1, name_of_table1.column_name2, …, name_of_table1.column_nameN name_of_table2.column_name1, name_of_table2.column_name2, …., name_of_table2.column_nameN from name_of_table1 RIGHT OUTER JOIN name_of_table2 ON name_of_table1. column_name = name_of_table2.column_name;
4) Join the redshift table using full outer join
Select name_of_table1.column_name1, name_of_table1.column_name2, …, name_of_table1.column_nameN name_of_table2.column_name1, name_of_table2.column_name2, …., name_of_table2.column_nameN from name_of_table1 FULL OUTER JOIN name_of_table2 ON name_of_table1. column_name = name_of_table2.column_name;
5) Join the redshift table using cross join
Select name_of_table1.column_name1, name_of_table1.column_name2, …, name_of_table1.column_nameN name_of_table2.column_name1, name_of_table2.column_name2, …., name_of_table2.column_nameN from name_of_table1 CROSS JOIN name_of_table2;
Parameter description syntax of redshift join
1) Name of table1 – This is the name of first table which was we have using in join condition to join the two table.
2) Name of table2 – This is the name of second table which was we have using in join condition to join the two table.
3) Name of column – This is the name of column which was we have using to fetching the data from table. We are using multiple column from multiple table to fetch the data from table.
4) Matching column name – This is name of column on which we have applying the join condition.
5) Redshift join – There are multiple types of joins available to join the two table. We have using five types of join to join two or multiple tables in redshift.
How join works in Redshift?
At the time of joining two tables, we need to create primary key column on first table and need to give reference of that primary key to second table.
Below are the types of join available are as follows.
1) CROSS JOIN
2) RIGHT OUTER JOIN OR RIGHT JOIN
3) LEFT OUTER JOIN OR LEFT JOIN
4) FULL OUTER JOIN OR FULL JOIN
5) INNER JOIN
• Cross join is used to fetch the data from two tables with all the combination of rows from left as well as right tables.
• Redshift inner join is also known as self-join. This is the most common type of join at the time of using join in redshift.
• Inner join will return the all matching rows from multiple tables which was we have used in our query.
• At the time of joining the table using left outer join it will first doing the normal inner or self-join condition on both table after self-joining It will starting to scanning the row from left table.
• Left join is also called as left outer join in redshift. Left join will be retrieving all rows from left table and only matching rows from the right table. If suppose we have not found any match on both table, right table will be retrieving the null values.
• At the time of joining the table using right outer join in redshift, it will first doing the normal inner or self-join condition on both table after self-joining It will starting scanning the row from right table.
• Right join is also called as right outer join in redshift. Right join will be retrieving all rows from right table and only matching rows from the left table. If suppose we have not found any match on both table, left table will be retrieving the null values.
• Full outer join in redshift will returning all the rows from both table. It will be putting the null values when full join condition is not satisfied in our query.
Examples
Below is the example of join in redshift are as follows.
1) Redshift join using INNER join
- Below example shows redshift join using inner join are as follows. We have joining cust_test and cont_test table.
- We have applying join condition on cont_test.id and cust_test.id column.
Code:
Select cont_test.id, cont_test.phone, cust_test.id, cust_test.name from cont_test INNER JOIN cust_test ON cont_test.id = cust_test.id;
2) Redshift join using RIGHT OUTER join
We have joining cust_test and cont_test table. We have applying join condition on cont_test.id and cust_test.id column.
Code:
Select cont_test.id, cont_test.phone, cust_test.id, cust_test.name from cont_test RIGHT OUTER JOIN cust_test ON cont_test.id = cust_test.id;
3) Redshift join using LEFT OUTER join
We have joining cust_test and cont_test table. We have applying join condition on cont_test.id and cust_test.id column.
Code:
Select cont_test.id, cont_test.phone, cust_test.id, cust_test.name from cont_test LEFT OUTER JOIN cust_test ON cont_test.id = cust_test.id;
4) Redshift join using FULL OUTER join
We have joining cust_test and cont_test table. We have applying join condition on cont_test.id and cust_test.id column.
Code:
Select cont_test.id, cont_test.phone, cust_test.id, cust_test.name from cont_test FULL OUTER JOIN cust_test ON cont_test.id = cust_test.id;
5) Redshift join using CROSS join
Below example shows redshift join using cross join are as follows. We have joining cust_test and cont_test table. We have applying join condition on cont_test.id and cust_test.id column.
Code:
Select cont_test.id, cont_test.phone, cust_test.id, cust_test.name from cont_test CROSS JOIN cust_test;
Conclusion
Redshift joins is used to retrieve data from two or multiple tables using join conditions. Inner join, right outer join, left outer join, full outer join and cross join are the type of joins available in redshift. Join is very useful and important to join the tables in redshift.
Recommended Articles
This is a guide to Redshift join. Here we discuss definition, syntax, How join work in Redshift? examples with code implementation respectively. You may also have a look at the following articles to learn more –