ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Database Systems
    COMP2114
    Progress0 / 34 topics
    Topics
    1. Basic Database Concepts2. Database Approach vs File Based System3. Database Architecture4. Three Level Schema Architecture5. Data Independence6. Relational Data Model7. Attributes8. Schemas9. Tuples10. Domains11. Relation Instances12. Keys of Relations13. Integrity Constraints14. Relational Algebra15. Selection in Relational Algebra16. Projection in Relational Algebra17. Cartesian Product in Relational Algebra18. Types of Joins19. Normalization20. Functional Dependencies21. Normal Forms22. Entity-Relationship Model23. Entity Sets24. Attributes in Entity-Relationship Model25. Relationship in Entity-Relationship Model26. Entity-Relationship Diagrams27. Structured Query Language (SQL)28. Joins in SQL29. Sub-Queries in SQL30. Grouping and Aggregation in SQL31. Concurrency Control32. Database Backup and Recovery33. Indexes34. NoSQL Systems
    COMP2114›Joins in SQL
    Database SystemsTopic 28 of 34

    Joins in SQL

    6 minread
    1,030words
    Intermediatelevel

    Joins in SQL

    In SQL, joins are used to combine rows from two or more tables based on a related column between them. Joins are essential in relational databases because they allow users to retrieve related data that is distributed across multiple tables.

    There are several types of joins, each serving a different purpose depending on how you want to match rows between the tables. Below are the different types of joins in SQL:


    1. INNER JOIN

    • Definition: The INNER JOIN returns only the rows where there is a match in both tables. If there is no match, the row will not be included in the result.
    • Usage: This is the most commonly used join type, as it retrieves only the data that exists in both tables.

    Syntax:

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

    Example: Suppose we have two tables, Employees and Departments:

    • Employees:

      EmployeeID Name DepartmentID
      1 Alice 101
      2 Bob 102
      3 Charlie 103
    • Departments:

      DepartmentID DepartmentName
      101 HR
      102 IT
      104 Marketing

    To get the names of employees along with their department names (only for employees who have a department):

    SELECT Employees.Name, Departments.DepartmentName
    FROM Employees
    INNER JOIN Departments
    ON Employees.DepartmentID = Departments.DepartmentID;
    

    Result:

    Name DepartmentName
    Alice HR
    Bob IT

    2. LEFT JOIN (LEFT OUTER JOIN)

    • Definition: The LEFT JOIN (or LEFT OUTER JOIN) returns all the rows from the left table and the matching rows from the right table. If there is no match, NULL values are returned for columns from the right table.
    • Usage: Use this join when you want to include all records from the left table, regardless of whether there is a match in the right table.

    Syntax:

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

    Example: Using the same Employees and Departments tables, to get all employees, including those who don't belong to a department:

    SELECT Employees.Name, Departments.DepartmentName
    FROM Employees
    LEFT JOIN Departments
    ON Employees.DepartmentID = Departments.DepartmentID;
    

    Result:

    Name DepartmentName
    Alice HR
    Bob IT
    Charlie NULL

    In this case, Charlie doesn't have a matching department, so the DepartmentName is NULL.


    3. RIGHT JOIN (RIGHT OUTER JOIN)

    • Definition: The RIGHT JOIN (or RIGHT OUTER JOIN) returns all the rows from the right table and the matching rows from the left table. If there is no match, NULL values are returned for columns from the left table.
    • Usage: This join is less commonly used but is helpful when you need all records from the right table, regardless of matching rows in the left table.

    Syntax:

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

    Example: To get all departments and their employees (including departments that don't have any employees):

    SELECT Employees.Name, Departments.DepartmentName
    FROM Employees
    RIGHT JOIN Departments
    ON Employees.DepartmentID = Departments.DepartmentID;
    

    Result:

    Name DepartmentName
    Alice HR
    Bob IT
    NULL Marketing

    In this case, the Marketing department has no employees, so the Name column is NULL.


    4. FULL OUTER JOIN

    • Definition: The FULL OUTER JOIN returns all rows from both the left and right tables. If there is no match, NULL values are returned for missing matches on either side.
    • Usage: Use this join when you want to include all rows from both tables, whether there is a match or not.

    Syntax:

    SELECT columns
    FROM table1
    FULL OUTER JOIN table2
    ON table1.common_column = table2.common_column;
    

    Example: To get all employees and all departments, whether or not an employee is assigned to a department and whether a department has employees:

    SELECT Employees.Name, Departments.DepartmentName
    FROM Employees
    FULL OUTER JOIN Departments
    ON Employees.DepartmentID = Departments.DepartmentID;
    

    Result:

    Name DepartmentName
    Alice HR
    Bob IT
    Charlie NULL
    NULL Marketing

    In this case, Charlie has no department, and the Marketing department has no employees, so both sides contain NULLs where no match is found.


    5. CROSS JOIN

    • Definition: The CROSS JOIN returns the Cartesian product of both tables. This means that each row in the first table is combined with every row in the second table. It doesn't require a condition to join.
    • Usage: Use CROSS JOIN when you want to get all possible combinations of rows between two tables.

    Syntax:

    SELECT columns
    FROM table1
    CROSS JOIN table2;
    

    Example: To generate a list of all possible combinations between two tables, Employees and Departments:

    SELECT Employees.Name, Departments.DepartmentName
    FROM Employees
    CROSS JOIN Departments;
    

    Result:

    Name DepartmentName
    Alice HR
    Alice IT
    Alice Marketing
    Bob HR
    Bob IT
    Bob Marketing
    Charlie HR
    Charlie IT
    Charlie Marketing

    This will produce a result where each employee is paired with every department, regardless of their actual department assignment.


    6. SELF JOIN

    • Definition: A self join is a join where a table is joined with itself. It is useful when you have hierarchical relationships in a table, such as employees who manage other employees.
    • Usage: You join a table to itself by using aliases to differentiate between the instances of the same table.

    Syntax:

    SELECT A.column, B.column
    FROM table A, table B
    WHERE A.common_column = B.common_column;
    

    Example: Consider an Employees table where each employee may report to another employee:

    EmployeeID Name ManagerID
    1 Alice NULL
    2 Bob 1
    3 Charlie 1

    To get the names of employees and their managers:

    SELECT E1.Name AS Employee, E2.Name AS Manager
    FROM Employees E1
    LEFT JOIN Employees E2
    ON E1.ManagerID = E2.EmployeeID;
    

    Result:

    Employee Manager
    Alice NULL
    Bob Alice
    Charlie Alice

    In this example, E1 represents employees and E2 represents their managers. The result shows that Bob and Charlie report to Alice.


    Conclusion

    SQL joins are essential for retrieving related data from multiple tables in relational databases. Depending on your requirements, you can choose the appropriate type of join:

    • INNER JOIN: Only matching rows from both tables.
    • LEFT JOIN: All rows from the left table and matched rows from the right table.
    • RIGHT JOIN: All rows from the right table and matched rows from the left table.
    • FULL OUTER JOIN: All rows from both tables.
    • CROSS JOIN: Cartesian product of both tables.
    • SELF JOIN: Joining a table with itself to find relationships within the same table.

    Understanding these different join types is fundamental to querying relational databases effectively and efficiently.

    Previous topic 27
    Structured Query Language (SQL)
    Next topic 29
    Sub-Queries in SQL

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time6 min
      Word count1,030
      Code examples0
      DifficultyIntermediate