PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
In PostgreSQL, as in many other relational database systems, the LEFT JOIN
or LEFT OUTER JOIN
operation returns all rows from the left table (the table listed before the JOIN keyword) and the matched rows from the right table (the table listed after the JOIN keyword). If there is no match for a particular row in the left table, the columns for the right table will contain NULL values.
SELECT columns FROM table1 LEFT JOIN table2 ON table1.column = table2.column;
Let's say we have two tables, students
and registrations
.
The students
table:
student_id | student_name |
---|---|
1 | Alice |
2 | Bob |
3 | Charlie |
The registrations
table:
reg_id | course | student_id |
---|---|---|
1 | Math | 1 |
2 | History | 1 |
3 | Science | 3 |
To find out which students are registered for which courses, and also include students who are not registered for any course, we'd use a LEFT JOIN:
SELECT s.student_name, r.course FROM students s LEFT JOIN registrations r ON s.student_id = r.student_id;
The result would be:
student_name | course |
---|---|
Alice | Math |
Alice | History |
Bob | NULL |
Charlie | Science |
Note that:
LEFT JOIN
returns all rows from the left table, whether or not there's a matching row in the right table.
If there's no match for a particular row in the left table, the result will contain NULL values for columns from the right table.
This type of join is particularly useful when you want to list all items from one table and any corresponding matches (or lack thereof) from another table.
PostgreSQL LEFT JOIN example:
LEFT JOIN
between two tables.SELECT * FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name;
LEFT JOIN vs. INNER JOIN in PostgreSQL:
LEFT JOIN
returns all records from the left table, while INNER JOIN
returns only the matching records.SELECT * FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name;
Multiple tables LEFT JOIN in PostgreSQL:
LEFT JOIN
to involve multiple tables.SELECT * FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name LEFT JOIN table3 ON table1.column_name = table3.column_name;
Filtering results with LEFT JOIN in PostgreSQL:
LEFT JOIN
results.SELECT * FROM orders LEFT JOIN customers ON orders.customer_id = customers.customer_id WHERE customers.country = 'USA';
Using aliases with LEFT JOIN in PostgreSQL:
LEFT JOIN
.SELECT * FROM employees AS e LEFT JOIN departments AS d ON e.department_id = d.department_id;
LEFT JOIN and WHERE clause in PostgreSQL:
WHERE
clause after a LEFT JOIN
.SELECT * FROM customers LEFT JOIN orders ON customers.customer_id = orders.customer_id WHERE orders.order_id IS NULL;
LEFT JOIN with aggregate functions in PostgreSQL:
LEFT JOIN
for summary data.SELECT department_name, COUNT(employee_id) AS employee_count FROM departments LEFT JOIN employees ON departments.department_id = employees.department_id GROUP BY department_name;
Nested LEFT JOIN in PostgreSQL:
LEFT JOIN
within another LEFT JOIN
for more complex relationships.SELECT * FROM orders LEFT JOIN customers ON orders.customer_id = customers.customer_id LEFT JOIN employees ON customers.salesperson_id = employees.employee_id;
Combining LEFT JOIN with other JOIN types in PostgreSQL:
LEFT JOIN
with other types like INNER JOIN
or RIGHT JOIN
.SELECT * FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name INNER JOIN table3 ON table1.column_name = table3.column_name;
LEFT JOIN on non-primary key columns in PostgreSQL:
LEFT JOIN
can be used on columns other than primary keys.SELECT * FROM employees LEFT JOIN departments ON employees.department_name = departments.department_name;
JOIN conditions and NULL values in LEFT JOIN in PostgreSQL:
LEFT JOIN
.SELECT * FROM orders LEFT JOIN customers ON orders.customer_id = customers.customer_id;
Using LEFT JOIN with subqueries in PostgreSQL:
LEFT JOIN
with subqueries for advanced queries.SELECT * FROM employees LEFT JOIN (SELECT employee_id, AVG(salary) AS avg_salary FROM salaries GROUP BY employee_id) AS avg_salaries ON employees.employee_id = avg_salaries.employee_id;
ORDER BY with LEFT JOIN in PostgreSQL:
ORDER BY
to sort results of a LEFT JOIN
.SELECT * FROM products LEFT JOIN reviews ON products.product_id = reviews.product_id ORDER BY products.product_name;
Comparing LEFT JOIN with EXISTS in PostgreSQL:
LEFT JOIN
with the EXISTS
condition.SELECT * FROM employees WHERE EXISTS (SELECT 1 FROM salaries WHERE salaries.employee_id = employees.employee_id);
LEFT JOIN and foreign key relationships in PostgreSQL:
LEFT JOIN
for querying tables with foreign key relationships.SELECT * FROM orders LEFT JOIN customers ON orders.customer_id = customers.customer_id;