vadnica-logo
X

MySQL JOIN - Combining Data from Multiple Tables

JOIN is an SQL command that allows combining data from two or more tables into one result. MySQL supports different types of JOIN commands:

  1. INNER JOIN: Returns only records that match in both tables
    1. Returns only rows where values match in both tables
    2. Most commonly used type of JOIN
    3. Example: List of employees with their departments
  2. LEFT JOIN (or LEFT OUTER JOIN): Returns all records from the left table and matching records from the right
    1. Returns all records from the left table and matching records from the right
    2. If no match, values from the right are NULL
    3. Useful for displaying all records from the main table
  3. RIGHT JOIN (or RIGHT OUTER JOIN): Returns all records from the right table and matching records from the left
    1. Returns all records from the right table and matching records from the left
    2. Works similar to LEFT JOIN, but keeps all records from the right table
    3. Less commonly used, usually LEFT JOIN can be used instead
  4. FULL JOIN: Returns all records from both tables (simulated with UNION in MySQL)

JOIN commands are used with ON or USING clause to specify join conditions.

  1. Using JOIN commands:
    1. Identify tables you want to join
    2. Choose appropriate JOIN type based on needs
    3. Define join condition with ON or USING
  2. Best practices
    1. Always use table aliases
    2. Clearly define join conditions
    3. Be careful with NULL values in outer joins
EXAMPLE
RESULT
  1. In this example, we have two tables:
    1. departments: contains data about departments (dept_id, dept_name)
    2. employees: contains data about employees (emp_id, name, dept_id, manager_id)
    The tables are linked via the dept_id field. Below is an example of an INNER JOIN:
    SELECT e.name, d.dept_name FROM employees e INNER JOIN departments d ON e.dept_id = d.dept_id;            
  2. INNER JOIN displays only those employees and departments where a match exists:
    SELECT e.name, d.dept_name FROM employees e INNER JOIN departments d ON e.dept_id = d.dept_id;            
  3. LEFT JOIN displays all employees, even if they do not have a department assigned:
    SELECT e.name, d.dept_name FROM employees e LEFT JOIN departments d ON e.dept_id = d.dept_id;            
  4. RIGHT JOIN displays all departments, even those without any employees:
    SELECT e.name, d.dept_name FROM employees e RIGHT JOIN departments d ON e.dept_id = d.dept_id;            
  5. CROSS JOIN displays all possible combinations of employees and departments:
    SELECT e.name AS employee, d.dept_name FROM employees e CROSS JOIN departments d;            
  6. Self JOIN on the employees table allows us to display managers:
    SELECT e1.name AS employee, e2.name AS manager FROM employees e1 LEFT JOIN employees e2 ON e1.manager_id = e2.emp_id ORDER BY e1.name;            

Thank you for visiting! Adding privacy policy.

© 2024 All rights reserved.

Vam je koda pomagala? Če želite podpreti moj trud pri pripravi vodičev in vzdrževanju strani, mi lahko namenite donacijo za kavo.