JOIN in SQL

JOIN in SQL

In SQL, a JOIN is used to combine rows from two or more tables based on a related column between them. It allows you to retrieve data from multiple tables in a single query, helping you understand how data in different tables is connected.


Key Points

  1. Combines Data Across Tables:
    Joins retrieve related information stored in separate tables.

  2. Requires a Common Key:
    The tables being joined must have a column (key) with related values.

  3. Different Types of Joins:
    Each type defines how rows are matched and included in the result:

    • INNER JOIN: Includes only matching rows.

    • LEFT JOIN (or LEFT OUTER JOIN): Includes all rows from the left table and matches from the right table (if any).

    • RIGHT JOIN (or RIGHT OUTER JOIN): Includes all rows from the right table and matches from the left table (if any).

    • FULL JOIN (or FULL OUTER JOIN): Includes all rows from both tables, with matches where possible.

    • CROSS JOIN: Creates a Cartesian product of the two tables.

    • SELF JOIN: Joins a table to itself.


Syntax

General Syntax

SELECT columns
FROM table1
JOIN_TYPE table2
ON table1.column_name = table2.column_name;

Examples of Different Joins

1. INNER JOIN

  • Returns rows with matching values in both tables.
SELECT students.name, scores.marks
FROM students
INNER JOIN scores
ON students.student_id = scores.student_id;
  • Combines students with their scores, excluding students without scores.

2. LEFT JOIN

  • Returns all rows from the left table and matches from the right table.
SELECT students.name, scores.marks
FROM students
LEFT JOIN scores
ON students.student_id = scores.student_id;
  • Includes all students, showing NULL for students without scores.

3. RIGHT JOIN

  • Returns all rows from the right table and matches from the left table.
SELECT students.name, scores.marks
FROM students
RIGHT JOIN scores
ON students.student_id = scores.student_id;
  • Includes all scores, showing NULL for scores without matching students.

4. FULL JOIN

  • Combines all rows from both tables, with matches where possible.
SELECT students.name, scores.marks
FROM students
FULL JOIN scores
ON students.student_id = scores.student_id;
  • Includes all students and scores, with NULL for unmatched rows.

5. CROSS JOIN

  • Produces a Cartesian product (all possible combinations of rows).
SELECT students.name, subjects.subject
FROM students
CROSS JOIN subjects;
  • Lists every student with every subject.

6. SELF JOIN

  • Joins a table to itself, useful for hierarchical data.
SELECT e1.name AS Employee, e2.name AS Manager
FROM employees e1
INNER JOIN employees e2
ON e1.manager_id = e2.employee_id;
  • Matches employees with their managers from the same table.

When to Use Joins

  • To combine related data stored in multiple tables.

  • To create complex reports or analyze relationships in data.

  • To handle hierarchical or relational data.


Common Mistakes to Avoid

  1. Missing ON Clause for Joins:
    Forgetting the ON clause in a join results in errors or unexpected results.

     SELECT * FROM students INNER JOIN scores; -- Error!
    

    Confusing INNER JOIN and OUTER JOIN:

    • Use INNER JOIN for matching rows only.

    • Use OUTER JOIN (e.g., LEFT, RIGHT, or FULL) when you want unmatched rows as well.

  2. Incorrect Join Condition:
    Ensure the ON clause uses the correct columns to match data.


Tips for Beginners

  • Start with INNER JOIN: It’s the simplest and most commonly used join.

  • Use Table Aliases: Shorten table names for clarity.

      SELECT s.name, sc.marks
      FROM students AS s
      INNER JOIN scores AS sc
      ON s.student_id = sc.student_id;
    

    Visualize Data: Draw tables and relationships to understand how data connects.


Example Combining Joins with Other Clauses

SELECT d.department_name, COUNT(e.employee_id) AS employee_count
FROM departments AS d
LEFT JOIN employees AS e
ON d.department_id = e.department_id
GROUP BY d.department_name
HAVING COUNT(e.employee_id) > 5
ORDER BY employee_count DESC;
  • Joins departments with employees.

  • Counts employees in each department.

  • Filters departments with more than 5 employees.

  • Sorts results by employee count in descending order.