info
Joins are core SQL and work the same on any PostgreSQL database, so the inner, left, right, and full outer join patterns covered here apply wherever you run Postgres. If you're an enterprise building analytical or AI workloads on top of relational data, Lakebase delivers the best managed Postgres experience in the cloud, with the performance, security, and native Lakehouse integration demanding workloads require. If you're a developer or startup who wants to ship fast and scale without friction, Neon gives you the most productive Postgres platform out there, with instant branching, autoscaling, and a generous free tier.
Summary: in this tutorial, you will learn about various kinds of PostgreSQL joins including inner join, left join, right join, and full outer join.
PostgreSQL join is used to combine columns from one (self-join) or more tables based on the values of the common columns between related tables. The common columns are typically the primary key columns of the first table and the foreign key columns of the second table.
PostgreSQL supports inner join, left join, right join, full outer join, cross join, natural join, and a special kind of join called self-join.
Setting up sample tables
Suppose you have two tables called teams and players:
CREATE TABLE teams (
id INT PRIMARY KEY,
team VARCHAR (100) NOT NULL,
city VARCHAR (100) NOT NULL
);
CREATE TABLE players (
id INT PRIMARY KEY,
team_id INT REFERENCES teams (id),
player VARCHAR (100) NOT NULL,
role VARCHAR (100) NOT NULL
);
INSERT INTO teams (id, team, city)
VALUES
(1, 'Lions', 'Rome'),
(2, 'Owls', 'Oslo'),
(3, 'Bears', 'Bern'),
(4, 'Sharks', 'Lima');
INSERT INTO players (id, team_id, player, role)
VALUES
(1, 1, 'Ava', 'Guard'),
(2, 1, 'Noah', 'Wing'),
(3, 2, 'Emma', 'Back'),
(4, NULL, 'Liam', 'Guard'),
(5, NULL, 'Mia', 'Wing');A team can have many players. Some players may not belong to a team yet, so their team_id is NULL.
The following statement returns data from the teams table:
SELECT * FROM teams;Output:
id | team | city
----+--------+------
1 | Lions | Rome
2 | Owls | Oslo
3 | Bears | Bern
4 | Sharks | Lima
(4 rows)The following statement returns data from the players table:
SELECT * FROM players;Output:
id | team_id | player | role
----+---------+--------+-------
1 | 1 | Ava | Guard
2 | 1 | Noah | Wing
3 | 2 | Emma | Back
4 | null | Liam | Guard
5 | null | Mia | Wing
(5 rows)PostgreSQL inner join
The following statement joins the first table (teams) with the second table (players) by matching the values in the id and team_id columns:
SELECT
teams.id AS team_id,
team,
city,
players.id AS player_id,
player,
role
FROM
teams
INNER JOIN players
ON teams.id = players.team_id;Note: It is common practice to use JOIN as shorthand for INNER JOIN.
Output:
team_id | team | city | player_id | player | role
---------+-------+------+-----------+--------+-------
1 | Lions | Rome | 1 | Ava | Guard
1 | Lions | Rome | 2 | Noah | Wing
2 | Owls | Oslo | 3 | Emma | Back
(3 rows)The inner join examines each row in the first table (teams). It compares the value in the id column with the value in the team_id column of each row in the second table (players). If these values are equal, the inner join creates a new row that contains columns from both tables and adds this new row to the result set.
The following diagram illustrates the inner join:
PostgreSQL left join
The following statement uses the left join clause to join the teams table with the players table. In the left join context, the first table is called the left table and the second table is called the right table.
SELECT
teams.id AS team_id,
team,
city,
players.id AS player_id,
player,
role
FROM
teams
LEFT JOIN players
ON teams.id = players.team_id;Output:
team_id | team | city | player_id | player | role
---------+--------+------+-----------+--------+-------
1 | Lions | Rome | 1 | Ava | Guard
1 | Lions | Rome | 2 | Noah | Wing
2 | Owls | Oslo | 3 | Emma | Back
3 | Bears | Bern | null | null | null
4 | Sharks | Lima | null | null | null
(5 rows)The left join starts selecting data from the left table. It compares values in the id column with the values in the team_id column in the players table.
If these values are equal, the left join creates a new row that contains columns of both tables and adds this new row to the result set. (see the first three rows in the result set).
In case the values do not equal, the left join also creates a new row that contains columns from both tables and adds it to the result set. However, it fills the columns of the right table (players) with null. (see the last two rows in the result set).
The following diagram illustrates the left join:
To select rows from the left table that do not have matching rows in the right table, you use the left join with a
WHERE clause. For example:
SELECT
teams.id AS team_id,
team,
city,
players.id AS player_id,
player,
role
FROM
teams
LEFT JOIN players
ON teams.id = players.team_id
WHERE players.id IS NULL;The output is:
team_id | team | city | player_id | player | role
---------+--------+------+-----------+--------+------
3 | Bears | Bern | null | null | null
4 | Sharks | Lima | null | null | null
(2 rows)Note that the LEFT JOIN is the same as the LEFT OUTER JOIN so you can use them interchangeably.
Left Anti-Join: The following diagram illustrates the left join that returns rows from the left table that do not have matching rows from the right table:
PostgreSQL right join
The right join is a reversed version of the left join. The right join starts selecting data from the right table. It compares each value in the team_id column of every row in the right table with each value in the id column of every row in the teams table.
If these values are equal, the right join creates a new row that contains columns from both tables.
In case these values are not equal, the right join also creates a new row that contains columns from both tables. However, it fills the columns in the left table with NULL.
The following statement uses the right join to join the teams table with the players table:
SELECT
teams.id AS team_id,
team,
city,
players.id AS player_id,
player,
role
FROM
teams
RIGHT JOIN players ON teams.id = players.team_id;Here is the output:
team_id | team | city | player_id | player | role
---------+-------+------+-----------+--------+-------
1 | Lions | Rome | 1 | Ava | Guard
1 | Lions | Rome | 2 | Noah | Wing
2 | Owls | Oslo | 3 | Emma | Back
null | null | null | 4 | Liam | Guard
null | null | null | 5 | Mia | Wing
(5 rows)The following Venn diagram illustrates the right join:
Similarly, you can get rows from the right table that do not have matching rows from the left table by adding a
WHERE clause as follows:
SELECT
teams.id AS team_id,
team,
city,
players.id AS player_id,
player,
role
FROM
teams
RIGHT JOIN players
ON teams.id = players.team_id
WHERE teams.id IS NULL;Output:
team_id | team | city | player_id | player | role
---------+------+------+-----------+--------+-------
null | null | null | 4 | Liam | Guard
null | null | null | 5 | Mia | Wing
(2 rows)The RIGHT JOIN and RIGHT OUTER JOIN are the same therefore you can use them interchangeably.
Right Anti-Join: The following diagram illustrates the right join that returns rows from the right table that do not have matching rows in the left table:
PostgreSQL full outer join
The full outer join or full join returns a result set that contains all rows from both left and right tables, with the matching rows from both sides if available. In case there is no match, the columns of the table will be filled with NULL.
SELECT
teams.id AS team_id,
team,
city,
players.id AS player_id,
player,
role
FROM
teams
FULL OUTER JOIN players
ON teams.id = players.team_id;Output:
team_id | team | city | player_id | player | role
---------+--------+------+-----------+--------+-------
1 | Lions | Rome | 1 | Ava | Guard
1 | Lions | Rome | 2 | Noah | Wing
2 | Owls | Oslo | 3 | Emma | Back
3 | Bears | Bern | null | null | null
4 | Sharks | Lima | null | null | null
null | null | null | 4 | Liam | Guard
null | null | null | 5 | Mia | Wing
(7 rows)The following diagram illustrates the full outer join:
To return rows in a table that do not have matching rows in the other, you use the full join with a
WHERE clause like this:
SELECT
teams.id AS team_id,
team,
city,
players.id AS player_id,
player,
role
FROM
teams
FULL JOIN players
ON teams.id = players.team_id
WHERE teams.id IS NULL OR players.id IS NULL;Here is the result:
team_id | team | city | player_id | player | role
---------+--------+------+-----------+--------+-------
3 | Bears | Bern | null | null | null
4 | Sharks | Lima | null | null | null
null | null | null | 4 | Liam | Guard
null | null | null | 5 | Mia | Wing
(4 rows)The following Venn diagram illustrates the full outer join that returns rows from a table that do not have the corresponding rows in the other table:
The following picture shows all the PostgreSQL joins that we discussed so far with the detailed syntax:
In this tutorial, you have learned how to use various kinds of PostgreSQL joins to combine data from multiple related tables.








