PostgreSQL FULL OUTER JOIN

PostgreSQL FULL OUTER JOIN

PostgreSQL FULL OUTER JOIN

A FULL OUTER JOIN returns all rows from both tables, with matching rows from both sides wherever available. If there is no match, the result will contain NULL columns from the table without a matching row.

  • The key feature of FULL OUTER JOIN is that it includes all rows from both tables, regardless of whether a match exists between them.

1. Syntax of FULL OUTER JOIN

SELECT columns FROM table1 FULL OUTER JOIN table2 ON table1.column = table2.column;
  • table1.column and table2.column are the columns used to match the rows between the two tables.

2. Example 1: Simple FULL OUTER JOIN

Scenario:

Let’s assume we have two tables:

employees Table:

employee_idname
1Alice
2Bob
3Charlie

departments Table:

department_iddepartment_name
1HR
2IT
4Marketing

Query: Get All Employees and Departments, Even if They Don’t Match

SELECT employees.name, departments.department_name FROM employees FULL OUTER JOIN departments ON employees.employee_id = departments.department_id;

Result:

namedepartment_name
AliceNULL
BobNULL
CharlieNULL
NULLHR
NULLIT
NULLMarketing
  • Explanation:
    • The query matches rows based on employee_id (from employees) and department_id (from departments).
    • Since there are no matching employee_id and department_id, the result contains NULL for columns from the table without a match.

3. Example 2: FULL OUTER JOIN with Matching Rows

Scenario:

Consider the orders table:

orders Table:

order_idproduct_namecustomer_id
101Laptop1
102Phone2
103TabletNULL

customers Table:

customer_idcustomer_name
1Alice
2Bob
4Charlie

Query: Get All Orders and Customers, Including Orders Without Customers

SELECT orders.order_id, orders.product_name, customers.customer_name FROM orders FULL OUTER JOIN customers ON orders.customer_id = customers.customer_id;

Result:

order_idproduct_namecustomer_name
101LaptopAlice
102PhoneBob
103TabletNULL
NULLNULLCharlie
  • Explanation:
    • The first two rows match orders.customer_id with customers.customer_id.
    • The third row (order_id = 103) doesn’t have a matching customer_id, so the result shows NULL for the customer_name.
    • The fourth row (customer_id = 4) has no matching order, so the result shows NULL for the order_id and product_name.

4. Example 3: FULL OUTER JOIN with Both Tables Having Non-Matching Rows

Scenario:

Consider the employees table:

employees Table:

employee_idemployee_name
1Alice
2Bob

projects Table:

project_idproject_nameemployee_id
101Project A1
102Project B3

Query: Get All Employees and Projects, Even If There Is No Match

SELECT employees.employee_name, projects.project_name FROM employees FULL OUTER JOIN projects ON employees.employee_id = projects.employee_id;

Result:

employee_nameproject_name
AliceProject A
BobNULL
NULLProject B
  • Explanation:
    • The first row matches employee_id = 1 with project_id = 101 for Alice.
    • The second row shows Bob with NULL for project_name because there is no project for Bob.
    • The third row shows NULL for employee_name and Project B because there is no employee assigned to that project.

5. Key Points to Remember

  • Matching Rows: FULL OUTER JOIN returns rows where there is a match based on the specified column(s).
  • Non-Matching Rows: It also returns rows that do not match from both the left and right tables, filling in NULL for missing columns.
  • More Efficient than Multiple LEFT JOIN/RIGHT JOIN: When you need all rows from both tables, a FULL OUTER JOIN is more efficient than doing multiple LEFT JOIN and RIGHT JOIN queries.

6. Performance Considerations

  • Complexity: FULL OUTER JOIN can be computationally expensive, especially when joining large tables, as it must find all possible matches between rows.
  • Indexes: Consider indexing the columns used in the ON clause to optimize performance.

7. Summary of FULL OUTER JOIN

Join TypeDescription
FULL OUTER JOINReturns all rows from both tables. If there is no match, NULL values are filled in for missing columns.
Use CaseUseful when you need all records from both tables, regardless of whether a match exists.
PerformanceMay be slower on large tables, as it needs to match and include all rows from both tables.
Would you like to dive into more advanced use cases with FULL OUTER JOIN, or see a complex example? 🚀
Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close