How to make an unconditional join in Redshift

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.

Older post

How to count the number of rows that match a condition in Redshift

How to count the rows by multiple conditions at the same time in SQL

Newer post

Get the last day of the month in Redshift

How to use the last_day function in Redshift