Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 685971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:58:36+00:00 2026-05-14T01:58:36+00:00

I’ve scoured StackOverflow and Google for an answer to this problem. I’m trying to

  • 0

I’ve scoured StackOverflow and Google for an answer to this problem.

I’m trying to create a Microsot SQL Server 2008 view. Not a stored procedure. Not a function. Just a query (i.e. a view).

I have three tables. The first table defines a common key, let’s say “CompanyID”. The other two tables have a sometimes-common field, let’s say “EmployeeName”.

I want a single table result that, when my WHERE clause says “WHERE CompanyID = 12” looks like this:

CompanyID | TableA    | TableB
12        | John Doe  | John Doe
12        | Betty Sue | NULL
12        | NULL      | Billy Bob

I’ve tried a FULL OUTER JOIN that looks like this:

SELECT Company.CompanyID,
    TableA.EmployeeName,
    TableB.EmployeeName
FROM Company
FULL OUTER JOIN TableA ON Company.CompanyID = TableA.CompanyID
FULL OUTER JOIN TableB ON 
    Company.CompanyID = TableB.CompanyID AND 
    (TableA.EmployeeName IS NULL OR TableB.EmployeeName IS NULL OR TableB.EmployeeName = TableA.EmployeeName)

I’m only getting the NULL from one matched table, I’m not getting the expansion for the other table. In the above sample, I’m basically only getting the first and third rows and not the second.

Can someone help me create this query and show me how this is done correctly?

BTW I already have a stored procedure that looks very clean and populates an in-memory table, but that isn’t what I want.

Thanks.

— EDIT:

Here’s a full-running sample of what currently doesn’t work (it’s missing ‘someone 2’ and ‘someone 3’.

DECLARE @Company TABLE
(
    CompanyID int
)

INSERT INTO @Company (CompanyID) VALUES (10)
INSERT INTO @Company (CompanyID) VALUES (12)

DECLARE @TableA TABLE
(
    EmployeeId int,
    CompanyId int,
    EmployeeName varchar(30)
)

DECLARE @TableB TABLE
(
    EmployeeId int,
    CompanyId int,
    EmployeeName varchar(30)
)

INSERT INTO @TableA ( EmployeeId, CompanyId, EmployeeName )
VALUES ( 1, 10, 'someone' )

--INSERT INTO @TableA ( EmployeeId, CompanyId, EmployeeName )
--VALUES ( 2, 12, 'someone 2' )

INSERT INTO @TableA ( EmployeeId, CompanyId, EmployeeName )
VALUES ( 3, 12, 'someone 3' )

INSERT INTO @TableA ( EmployeeId, CompanyId, EmployeeName )
VALUES ( 3, 12, 'someone 4' )

INSERT INTO @TableB ( EmployeeId, CompanyId, EmployeeName )
VALUES ( 1, 10, 'someone' )

INSERT INTO @TableB ( EmployeeId, CompanyId, EmployeeName )
VALUES ( 2, 12, 'someone 2' )

--INSERT INTO @TableB ( EmployeeId, CompanyId, EmployeeName )
--VALUES ( 3, 12, 'someone 3' )

INSERT INTO @TableB ( EmployeeId, CompanyId, EmployeeName )
VALUES ( 3, 12, 'someone 4' )

SELECT Company.CompanyID,
    TableA.EmployeeName,
    TableB.EmployeeName
FROM @Company Company
FULL OUTER JOIN @TableA TableA ON Company.CompanyID = TableA.CompanyID
FULL OUTER JOIN @TableB TableB ON Company.CompanyID = TableB.CompanyID
WHERE
(
    TableA.EmployeeName IS NULL OR TableB.EmployeeName IS NULL OR 
    TableB.EmployeeName = TableA.EmployeeName
)
AND Company.CompanyID = 12

Result:

CompanyID   EmployeeName    EmployeeName
12          someone 4       someone 4

What I want:

CompanyID   EmployeeName    EmployeeName
12          NULL            someone 2
12          someone 3       NULL
12          someone 4       someone 4
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:58:36+00:00Added an answer on May 14, 2026 at 1:58 am

    The FULL OUTER JOIN should be made only between TableA and TableB on companyID AND employeeName since this is the value you want filled as NULL if it exists only on one table.
    Once you get this, you can do an inner join with Company to get other data from Company.

    FULL OUTER JOIN Solution:

    select Company.companyID, EmployeeNameA, EmployeeNameB
    from (
        SELECT isnull(TableA.CompanyID, TableB.CompanyID) as companyID,
            TableA.EmployeeName as EmployeeNameA,
            TableB.EmployeeName as EmployeeNameB
        FROM @TableA TableA 
        FULL OUTER JOIN @TableB TableB ON TableA.EmployeeName = TableB.EmployeeName and TableA.companyID = TableB.companyID
        WHERE
         TableA.CompanyID = 12 or TableB.CompanyID = 12 
    ) merged
    inner join @Company Company
        on merged.companyID = Company.companyID
    

    Personally I find it difficult to think in terms of FULL OUTER JOINS. My approach on this would be: Find the distinct EmployeeNames you need in your result by making a UNION between affected tables and then use left joins to get data from both tables thus getting your NULLs when you should.

    LEFT JOIN Example:

    select c.companyID, a.employeeName, b.employeeName
    from  (
        select distinct employeeName, companyID
        from  (
            select a.employeeName, companyID 
            from @tableA  a
            union 
            select b.employeeName, companyID
            from @tableB b
        ) a
    ) z
    inner join @company c
        on c.companyID = z.companyID
    left join @tableA  a
        on z.companyID = a.companyID and z.employeeName = a.employeeName
    left join @tableB  b
        on z.companyID = b.companyID and z.employeeName = b.employeeName
    where z.companyID = 12
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.