Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

SQL LEFT JOIN

by suresh

The SQL Left Join returns all the rows or records present in the Left table and matching rows from the right table. The visual representation of the SQL Server Left Outer joins is

SQL LEFT OUTER JOIN

From the above image, the Sql Server Left Outer join displays all the records present in Table 1 and matching records from Table 2. All the Unmatched rows from the right table will fill with NULL Values.

SQL LEFT JOIN Syntax

The syntax of the SQL Server Left Join is

-- SQL Server LEFT JOIN Syntax
SELECT Table1.Column(s), Table2.Column(s),
FROM Table1
 LEFT OUTER JOIN
     Table2 ON
   Table1.Common_Column = Table2.Common_Column

--OR We can Simply Write it as
SELECT Table1. Column(s), Table2. Column(s),
FROM Table1
 LEFT JOIN
     Table2 ON
   Table1.Common_Column = Table2.Common_Column

The Left Outer join can also call as Left Join. So it is optional to use the Outer Keyword. For this Sql Server Left Join example, We use the Employee Table is:

SQL LEFT JOIN

Department Table

SQL LEFT JOIN

SQL Left Join Select All Columns

The following Left Outer Join Query will display all the columns present in Employees, and matching records from Department tables

-- SQL Server LEFT JOIN Example
SELECT *
FROM [Employee]
 LEFT OUTER JOIN
     [Department] ON
 [Employee].[DepartID] = [Department].[id]
SQL LEFT JOIN 1

Left Join avoid Outer keyword

Let me remove the Outer keyword, which is optional and work will LEFT JOIN

-- SQL Server LEFT JOIN Example
SELECT *
FROM [Employee]
 LEFT JOIN
     [Department] ON
 [Employee].[DepartID] = [Department].[id]
SQL LEFT JOIN Example 1

If you observe the above screenshot, We have 15 records in the Employee table. And the Left Outer Join is displaying 15 records, but for [DepartID], id, [Department Name], it is displaying NULL Values for ID numbers 10, 11, 14, and 15. It is because the Department Id for them in the Employee table is NULLS, so there are no matching records in the right table.

The [Department ID] column is repeated twice, which is annoying to the user. By selecting individual column names we can avoid unwanted columns. So, please avoid SELECT * Statements in Left Join

SQL Left Join Select Few Columns

Please place the required columns after the SELECT Statement to avoid unwanted SQL Server columns in Joins

-- SQL Server LEFT JOIN Example
SELECT [FirstName]
      ,[LastName]
      ,[DepartmentName]
FROM [Employee]
  LEFT JOIN
     [Department] ON
  [Employee].[DepartID] = [Department].[id]
SQL LEFT JOIN 2

The above query work as long as the column names from both tables are different like above. If we have the identical Column names in both the tables, left join will throw an error. For instance, we are using the above Left Join query. But, we added id from department table as an additional column.

-- SQL Server LEFT JOIN Example
SELECT [FirstName]
      ,[LastName]
      ,id
      ,[DepartmentName]
FROM [Employee]
LEFT OUTER JOIN
     [Department] ON
           [Employee].[DepartID] = [Department].[id]

As you can see, the left join is throwing an error: Ambiguous column name id. It is because the id column is present in both the tables. And SQL Server doesn’t understand which column you are requesting it to retrieve.

SQL LEFT JOIN Example 2

To resolve this sort of issue, forever use the table name before the column name. The following left Outer Join query is using the ALIAS table name before the column names. By this method, we can inform the SQL Server that we are looking for id column relating to the department table. We can rewrite the above query as:

-- SQL Server LEFT JOIN Example
SELECT Emp.[FirstName] AS [First Name]
      ,Emp.[LastName] AS [Last Name]
	  ,Dept.id 
      ,Dept.[DepartmentName] AS [Department Name]
FROM [Employee] AS Emp
LEFT JOIN
     [Department] AS Dept ON
          Emp.[DepartID] = Dept.[id]
SQL LEFT JOIN Example 3

Left Join Where Clause

The SQL Server Left Join also permits us to use Where Clause to limit the number of rows returned by the Left Outer Join. Here, we use WHERE Clause along with the Left Outer Join.

-- SQL Server LEFT JOIN Example
SELECT Emp.[FirstName] AS [First Name]
      ,Emp.[LastName] AS [Last Name]
      ,Dept.[DepartmentName] AS [Department Name]
FROM [Employee] AS Emp
LEFT JOIN
     [Department] AS Dept ON
          Emp.[DepartID] = Dept.[id]
WHERE Dept.[DepartmentName] IS NOT NULL
SQL LEFT JOIN Example 4

Left Join Order By Clause

The SQL Server Left Outer Join allows us to use Order By Clause in Left Outer Join to rearrange the order of the records.

-- SQL Server LEFT JOIN Example
SELECT Emp.[FirstName] AS [First Name]
      ,Emp.[LastName] AS [Last Name]
      ,Dept.[DepartmentName] AS [Department Name]
FROM [Employee] AS Emp
 LEFT JOIN
     [Department] AS Dept ON
    Emp.[DepartID] = Dept.[id]
ORDER BY [DepartmentName] ASC
SQL LEFT JOIN 3

