- SQL - Home
- SQL - Roadmap
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL - Comments
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Cheatsheet
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
Left Join vs Right Join
Both LEFT JOIN and RIGHT JOIN are types of outer joins in SQL. They are used to combine rows from two tables based on a related column, but they differ in which table's rows are preserved when there is no match.
- LEFT JOIN keeps all unmatched rows from the left table.
- RIGHT JOIN keeps all unmatched rows from the right table.
What is Left Join in SQL?
A LEFT JOIN (also called LEFT OUTER JOIN) in SQL is used to combine rows from two or more tables based on a related column between them.
- It returns all rows from the left table (the first table in the query), even if there are no matching rows in the right table.
- If a row in the left table does not have a corresponding match in the right table, the query will still include that row in the result, but the columns from the right table will contain NULL values.
- If there is a match, the query includes the combined row with values from both tables.
Syntax
Following is the basic syntax of Left Join in SQL:
SELECT table1.column1, table2.column2... FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name;
Example
The following example demonstrates a Left Join operation between two related tables. The first table stores salary information, while the second table stores marital status details.
Since Alex's marital status is not recorded in the second table, it appears as NULL in the result.
What is Right Join in SQL?
A RIGHT JOIN (also called RIGHT OUTER JOIN) in SQL is used to combine rows from two or more tables based on a related column.
- It returns all rows from the right table (the second table in the query), even if there are no matching rows in the left table.
- If a row in the right table does not have a corresponding match in the left table, the query will still include that row in the result, but the columns from the left table will contain NULL values.
- If there is a match, the query includes the combined row with values from both tables.
Syntax
Following is the basic syntax of a Right Join in SQL:
SELECT table1.column1, table2.column2... FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name;
Example
In this example, the Right Join operation is performed on the same tables. Since the join begins with the right table, any row in the left table without a matching record in the right table, such as Alex's row, is excluded from the final result.
The final table only consists of two rows as the right table consists of two rows only.
Difference Between Left Join and Right Join in SQL
Let us summarize all the differences between the Left Join and Right Join in the table below:
| Feature | Left Join | Right Join |
|---|---|---|
| Definition | Returns all records from the left table and matched records from the right table. If no match, NULL appears in the right table columns. | Returns all records from the right table and matched records from the left table. If no match, NULL appears in the left table columns. |
| Other Name | Also called Left Outer Join | Also called Right Outer Join |
| Unmatched Rows | Unmatched rows from the left table are included; unmatched rows from the right table are discarded. | Unmatched rows from the right table are included; unmatched rows from the left table are discarded. |
| SQL Syntax | SELECT * FROM TableA LEFT JOIN TableB ON TableA.id = TableB.id; | SELECT * FROM TableA RIGHT JOIN TableB ON TableA.id = TableB.id; |
| Transact-SQL Equivalent | *= can be used instead of LEFT JOIN | =* can be used instead of RIGHT JOIN |
| Use Case | Used when we want all data from the left table, whether or not matching data exists in the right table. | Used when we want all data from the right table, whether or not matching data exists in the left table. |
| Focus / Priority | Focuses on the left table; ensures left table rows are not lost. | Focuses on the right table; ensures right table rows are not lost. |
| Practical Example | List all employees and their department details, even if some employees are not assigned to any department. | List all departments and their employee details, even if some departments have no employees. |
Conclusion
LEFT JOIN and RIGHT JOIN are both types of outer joins in SQL used to combine data from two tables. The main difference lies in which table's rows are fully preserved when there is no matching record in the other table. LEFT JOIN preserves all rows from the left table, while RIGHT JOIN preserves all rows from the right table.
Choosing between them depends on the specific requirement of the query and which table's data you want to retain completely.