Yogesh Chauhan's Blog

INNER JOIN in Postgres

in Postgres on December 17, 2020

The INNER JOIN keyword selects records that have matching values in both tables.

Basically, it looks like this.

Inner Join in PostgreSQL

The highlighted blue part from the image above is our query results when we apply INNER JOIN between 2 tables.

Syntax:


SELECT a.column2, b.column2
FROM table a
INNER JOIN table b
ON a.column = b.column;

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

INNER JOIN Query Example 2 tables

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 INNER JOIN orders o
ON c.customer_id = o.customer_id

//Output
contact_name.  order_date 
"Paul Henriot"	"1996-07-04"
"Karin Josephs"	"1996-07-05"
"Mario Pontes"	"1996-07-08"
...
...
830 rows

INNER JOIN Query Example 3 tables


SELECT c.contact_name, o.order_date, COUNT(od.product_id)
FROM customers c INNER JOIN orders o ON c.customer_id = o.customer_id
INNER JOIN order_details od ON od.order_id = o.order_id
GROUP BY od.order_id, c.contact_name, o.order_date

//Output
contact_name.    order_date.   count
"Paolo Accorti"	"1997-10-20"	2
"José Pedro Freyre"	"1997-05-28"	4
"Paula Wilson"	"1998-01-07"	1
...
...
830 rows

Of course, we can use ORDER BY and sort the query results the way we want.


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
Use eq() method in jQueryjQueryHow to use PHPMailer to send an Email via Gmail SMTP Server?PHPHow to hide a DIV on clicks outside of it using jQuery?jQueryHow does AdSense calculate page loading time?JavaScriptEssential SQL Commands We Need to KnowSQL/MySQLCREATE DATABASE in PostgreSQLPostgres