Full Join in SQL: A Concise Tutorial Just An Hour | ACTE
Full Join SQL

Full Join in SQL: A Concise Tutorial Just An Hour – FREE

Last updated on 09th Jul 2020, Blog, Tutorials

About author

Vikas Aswal (Senior Sql Database Administrator )

Vikas Aswal is a Senior SQL Database Administrator who has more than 6 years of experience in SQL Databases. and is also an expert in Datastage, Hadoop, Microsoft Power BI, MicroStrategy, OBIEE, and Cognos.

(5.0) | 16547 Ratings 1200

What does a SQL FULL JOIN return?

  • FULL JOIN returns all matching records from both tables whether the other table matches or not.
  • FULL JOIN can potentially return very large datasets.
  • FULL JOIN and FULL OUTER JOIN are the same.
sql-full-join

 The SQL FULL JOIN syntax

The general syntax is:

  1. SELECT column-names
  2. FROM table-name1 FULL JOIN table-name2
  3. ON column-name1 = column-name2
  4. WHERE condition

    Subscribe For Free Demo

    [custom_views_post_title]

    The general FULL OUTER JOIN syntax is:

    1. SELECT column-names
    2. FROM table-name1 FULL OUTER JOIN table-name2
    3. ON column-name1 = column-name2
    4. WHERE condition
    SUPPLIER
    Id
    CompanyName
    ContactName
    City
    Country
    Phone
    Fax
    CUSTOMER
    Id
    FirstName
    LastName
    City
    Country
    Phone

    SQL FULL JOIN Examples:

    Problem: Match all customers and suppliers by country.

    1. SELECT C.FirstName, C.LastName, C.Country AS CustomerCountry,
    2. S.Country AS SupplierCountry, S.CompanyName
    3. FROM Customer C FULL JOIN Supplier S
    4. ON C.Country = S.Country
    5. ORDER BY C.Country, S.Country

    This returns suppliers that have no customers in their country, and customers that have no suppliers in their country, and customers and suppliers that are from the same country.

    Result: 195 records

    What Makes A Join?

    Most subqueries can be rewritten as joins, and most joins can be rewritten as subqueries. A join defines two or more tables by a related column. Tables usually are joined on primary and foreign keys. For example, an employee table might have a primary key of an employee id column, while a timesheet table also has an employee id column that is a foreign key to the employee table. The SQL join can be written as “WHERE employee.empid = timesheet.empid” or “FROM employee JOIN timesheet ON (employee.empid = timesheet.empid).”

    Advantages and Disadvantages of Joins

    The main advantage of a join is that it executes faster. The performance increase might not be noticeable by the end user. However, because the columns are specifically named and indexed and optimized by the database engine, the retrieval time almost always will be faster than that of a subquery. There are also inner and outer joins, left and right joins, full joins and cross joins. A disadvantage of using joins is that they are not as easy to read as subqueries. Another disadvantage is that it can be confusing as to which join is the appropriate type of join to use to yield the correct desired result set.

    Different Types of SQL JOINs

    Here are the different types of the JOINs in SQL:

    • (INNER) JOIN: Returns records that have matching values in both tables
    • LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table
    • RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table
    • FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table

    SQL Full Join Syntax

    The syntax of the Full Join in SQL Server is as shown below:

    SQL Server FULL JOIN Syntax

    • SELECT Table1.Column(s), Table2.Column(s),
    • FROM Table1
    •  FULL 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
    •  FULL JOIN
    •      Table2 ON
    •    Table1.Common_Column = Table2.Common_Column

    For this SQL Server Full Join demonstration, We are going to use two tables (Employee and Department) present in our [SQL Server Tutorials] Database.

    Data present in the Employee Table is:

    SQL FULL JOIN 1

    Data present in the SQL Server Department Table is:

    Course Curriculum

    Get SQL Certification Courses with Industry Standard Syllabus

    Weekday / Weekend BatchesSee Batch Details
    SQL FULL JOIN 2

    Full Join Select All Columns

    The following Sql Server Full Outer Join Select all columns query will display all the columns and rows present in Employees and Department tables

    SQL Server FULL JOIN Example:

    • SELECT
    • FROM [Employee]
    •  FULL OUTER JOIN
    • [Department] ON
    • [Employee].[DepartID] = [Department].[id]

    OUTPUT

    sql full join 3
    • If you observe the above screenshot, Although We have 15 records in the Employee table, SQL Full Outer Join is displaying 17 records. It is because there are two records in the Department table, i.e., Department Id 3, 4 (Module Lead and Team Lead), so 15 + 2 = 17 total records.
    • For Department Id 3 and 4 (Module Lead and Team Lead), there are no matching records in the Employees table, so NULLS replaces them.
    • ID number 10, 11, 14 and 15 of [DepartID], id, [Department Name], it is displaying NULL Values. It is because the Department Id for them in the Employee table is NULLS, so there are no matching records in the right table.

    Full Join without Outer keyword

    As we said before, it is optional to use an Outer keyword in this Join type. Let me remove the Outer keyword, and work will FULL JOIN

    SQL Server FULL JOIN Example:

    • SELECT *
    • FROM [Employee]
    •  FULL JOIN
    •      [Department] ON
    •  [Employee].[DepartID] = [Department].[id]

    OUTPUT

    sql full join 7

    NOTE: 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 Full Join

    Full Join Select Few Columns

    Please place the required columns after the SELECT Statement to avoid unwanted columns in full outer join

    SQL Server FULL JOIN Example:

    • SELECT [FirstName]
    •       ,[LastName]
    •       ,[DepartmentName]
    • FROM [Employee]
    •   FULL JOIN
    •      [Department] ON
    •   [Employee].[DepartID] = [Department].[id]

    OUTPUT

    sql full join 4

    Above SQL Full Join query will perfectly work as long as the column names from both tables (Employee and Department) are different like above.

    What happens if we have the same Column names in both the tables? Well, you will end up in a mess. Let us see how to resolve the issue.

    Before we get into the solution, let me show you one practical example. As you can see, we are using the above query. Still, we added id from the department table as an additional column.

    SQL Server FULL JOIN Example:

    • SELECT [FirstName]
    •       ,[LastName]
    •       ,id
    •       ,[DepartmentName]
    • FROM [Employee]
    • FULL OUTER JOIN
    •      [Department] ON
    •            [Employee].[DepartID] = [Department].[id]

    As you can see from the below screenshot, it is throwing an error: Ambiguous column name id. It is because the id column is present in both Employee and department table. And SQL Server doesn’t know which column you are asking it to retrieve.

    sql full join 8

    To resolve this kind of issue, you always have to use the table name before the column name.

    The following Full outer Join query is using the ALIAS table name before the column names. By this approach, we can inform the SQL Server that we are looking for id column belonging to the department table.

    We can write the above query as:

    SQL Server FULL JOIN Example:

    • SELECT Emp.[FirstName] AS [First Name]
    •       ,Emp.[LastName] AS [Last Name]
    •   ,Dept.id 
    •       ,Dept.[DepartmentName] AS [Department Name]
    • FROM [Employee] AS Emp
    • FULL JOIN
    •      [Department] AS Dept ON
    •           Emp.[DepartID] = Dept.[id]

    OUTPUT

    sql full join 9

    SQL Full Join Where Clause

    Course Curriculum

    Enroll in SQL Training & Build Your Skills to Next Level

    • Instructor-led Sessions
    • Real-life Case Studies
    • Assignments
    Explore Curriculum

    The Full Outer Join also allows us to use Where Clause to restrict the number of rows returned by the Full Join. In this example, we will use that WHERE Clause along with the Full Join.

    SQL Server FULL JOIN Example:

    • SELECT Emp.[FirstName] AS [First Name]
    •       ,Emp.[LastName] AS [Last Name]
    •       ,Dept.[DepartmentName] AS [Department Name]
    • FROM [Employee] AS Emp
    •   FULL OUTER JOIN
    •      [Department] AS Dept ON
    •     Emp.[DepartID] = Dept.[id]
    • WHERE Dept.[DepartmentName] IS NOT NULL

    OUTPUT

    SQL-FULL-JOIN-10

    SQL Full Join Order By Clause

    The Full outer Join allows us to use Order By Clause in Full Join to rearrange the order of the records.

    SQL Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

    SQL Server FULL JOIN Example:

    • SELECT Emp.[FirstName] AS [First Name]
    •       ,Emp.[LastName] AS [Last Name]
    •       ,Dept.[DepartmentName] AS [Department Name]
    • FROM [Employee] AS Emp
    •   FULL OUTER JOIN
    •      [Department] AS Dept ON
    •     Emp.[DepartID] = Dept.[id]
    • ORDER BY [DepartmentName] ASC

    OUTPUT

    sql full join 6

    Are you looking training with Right Jobs?

    Contact Us
    Get Training Quote for Free