PostgreSQL RIGHT JOIN
A RIGHT JOIN
returns all rows from the right table, plus matching rows from the left table. Rows in the right table without matches in the left table still appear, with NULLs for left table columns.
Example
SELECT c.customer_id, o.order_id
FROM customers c
RIGHT JOIN orders o ON c.customer_id = o.customer_id;
Best Practices
- Use
RIGHT JOIN
if you must keep all rows from the right table in the result set. - Be mindful of potential
NULL
values in columns from the left table.
Key Takeaways
RIGHT JOIN
is the mirror operation ofLEFT JOIN
.- Non-matching rows from the right table still appear with
NULL
for left columns.