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 JOINif you must keep all rows from the right table in the result set. - Be mindful of potential
NULLvalues in columns from the left table.
Key Takeaways
RIGHT JOINis the mirror operation ofLEFT JOIN.- Non-matching rows from the right table still appear with
NULLfor left columns.