ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCC-215Joins In Sql
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    CC-215

    Database Systems

    Progress0 / 34 done
    Topics34
    1Basic Database Concepts2Database Approach vs File Based System3Database Architecture4Three Level Schema Architecture5Data Independence6Relational Data Model7Attributes8Schemas9Tuples10Domains11Relation Instances12Keys of Relations13Integrity Constraints14Relational Algebra15Selection in Relational Algebra16Projection in Relational Algebra17Cartesian Product in Relational Algebra18Types of Joins19Normalization20Functional Dependencies21Normal Forms22Entity-Relationship Model23Entity Sets24Attributes in Entity-Relationship Model25Relationship in Entity-Relationship Model26Entity-Relationship Diagrams27Structured Query Language (SQL)28Joins in SQL29Sub-Queries in SQL30Grouping and Aggregation in SQL31Concurrency Control32Database Backup and Recovery33Indexes34NoSQL Systems
    6 min read
    Topic 28 of 34
    Topic Study Note Illustration
    28

    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...

    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 view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    6mremaining
    1.0kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Database Systems