Working with Joins in Postgres

01.12.2022

Intro

Often when working with SQL we have multiple tables that are related. For example, we have have a user and their preferences in two separate tables. We can combine the information in both tables using SQL joins. In this article we will learn how to use PostgreSQL joins.

The Syntax

The basic syntax of using a Join is as follows:

select [columns] from table1
	JOIN table2 ON table1.column = table2.column.

Here we join two tables. We use the ON clause to determine how the two table match. For example, we may have a user_id column in both tables to join on.

There are four types of joins:

  • Inner Join
  • Left Join
  • Right Join
  • Cross Join

We can also join with another table, multiple tables, or on the same table, called a Self Join.

Getting Setup

For our setup, we will use docker compose to create a Postgres database and to connect phpmyadmin. Start by copying the following into a docker compose file called docker-compose.yml

version: '3'
 
services:
  db:
    image: 'postgres:latest'
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: username
      POSTGRES_PASSWORD: password
      POSTGRES_DB: default_database
    volumes:
      - psqldata:/var/lib/postgresql

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    links:
      - db
    environment:
      PMA_HOST: db
      PMA_PORT: 3306
      PMA_ARBITRARY: 1
    restart: always
    ports:
      - 8081:80

volumes:
  psqldata:

We can run this file, we can use docker-compose up. One this is done, open up phpmyadmin by going to http://localhost:8081.

You can then login by leaving the host empty and using the following credentials.

POSTGRES_USER: username
POSTGRES_PASSWORD: password

Creating a DB

CREATE TABLE members (
    member_id SERIAL PRIMARY KEY,
    name VARCHAR(100)
);

CREATE TABLE committees (
    committee_id SERIAL PRIMARY KEY,
    name VARCHAR(100)
);
INSERT INTO members(name)
VALUES('John'),('Jane'),('Mary'),('David'),('Amelia');

INSERT INTO committees(name)
VALUES('John'),('Mary'),('Amelia'),('Joe');

Inner Join Example

Let’s begin with an inner join. The inner join well select all rows that have a match between two tables and join them together. Using our example tables, if we have a member that is not in a committee, or a committee that does not have members, they will be ignored.

SELECT 
    m.member_id, 
    m.name as member_name, 
    c.committee_id, 
    c.name as committee_name
FROM
    members m
INNER JOIN committees c ON c.name = m.name;
member_id member_name committee_id committee_name
1 John 1 John
3 Mary 2 Mary
5 Amelia 3 Amelia

Left Join

In a left join, the first table rows will all be kept. Using our example below, members is the first table. So any member that is not in a committee will still be kept and the joined column values will be null.

SELECT 
    m.member_id, 
    m.name as member_name, 
    c.committee_id, 
    c.name as committee_name
FROM
    members m
LEFT JOIN committees c ON c.name = m.name;
member_id member_name committee_id committee_name
1 John 1 John
2 Jane
3 Mary 2 Mary
4 David
5 Amelia 3 Amelia

Right Join

The right join is the opposite of the left join. In our example, we will keep all committees that have no members.

SELECT 
    m.member_id, 
    m.name as member_name, 
    c.committee_id, 
    c.name as committee_name
FROM
    members m
RIGHT JOIN committees c ON c.name = m.name;
member_id member_name committee_id committee_name
1 John 1 John
3 Mary 2 Mary
5 Amelia 3 Amelia
4 Joe