Skip to main content

SQL FULL JOIN

SQL FULL JOIN

SQL FULL JOIN retrieves all records from both tables, matching records where available and returning NULL values for unmatched records.


1. Overview

SQL FULL JOIN is used to combine rows from two or more tables based on a related column, returning all records from both tables. If there is no match, NULL values are returned for the columns of the table with no matching record.

Example:

// Example of using FULL JOIN to retrieve data from two tables
SELECT customers.customer_name, orders.order_date
FROM customers
FULL JOIN orders ON customers.customer_id = orders.customer_id;

This example retrieves the customer names along with their order dates, including all orders and customers, even if there is no match.


2. Syntax

The syntax for SQL FULL JOIN is as follows:

SELECT columns
FROM table1
FULL JOIN table2 ON table1.column = table2.column;

Where table1 and table2 are the tables to be joined, and column is the related column that exists in both tables.


3. Usage

SQL FULL JOIN is typically used to retrieve all records from both tables, including matching records and NULL values for unmatched records. It is useful for situations where you want to include all records from both tables, regardless of whether they have corresponding matches.

Example:

// Example of using FULL JOIN to retrieve all records from both tables
SELECT customers.customer_name, orders.order_date
FROM customers
FULL JOIN orders ON customers.customer_id = orders.customer_id;

This example retrieves customer names along with their order dates, including all orders and customers, even if there is no match.


4. Conclusion

SQL FULL JOIN is a powerful tool in database querying, allowing users to retrieve all records from both tables, regardless of matching criteria. By combining data from multiple tables, FULL JOIN enables comprehensive analysis and reporting in database applications.

Comments