Yogesh Chauhan's Blog

CROSS JOIN in Postgres

in Postgres on 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.


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
the box-sizing property in CSSCSSHow can one check to see if a remote file exists using PHP?PHPHow to use $INSERT command in Envision Basic?Envision BasicHow to Make CSS Lists Bullets Smaller?CSSHow to make HTML form interactive and using CSS?CSSLearn to Make a Simple Contact Us Form using PHP and PDO-MySQLPHP