TExcelTAD
Home › PostgreSQL › PostgreSQL RIGHT & FULL JOIN

PostgreSQL RIGHT & FULL JOIN

RIGHT JOIN is the mirror image of LEFT JOIN, and FULL JOIN combines both: it keeps unmatched rows from either side.

RIGHT JOIN

A RIGHT JOIN B keeps every row of B (the right table) and fills in A's columns where they match:

pgsql · try it yourself
SELECT b.title, cat.name AS category
FROM books b
RIGHT JOIN categories cat ON cat.id = b.category_id
WHERE cat.id >= 6
ORDER BY cat.name, b.title;
      title       | category
------------------+----------
 Moon Socks       | Children
 Ten Tiny Dragons | Children
                  | Poetry
(3 rows)

Poetry has no books, so it appears with an empty title. The same result comes from swapping the tables and writing a LEFT JOIN:

pgsql · try it yourself
SELECT b.title, cat.name AS category
FROM categories cat
LEFT JOIN books b ON b.category_id = cat.id
WHERE cat.id >= 6
ORDER BY cat.name, b.title;
      title       | category
------------------+----------
 Moon Socks       | Children
 Ten Tiny Dragons | Children
                  | Poetry
(3 rows)

Tip: Most people find left joins easier to read ("start from this table, add what you can find"). Many teams write only left joins and reorder the tables instead of using RIGHT JOIN.

FULL JOIN

A FULL JOIN (or FULL OUTER JOIN) returns matched pairs plus the unmatched rows from both tables. It shines when you compare two lists. Imagine two stock counts, one from the warehouse system and one from a manual count:

pgsql · try it yourself
CREATE TABLE system_count (book_id integer, qty integer);
CREATE TABLE manual_count (book_id integer, qty integer);
INSERT INTO system_count VALUES (1, 12), (2, 0), (3, 7), (4, 3);
INSERT INTO manual_count VALUES (1, 12), (3, 6), (4, 3), (5, 15);

SELECT coalesce(s.book_id, m.book_id) AS book_id,
       s.qty AS system_qty,
       m.qty AS manual_qty
FROM system_count s
FULL JOIN manual_count m ON m.book_id = s.book_id
ORDER BY book_id;
CREATE TABLE
CREATE TABLE
INSERT 0 4
INSERT 0 4
 book_id | system_qty | manual_qty
---------+------------+------------
       1 |         12 |         12
       2 |          0 |
       3 |          7 |          6
       4 |          3 |          3
       5 |            |         15
(5 rows)

Book 2 is only in the system, book 5 only in the manual count. coalesce takes the first non-NULL id, so every row has a book_id.

Showing only the differences

Add a WHERE that keeps rows that are missing on one side or disagree. IS DISTINCT FROM treats NULL as a normal value, so it also catches the one-sided rows:

pgsql · try it yourself
CREATE TABLE system_count (book_id integer, qty integer);
CREATE TABLE manual_count (book_id integer, qty integer);
INSERT INTO system_count VALUES (1, 12), (2, 0), (3, 7), (4, 3);
INSERT INTO manual_count VALUES (1, 12), (3, 6), (4, 3), (5, 15);

SELECT coalesce(s.book_id, m.book_id) AS book_id,
       s.qty AS system_qty, m.qty AS manual_qty,
       CASE WHEN s.book_id IS NULL THEN 'only manual'
            WHEN m.book_id IS NULL THEN 'only system'
            ELSE 'different' END AS problem
FROM system_count s
FULL JOIN manual_count m ON m.book_id = s.book_id
WHERE s.qty IS DISTINCT FROM m.qty
ORDER BY book_id;
CREATE TABLE
CREATE TABLE
INSERT 0 4
INSERT 0 4
 book_id | system_qty | manual_qty |   problem
---------+------------+------------+-------------
       2 |          0 |            | only system
       3 |          7 |          6 | different
       5 |            |         15 | only manual
(3 rows)

FULL JOIN on the sample data

Which countries do our authors come from, which countries do our customers live in, and where do both lists overlap? Each side is first summarised per country in a subquery, then the two summaries are fully joined:

pgsql · try it yourself
SELECT coalesce(a.country, c.country) AS country,
       a.authors,
       c.customers
FROM (SELECT country, count(*) AS authors FROM authors GROUP BY country) a
FULL JOIN (SELECT country, count(*) AS customers FROM customers GROUP BY country) c
       ON c.country = a.country
ORDER BY country;
   country   | authors | customers
-------------+---------+-----------
 Bulgaria    |       1 |
 Canada      |       1 |         1
 France      |         |         1
 Germany     |       1 |         1
 India       |       1 |
 Ireland     |         |         1
 Italy       |       1 |         1
 Japan       |       1 |         1
 Lebanon     |         |         1
 Mexico      |       1 |
 Nigeria     |       1 |
 Norway      |       1 |
 Poland      |         |         1
 Portugal    |         |         1
 South Korea |         |         1
 Sweden      |         |         1
 UK          |       1 |         3
 USA         |         |         1
(18 rows)

Rows with both numbers are countries that have authors and customers. A NULL on one side means the country appears in only one of the lists.

Join Unmatched left rows Unmatched right rows
INNER dropped dropped
LEFT kept dropped
RIGHT dropped kept
FULL kept kept

Test yourself

Which rows does A RIGHT JOIN B always keep?

  1. All rows of A
  2. All rows of B
  3. Only matching rows
  4. All rows of both

Which query is equivalent to A RIGHT JOIN B ON ...?

  1. A LEFT JOIN B ON ...
  2. B LEFT JOIN A ON ...
  3. A FULL JOIN B ON ...
  4. B INNER JOIN A ON ...

Why is coalesce(s.id, m.id) useful after a FULL JOIN?

  1. It removes duplicates
  2. One side's id is NULL for unmatched rows, so it picks whichever exists
  3. It sorts the rows
  4. It is required syntax