MySQL Join

MySQL Join

MySQL JOIN

The JOIN The clause in MySQL combines rows from two or more tables based on a related column. Joins are essential when retrieving data from multiple tables in a relational database.


Types of Joins

  1. INNER JOIN: Returns only the rows with matching values in both tables.
  2. LEFT JOIN (or LEFT OUTER JOIN): Returns all rows from the left table and the matching rows from the right table. Unmatched rows from the right table will have NULL values.
  3. RIGHT JOIN (or RIGHT OUTER JOIN): Returns all rows from the right table and the matching rows from the left table. Unmatched rows from the left table will have NULL values.
  4. FULL JOIN (or FULL OUTER JOIN): Combines the results of both LEFT JOIN and RIGHT JOIN. Rows from either table that do not match will still appear, with NULL values for missing data (not supported natively in MySQL but can be simulated).
  5. CROSS JOIN: Returns the Cartesian product of two tables.
  6. SELF JOIN: Joins a table to itself.

1. INNER JOIN

Syntax

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

Example

Get a list of employees and their department names:

SELECT e.name AS employee_name, d.name AS department_name FROM employees e INNER JOIN departments d ON e.department_id = d.id;

Result

+----------------+------------------+ | employee_name | department_name | +----------------+------------------+ | Alice | HR | | Bob | IT | +----------------+------------------+

2. LEFT JOIN

Syntax

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

Example

Get a list of all employees, including those without departments:

SELECT e.name AS employee_name, d.name AS department_name FROM employees e LEFT JOIN departments d ON e.department_id = d.id;

Result

+----------------+------------------+ | employee_name | department_name | +----------------+------------------+ | Alice | HR | | Bob | IT | | Carol | NULL | +----------------+------------------+

3. RIGHT JOIN

Syntax

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

Example

Get a list of all departments, including those with no employees:

SELECT e.name AS employee_name, d.name AS department_name FROM employees e RIGHT JOIN departments d ON e.department_id = d.id;

Result

+----------------+------------------+ | employee_name | department_name | +----------------+------------------+ | Alice | HR | | Bob | IT | | NULL | Finance | +----------------+------------------+

4. FULL JOIN (Simulated)

Since MySQL does not support FULL JOIN natively, it can be simulated using UNION.

Example

Get all employees and departments, even if there are no matches:

SELECT e.name AS employee_name, d.name AS department_name FROM employees e LEFT JOIN departments d ON e.department_id = d.id UNION SELECT e.name AS employee_name, d.name AS department_name FROM employees e RIGHT JOIN departments d ON e.department_id = d.id;

5. CROSS JOIN

Syntax

SELECT columns FROM table1 CROSS JOIN table2;

Example

Combine all employees with all projects:

SELECT e.name AS employee_name, p.name AS project_name FROM employees e CROSS JOIN projects p;

Result

+----------------+----------------+ | employee_name | project_name | +----------------+----------------+ | Alice | Project A | | Alice | Project B | | Bob | Project A | | Bob | Project B | +----------------+----------------+

6. SELF JOIN

A self-join is when a table is joined to itself. It is useful for hierarchical or relational data.

Example

Find employees and their managers:

SELECT e.name AS employee_name, m.name AS manager_name FROM employees e LEFT JOIN employees m ON e.manager_id = m.id;

Best Practices for Using Joins

  1. Use Aliases:

    • Shorten table names for readability, e.g., employees e.
  2. Filter Early:

    • Use WHERE clauses to reduce the number of rows processed.
  3. Be Cautious with CROSS JOIN:

    • Ensure you need the Cartesian product; it can generate a massive result set.
  4. Index Related Columns:

    • Index the columns used in the ON condition for better performance.

Sample Data for Testing

Table: employees

+----+--------+---------------+ | id | name | department_id | +----+--------+---------------+ | 1 | Alice | 1 | | 2 | Bob | 2 | | 3 | Carol | NULL | +----+--------+---------------+

Table: departments

+----+-----------+ | id | name | +----+-----------+ | 1 | HR | | 2 | IT | | 3 | Finance | +----+-----------+

Let me know if you'd like further examples or need help with advanced queries!

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