SQLforGeeks
  • Home
  • SQL
  • SQL Tutorial
  • SQL Syntax
  • Our Services
Contact
  1. SQL
  2. SQL left join

Text copied!

« Previous
Next »

SQL left join

Dec. 27, 2022, 8:56 p.m. under SQL

  • Hi! You should check SQL inner join post first.

    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, the result will have null values for the right table columns.

    The syntax of the SQL left join generally looks like this :
    SELECT column_name(s)
    FROM table1
    LEFT JOIN table2
    ON table1.column_name = table2.column_name;

    • 'table1' and 'table2' refer to the two tables being joined together.

    • 'column_name(s)' refer to the selected columns from the tables being joined together.

    • 'ON' clause is used to specify the conditions for joining two or more tables together.

    SQL left join

    As seen in above image, SQL left join produces a result set that includes each row from the left table (Table-A) and any matching rows from the right table (Table-B), if any.

    Here's an example of how you might use the SQL left join :

    Let's suppose we have two tables :

    [a] Employees Table :

    → It contains columns [Employee_Id], [Employee_Name], [Gender] and [Department_Id].

    [b] Departments Table :

    → It contains columns [Department_Id] and [Department_Name].

    SQL left join

    1. Let's use SQL left join to retrieve the employees names and their respective department names by joining the [Employees] and [Departments] tables together based on matching values in both the tables.

    2. Please run the following SQL statement :

    SELECT Emp.Employee_Name, Emp.Department_Id, Dpt.Department_Name
    FROM [Employees] Emp
    LEFT JOIN [Departments] Dpt
    ON Emp.Department_Id = Dpt.Department_Id
            

    3. Above statement selects the columns from both tables based on the matching values in the 'DepartmentId' column. It returns all the rows from the left table and the matching rows from the right table as the output.

    SQL left join

    • As you have seen, Only specific selected columns ( [Employee_Name], [Department_Id] from "Employees" table and [Department_Name] from "Departments" table ) are returned.

    • It returned only the rows where the [Department_Id] column contained the values '1', '2', or '5'. This was done because '1' and '2' were the only values shared by both tables.

    • The left table ('Employees') also included '5' but with no matching 'Department_Name' value based on [Department_Id] column, resulting in a 'NULL' value for that column.

    That's it! You have successfully retrieved data using 'LEFT JOIN' statement.

    To enhance your understanding of 'SQL joins', be sure to check out our next tutorial.

    Frequently Asked Questions :

    What are left joins in SQL?

    Left joins in SQL retrieve all records from the left table and matching records from the right table, with null values for non-matching records.

    What are the 4 types of joins in SQL?

    The four types of joins in SQL are inner join, left join, right join, and full outer join.

    How do I left join between two tables in SQL?

    To left join between two tables in SQL, use the "LEFT JOIN" clause followed by the "ON" keyword specifying the join condition.

    What is the difference between inner join and left join?

    The difference between inner join and left join lies in their behavior: inner join returns only matching records from both tables, while left join returns all records from the left table and matching records from the right table.
    Thank You! You should check SQL right join post next.
    « Previous
    Next »
    RELATED :

    What is SQL

    What is T-SQL

    Difference between DBMS and Data Warehouse

    Download SQL Server

    Install SQL Server

    Download SQL Server Management Studio SSMS

    SQL Server Management Studio

    SQL Database

    Download database

    Restore database

    Backup database

    Attach database

    Detach database

    Create database

    Delete database

    Rename database

    Select database

    Database offline

    Database online

    SQL Commands

    SQL Tables

    Create table

    Truncate table

    Delete table

    Rename table

    Select table

    Alter table

    SQL Data Types

    SQL Comments

    SQL Constraints

    SQL Joins

    SQL inner join

    SQL right join

    SQL full join

    SQL cross join

    SQL self join

    INSERT INTO SELECT statement

    INSERT INTO statement

    SQL Clauses

    SELECT clause

    FROM clause

    WHERE clause

    GROUP BY clause

    HAVING clause

    ORDER BY clause

    JOIN clause

    UNION clause

    UNION ALL clause

    TOP clause

    DISTINCT clause

    SQL Operators

    SQL Arithmetic operators

    SQL Comparison operators

    SQL Logical operators

    UNION operator

    UNION ALL operator

    INTERSECT operator

    EXCEPT operator

    LIKE operator

    NOT LIKE operator

    IN operator

    NOT IN operator

    IS NULL operator

    IS NOT NULL operator

    EXISTS operator

    NOT EXISTS operator

    BETWEEN operator

    NOT BETWEEN operator

    SQL Functions

    SQL Built-In functions

    CHARINDEX function

    DATEADD function

    CONCAT function

    LEN function

    REPLACE function

    SUBSTRING function

    CASE statement

    GETDATE function

    DATEPART function

    DATEDIFF function

    CAST function

    TRY_CAST function

    CONVERT function

    TRY_CONVERT function

    ISNULL function

    NULLIF function

    COALESCE function

    SQL Window functions

    ROW_NUMBER function

    RANK function

    DENSE_RANK function

    IIF function

    CHOOSE function

    SQL Store Procedure

    Store Procedure vs. Function

    SQL Subquery

    SQL Aliases

    Temp table

    SQL Error Handling

    SQL Variables

    SQL Views

    SQL Merge

    SQL CTE

    SQL WITH TIES

    Define Transaction in DBMS

    ACID properties in DBMS

    Types of Triggers in DBMS


    • Have Some Questions?
    logo

    Elevate your data experience with SQL excellence

    Quick Links
    •  Home
    •  SQL Tutorial
    •  SQL Syntax
    •  Our Services
    Our Services
    • Web Development
    • BI Development
    • Data Warehousing
    • Data Integration ETL


    Follow Us

    GST Registered: XXAXXXXXXXZX
    Mumbai, Maharashtra, India support@sqlforgeeks.com

    © 2025 Copyright™ | All Rights Reserved | Privacy Policy