PostgreSQL - LEFT JOIN

Last Updated : 29 Jul, 2026

The LEFT JOIN in PostgreSQL returns all rows from the left table and the matching rows from the right table. If no matching row exists in the right table, the result contains NULL values for the right table columns. The LEFT JOIN helps in:

  • Returning all records from the left table.
  • Retrieving matching records from the right table.
  • Displaying NULL values when no matching record exists.
  • Finding records with or without related data.

Syntax

SELECT table1.column1, table1.column2, table2.column1, ...
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;

Example

Firstly, create the Customers and Orders tables and insert some records.

Customers Table

CustomerIDCustomerNameCity
101James CarterNew York
102Emily JohnsonChicago
103Michael BrownDallas
104Sophia DavisBoston

Orders Table

OrderIDProductNameCustomerID
1Laptop101
2Keyboard102
3Mouse101

The following query returns all customers along with their orders. Customers without any orders are also included in the result.

Query:

SELECT
c.CustomerName,
o.ProductName
FROM Customers c
LEFT JOIN Orders o
ON c.CustomerID = o.CustomerID;

Output:

CustomerNameProductName
James CarterLaptop
James CarterMouse
Emily JohnsonKeyboard
Michael BrownNULL
Sophia DavisNULL

Example: LEFT JOIN with WHERE Clause

The following query retrieves customers who have not placed any orders.

Query:

SELECT
c.CustomerName,
o.ProductName
FROM Customers c
LEFT JOIN Orders o
ON c.CustomerID = o.CustomerID
WHERE o.OrderID IS NULL;

Output:

CustomerNameProductName
Michael BrownNULL
Sophia DavisNULL
  • The LEFT JOIN returns all records from the Customers table.
  • Matching rows from the Orders table are included.
  • The WHERE o.OrderID IS NULL condition filters customers who do not have any matching orders.
Comment

Explore