How to make an unconditional join in Redshift

This article is a part of my "100 data engineering tutorials in 100 days" challenge. (85/100)

Sometimes we need a weird kind of join operation, something that returns all of the values from the “left” table cross joined with all of the rows from the “right” table. However, it is not a cross join because the cross join would skip the not matched rows from the “left” table.

In such situations, we use a weird SQL expression that looks like this left_table LEFT OUTER JOIN right_table ON 1=1. What does it do? It returns the true for every combination of rows from both tables, so all rows from left_table will be matched with all rows from right_table. It is almost like a CROSS JOIN, but it will return everything from the left_table even if the right_table is empty.

Note that, in Redshift (and Postgres and any other database based on Postgres), we can make this expression more readable by typing left_table LEFT OUTER JOIN right_table ON true, which, in my opinion, makes the intent more explicit.

Subscribe to the newsletter and join the free email course.

Remember to share on social media!
If you like this text, please share it on Facebook/Twitter/LinkedIn/Reddit or other social media.

If you want to contact me, send me a message on LinkedIn or Twitter.

Would you like to have a call and talk? Please schedule a meeting using this link.

Bartosz Mikulski
Bartosz Mikulski * MLOps Engineer / data engineer * conference speaker * co-founder of Software Craft Poznan & Poznan Scala User Group

Subscribe to the newsletter and get access to my free email course on building trustworthy data pipelines.