In today’s article, we will explain the general use of PostgreSQL Cross Join with examples.
PostgreSQL Cross Join returns cartesian product of Tables. That is, it returns all rows of the right table against each row in the left table.
General usage:
1 | SELECT *FROM table_1 AS takma_isim1 CROSS JOIN table_2 takma_isim2 |
For example :
1 | SELECT s.SehirIsmi,my.YiyecekIsmi FROM Sehirler s CROSS JOIN MeshurYiyecekler my; |
If we write ‘on’ statement like INNER JOIN instead of ‘where’ condition of CROSS JOIN and do the synchronization, it will work like INNER JOIN.
When we run the script below, you will see that CROSS JOIN with WHERE added returns the same result as INNER JOIN.
1 | SELECT s.SehirIsmi,my.YiyecekIsmi FROM Sehirler s CROSS JOIN MeshurYiyecekler my WHERE s.ID=my.SehirID |