PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
Home / PostgreSQL Tutorial / PostgreSQL LEFT JOIN

PostgreSQL LEFT JOIN

Summary: in this tutorial, you will learn how to use PostgreSQL LEFT JOIN clause to select data from multiple tables.

Introduction to PostgreSQL LEFT JOIN clause

Suppose we have two tables: A and B.

A and B tables

The data in the B table relates to the data in the A table via the fkafield.

Each row in the A table may have zero or many corresponding rows in the B table. Each row in the B table has one and only one corresponding row in the A table.

If you want to select rows from the A table that have corresponding rows in the B table, you use the INNER JOIN clause.

If you want to select rows from the A table which may or may not have corresponding rows in the B table, you use the LEFT JOIN clause. In case, there is no matching row in the B table, the values of the columns in the B table are substituted by the NULL values.

The following statement illustrates the LEFT JOIN syntax that join A table to B table:

1
2
3
4
5
6
7
8
SELECT
A.pka,
A.c1,
B.pkb,
B.c2
FROM
A
LEFT JOIN B ON A .pka = B.fka;

To join the A table to the B table, you need to:

  • Specify the columns from which you want to select data in the SELECT clause.
  • Specify the left table i.e., A table where you want to get all rows, in the FROM clause.
  • Specify the right table i.e., B table in the LEFT JOIN clause. In addition, specify the condition for joining two tables.

The LEFT JOIN clause returns all rows in the left table ( A) that are combined with rows in the right table ( B) even though there is no corresponding rows in the right table ( B).

The LEFT JOIN is also referred as LEFT OUTER JOIN.

The following Venn diagram illustrates how the LEFT JOIN clause works. The intersection is the rows in the A table that have corresponding rows in the B table.

PostgreSQL LEFT JOIN Venn Diagram

PostgreSQL LEFT JOIN examples

Let’s take a look at the following ER diagram, which is a part of the DVD rental sample database.

Film and Inventory tables

Each row in the filmtable may have zero or many rows in the inventorytable. Each row in the inventorytable has one and only one row in the filmtable.

You use the LEFT JOIN clause to join filmtable to the inventorytable as follows:

1
2
3
4
5
6
7
SELECT
film.film_id,
film.title,
inventory_id
FROM
film
LEFT JOIN inventory ON inventory.film_id = film.film_id;

PostgreSQL LEFT JOIN two tables

Because some rows in the filmtable do not have corresponding rows in the inventorytable, the values of the inventory id are NULL.

You can add a WHERE clause to select only films that are not in the inventory as the following query:

1
2
3
4
5
6
7
8
9
SELECT
film.film_id,
film.title,
inventory_id
FROM
film
LEFT JOIN inventory ON inventory.film_id = film.film_id
WHERE
inventory.film_id IS NULL;

PostgreSQL LEFT JOIN WHERE IS NULL

This technique is useful when you want to select data in one table that does not have a match in another table.

In this tutorial, we have shown you how to use PostgreSQL LEFT JOIN clause to select rows from one table that may or may not have corresponding rows in another table.

Related Tutorials

  • PostgreSQL Cross Join By Example
  • PostgreSQL NATURAL JOIN Explained By Examples
  • PostgreSQL FULL OUTER JOIN
Previous Tutorial: PostgreSQL FULL OUTER JOIN
Next Tutorial: PostgreSQL NATURAL JOIN Explained By Examples

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete
  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • How To Change The Password of a PostgreSQL User
  • PostgreSQL AGE Function
  • PostgreSQL DATE_PART Function
  • PostgreSQL List Users
  • PostgreSQL NOW Function
  • PostgreSQL DATE_TRUNC Function
  • PostgreSQL TO_DATE Function: Convert String to Date
  • A Look at PostgreSQL User-defined Data Types
  • PostgreSQL Copy Database Made Easy
  • How to Get Table, Database, Indexes, Tablespace, and Value Size in PostgreSQL

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.