Placed Under: SQL

  • Install SQL Server
  • Install SQL Management Studio
  • Uninstall Management Studio
  • Install AdventureWorks Database
  • SQL Management Studio Intro
  • Connect SQL with sqlcmd utility
  • SQL Attach Database
  • SQL Detach Database
  • SQL Restore Database
  • Restore Database using BAK
  • SQL Rename Database with Files
  • Get SQL Database Names
  • SQL Create Table
  • SQL Rename Table
  • SQL Alter Table
  • SQL Add Column
  • SQL Rename Column
  • Get SQL Table Names in a DB
  • Find SQL Table Dependencies
  • Rename SQL Table & Column
  • SQL Global & Local Temp Table
  • SQL Table Variable
  • SQL Derived Table
  • SQL DATALENGTH
  • SQL Data Types
  • DML, DDL, DCL & TCL Cmds
  • SQL Query Builder
  • SQL ALIAS
  • SQL SELECT Statement
  • SQL SELECT DISTINCT
  • SQL SELECT INTO Statement
  • SQL INSERT Statement
  • SQL INSERT INTO SELECT
  • SQL BULK INSERT or BCP
  • SQL UPDATE Statement
  • SQL UPDATE from SELECT
  • SQL DELETE Statement
  • SQL TRUNCATE Table
  • SQL CASE Statement
  • SQL MERGE Statement
  • SQL Subquery
  • SQL CTE
  • SQL PIVOT
  • SQL UNPIVOT
  • SQL Clauses Examples
  • SQL TOP Clause
  • SQL WHERE Clause
  • SQL ORDER BY Clause
  • SQL GROUP BY Clause
  • SQL HAVING Clause
  • SQL Primary Key
  • SQL Foreign Key
  • SQL Referential Integrity
  • SQL Check Constraint
  • SQL Unique Constraint
  • SQL Default Constraint
  • SQL Clustered Index
  • SQL Non Clustered Index
  • SQL Filtered Indexes
  • SQL COALESCE Function
  • SQL IS NOT NULL
  • SQL IS NULL Function
  • SQL ISNULL
  • SQL JOINS
  • SQL CROSS JOIN
  • SQL FULL JOIN
  • SQL SELF JOIN
  • SQL Outer Joins
  • SQL Cross Join Vs Inner Join
  • SQL LEFT JOIN
  • SQL RIGHT JOIN
  • SQL AND & OR Operators
  • SQL Arithmetic Operators
  • SQL BETWEEN Operator
  • SQL Comparison Operators
  • SQL LIKE
  • SQL EXCEPT
  • SQL EXISTS Operator
  • SQL NOT EXISTS Operator
  • SQL INTERSECT
  • SQL IN Operator
  • SQL NOT IN Operator
  • SQL UNION
  • SQL UNION ALL
  • SQL IF ELSE
  • SQL ELSE IF
  • SQL WHILE LOOP
  • SQL Nested While Loop
  • SQL BREAK Statement
  • SQL CONTINUE Statement
  • SQL GOTO Statement
  • SQL IIF Function
  • SQL CHOOSE Function
  • SQL Change Data Capture
  • SQL Table Partitioning
  • SQL Table Partition using SSMS
  • SQL TRY CATCH
  • SQL VIEWS
  • SQL User Defined Functions
  • SQL Alter User Defined Functions
  • SQL Stored Procedure Intro
  • Useful System Stored Procedures
  • SQL SELECT Stored Procedure
  • SQL INSERT Stored Procedure
  • SQL UPDATE Stored Procedure
  • Stored Procedure Return Values
  • Stored Procedure Output Params
  • Stored Procedure Input Params
  • Insert SP result into Temp Table
  • SQL Triggers Introduction
  • SQL AFTER INSERT Triggers
  • SQL AFTER UPDATE Triggers
  • SQL AFTER DELETE Triggers
  • SQL INSTEAD OF INSERT
  • SQL INSTEAD OF UPDATE
  • SQL INSTEAD OF DELETE
  • SQL STATIC CURSOR
  • SQL DYNAMIC CURSOR
  • SQL FORWARD_ONLY Cursor
  • SQL FAST_FORWARD CURSOR
  • SQL KEYSET CURSOR
  • SQL TRANSACTIONS
  • SQL Nested Transactions
  • SQL ACID Properties
  • Create SQL Windows Login
  • Create SQL Server Login
  • SQL Server Login Error
  • Create SQL Server Roles
  • SQL Maintenance Plan
  • Backup SQL Database
  • SQL Ranking Functions Intro
  • SQL RANK Function
  • SQL PERCENT_RANK Function
  • SQL DENSE_RANK Function
  • SQL NTILE Function
  • SQL ROW_NUMBER
  • SQL Aggregate Functions
  • SQL Date Functions
  • SQL Mathematical Functions
  • SQL String Functions
  • SQL CAST Function
  • SQL TRY CAST
  • SQL CONVERT
  • SQL TRY CONVERT
  • SQL PARSE Function
  • SQL TRY_PARSE Function
  • SQL Calculate Running Total
  • SQL Find Nth Highest Salary
  • SQL Reverse String
  • SQL FOR XML PATH
  • SQL FOR XML AUTO
  • SQL FOR XML RAW

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy