YogeshChauhan.com
CROSS JOIN in Postgres
October 19, 2020

When you think of CROSS JOIN, think about Cartesian product. Because CROSS JOIN will give us a result set which is the number of rows in the first table multiplied by the number of rows in the second table.

Kind of like this.

CROSS JOIN in PostgreSQL

Syntax:


SELECT a.column2, b.column2
FROM table a
CROSS JOIN table b;

I am using alias as well in the syntax. You can use name of the table instead of alias. Alias makes it better to write the query. Learn more about alias in this post:

Column And Table Alias In Postgres

CROSS JOIN Examples

I am using the database for all examples. It is available on my Github public repo:


SELECT c.contact_name, o.order_date 
FROM customers c CROSS JOIN orders o;

//Output
contact_name.        order_date 
"Maria Anders"	     "1996-07-04"
"Ana Trujillo"	     "1996-07-04"
"Antonio Moreno"	 "1996-07-04"
...
...

Now, if we have small tables (with fewer rows), the result set will be easier to look at. We have two big tables here so our result set has thousands of rows.

But I just wanted to show what CROSS JOIN is so included the example. 

Use my database from Github link and play with the data. Let me know if you have any questions.

dreamhost

Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 4 How to use data-* Attributes in HTML? Mar 4 The substr() method in JavaScript and how it’s different from substring() Mar 4 A complete guide to add responsive YouTube videos using HTML and CSS Mar 3 How to embed YouTube or other video links in WordPress? Mar 3 How to change the Login Logo in WordPress? Mar 3 substring() Method in JavaScript

You might also like these

How to uninstall Cocoapods from the Mac OS?MiscPagination in CSS with multiple examplesCSSHow to list all PHP variables to debug the script?PHPHow to obfuscate JavaScript code to hide it from View Source?JavaScriptSQL Right JoinSQL/MySQLCan We Use For Loop to Loop Through Associative Arrays in PHP?PHP