MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
These are types of SQL JOINs that allow you to combine rows from two or more tables based on a related column.
LEFT JOIN
A LEFT JOIN returns all the rows from the left table, and the matched rows from the right table. If there is no match, the result is NULL on the right side. Here's a basic example:
Consider two tables: employees
and departments
.
employees
table:
id | name | department_id |
---|---|---|
1 | John | 100 |
2 | Alice | 200 |
3 | Bob | 300 |
4 | Nancy | NULL |
departments
table:
id | name |
---|---|
100 | HR |
200 | Marketing |
300 | Engineering |
You can use a LEFT JOIN to join these two tables on the department_id
and id
fields like this:
SELECT employees.name, departments.name FROM employees LEFT JOIN departments ON employees.department_id = departments.id;
This will give you the following result:
name | name |
---|---|
John | HR |
Alice | Marketing |
Bob | Engineering |
Nancy | NULL |
Notice that the employee "Nancy" appears in the result, even though there is no matching department for her. This is because LEFT JOIN includes all records from the left table (employees
), even if there is no match in the right table (departments
).
RIGHT JOIN
A RIGHT JOIN works exactly the opposite way of a LEFT JOIN. It returns all the rows from the right table, and the matched rows from the left table. If there is no match, the result is NULL on the left side. Here's an example with the same tables:
SELECT employees.name, departments.name FROM employees RIGHT JOIN departments ON employees.department_id = departments.id;
This will give you the following result:
name | name |
---|---|
John | HR |
Alice | Marketing |
Bob | Engineering |
NULL | NULL |
This time, all departments are included in the result, even if there is no matching employee for them.
Keep in mind, MySQL supports several types of JOINs including LEFT JOIN, RIGHT JOIN, INNER JOIN and FULL JOIN, each with their own specific use cases. You should choose the type of JOIN based on what you need for your specific situation.
MySQL LEFT JOIN Example:
SELECT * FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name;
How to Use LEFT JOIN in MySQL:
SELECT column1, column2, ... FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name;
Left Outer Join in MySQL:
SELECT * FROM table1 LEFT OUTER JOIN table2 ON table1.column_name = table2.column_name;
Joining Multiple Tables Using LEFT JOIN in MySQL:
SELECT * FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name LEFT JOIN table3 ON table1.column_name = table3.column_name;
Filtering LEFT JOIN Results in MySQL:
SELECT * FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name WHERE table2.column_name IS NOT NULL;
MySQL RIGHT JOIN Example:
SELECT * FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name;
How to Use RIGHT JOIN in MySQL:
SELECT column1, column2, ... FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name;
Right Outer Join in MySQL:
SELECT * FROM table1 RIGHT OUTER JOIN table2 ON table1.column_name = table2.column_name;