PostgreSQL LEFT JOIN
A LEFT JOIN
returns all rows from the left table, plus matching rows from the right table. Rows in the left table without matches in the right table still appear, but with NULLs for right table columns.
Example
SELECT c.customer_id, c.first_name, o.order_id
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id;
Best Practices
- Use
LEFT JOIN
when you must keep all rows from the left table, regardless of a match. - Check for
NULL
values in the right table columns if you need to filter unmatched rows.
Key Takeaways
LEFT JOIN
includes all rows from the left table.- Non-matching rows in the left table get
NULL
values for right table columns.