How to Use JOINs in PostgreSQL

Galaxy Glossary

How do I use JOINs in PostgreSQL to combine data across tables?

JOINs combine rows from two or more tables based on a related column, enabling richer result sets.

Sign up for the latest in SQL knowledge from the Galaxy Team!

Description

What is a JOIN in PostgreSQL?

JOINs merge rows from two or more tables by evaluating a Boolean condition. They let you query related data without data duplication, keeping schemas normalized.

Which JOIN types are available?

PostgreSQL supports INNER, LEFT (OUTER), RIGHT (OUTER), FULL (OUTER), CROSS, and self-join patterns. Every type solves a specific need for matching or retaining non-matching rows.

How does INNER JOIN work?

INNER JOIN returns rows with matching values in both tables.Use it for most analytic queries where non-matches are irrelevant.

When should I use LEFT JOIN?

LEFT JOIN keeps all rows from the left table and adds NULLs for non-matches in the right table. Useful for "show all customers, even those without orders" style queries.

Can I join on multiple columns?

Yes.Provide a composite condition with AND operators, or use USING (col1, col2) when both tables share identical column names.

What is a NATURAL JOIN?

NATURAL JOIN automatically joins on columns with identical names. Avoid in production because implicit behavior can break when schemas change.

How do I alias tables for readability?

Add an alias right after each table: FROM orders AS o INNER JOIN customers AS c ON o.customer_id = c.id.Aliases shorten column references.

Is CROSS JOIN the same as Cartesian product?

Yes. CROSS JOIN combines every row in the first table with every row in the second.Use sparingly; result sets grow quickly.

What are performance best practices?

Ensure joined columns are indexed, avoid functions on join keys, filter early with WHERE, and only select required columns.

Example: combine customers with order totals

SELECT c.id,
c.name,
COALESCE(SUM(o.amount), 0) AS total_spent
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.id
GROUP BY c.id, c.name;

This query lists every customer and the sum of their orders, returning 0 for customers without orders.

How do I join more than two tables?

Chain JOIN clauses: FROM a JOIN b ON ...JOIN c ON ... . Each clause references tables introduced earlier, keeping logic modular.

What is a self-join?

A self-join joins a table to itself, helpful for hierarchical or adjacency-list data. Alias the table twice to differentiate sides.

Should I use USING or ON?

USING is shorter when column names match exactly.ON is more flexible, allowing expressions, inequalities, and differently-named columns.

How to avoid NULL pitfalls in OUTER JOINs?

Wrap nullable expressions with COALESCE, and apply WHERE filters on the correct side (place filters for outer table in JOIN … ON).

.

Why How to Use JOINs in PostgreSQL is important

How to Use JOINs in PostgreSQL Example Usage


SELECT o.id,
       c.name,
       p.title,
       o.quantity
FROM   orders      AS o
INNER JOIN customers AS c ON c.id = o.customer_id
INNER JOIN products  AS p ON p.id = o.product_id
WHERE  o.created_at >= CURRENT_DATE - INTERVAL '30 days';

How to Use JOINs in PostgreSQL Syntax


SELECT select_list
FROM   table1 [AS alias1]
       { INNER | LEFT [OUTER] | RIGHT [OUTER] | FULL [OUTER] | CROSS | NATURAL } JOIN table2 [AS alias2]
       [ON boolean_expression | USING (column_list)]
       [JOIN ...] -- chain additional joins
[WHERE conditions]
[GROUP BY ...]
[ORDER BY ...];

Common Mistakes

Frequently Asked Questions (FAQs)

Is OUTER keyword mandatory?

No. PostgreSQL treats LEFT JOIN and LEFT OUTER JOIN identically. The OUTER keyword is optional syntactic sugar.

Can I JOIN a table with a subquery?

Yes. Enclose the subquery in parentheses, alias it, and join as if it were a regular table.

How do I filter after an OUTER JOIN?

Place conditions on the outer table inside the JOIN ... ON clause to avoid unintentionally converting the result to an INNER JOIN.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo