38

I am reading an SQL query in Redshift and can't understand the last part:

...
LEFT JOIN (SELECT MIN(modified) AS first_modified FROM user) ue
ON 1=1

What does ON 1=1 mean here?

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
kee
  • 10,969
  • 24
  • 107
  • 168
  • 2
    It just ensures the `join` will return a match -- `1=1` is the same as `true`. Given the subquery, it will only return a single row -- `min(modified)`. That value will be combined to the other joins. Almost acts like a `cross join`, but with only a single value. – sgeddes Feb 13 '16 at 01:11

4 Answers4

52

The intention is an unconditional LEFT JOIN, which is different from a CROSS JOIN in that all rows from the left table expression are returned, even if there is no match in the right table expression - while a CROSS JOIN drops such rows from the result. More on joins in the manual.

However:

1=1 is pointless in Postgres and all derivatives including Amazon Redshift. Just use true. This has probably been carried over from another RDBMS that does not support the boolean type properly.

... LEFT JOIN (SELECT  ...) ue ON true

Then again, LEFT JOIN is pointless for this particular subquery with SELECT MIN(modified) FROM user on the right, because a SELECT with an aggregate function (min()) and no GROUP BY clause always returns exactly one row. This case (but not other cases where no row might be found) can be simplified to:

... CROSS JOIN (SELECT MIN(modified) AS first_modified FROM user) ue
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
18

It's simply doing a cross join, which selects all rows from the first table and all rows from the second table and shows as cartesian product, i.e. with all possibilities.

JOIN (LEFT, INNER, RIGHT, etc.) statements normally require an 'ON ..." condition. Putting in 1=1 is like saying "1=1 is always true, do don't eliminate anything".

Buddy Yaussy
  • 535
  • 2
  • 7
  • 20
    It's not exactly `cross join`, because when right table returns no rows, `cross join` will return also no rows, but `left join on 1=1` will return all rows from left table with null values as a right table. So be carefull with rewriting. – dey Oct 12 '16 at 09:14
  • `join on true` and `left join on true` is different, as per https://stackoverflow.com/questions/21520048/what-means-table-a-left-outer-join-table-b-on-true – Sida Zhou Jun 08 '20 at 07:24
13

My answer illustrates on top of Erwin's answers with an example.

Suppose you have three tables A1, B1, & C1(empty)

A1 -

+-+
|a|
+-+
|2|
|1|
|3|
+-+

B1 -

+----+
|b   |
+----+
|a   |
|b   |
|c   |
|NULL|
+----+

C1 -

+----+
|c   |
+----+

When joining table A1 & B1, on 1=1 behaves same as CROSS JOIN.

select * from a1 left join b1 on 1=1;
select * from a1 cross join b1;

Result -

+-+----+
|a|b   |
+-+----+
|1|NULL|
|1|a   |
|1|b   |
|1|c   |
|2|NULL|
|2|a   |
|2|b   |
|2|c   |
|3|NULL|
|3|a   |
|3|b   |
|3|c   |
+-+----+

However, when we join A1 with C1, you get two different results

select * from a1 left join c1 on 1=1;

Result -

+-+----+
|a|c   |
+-+----+
|1|NULL|
|3|NULL|
|2|NULL|
+-+----+

For cross join -

select * from a1 cross join c1;

Result -

+-+-+
|a|c|
+-+-+
Pirate X
  • 3,023
  • 5
  • 33
  • 60
4

I believe its used to emulate cartesian join.

From your query, the least modified value (It will be just 1 element) will be assigned to all the records of the left table.

PS : Left join is not much useful here. Might as well just use inner join

data_set
  • 339
  • 2
  • 15