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 697113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:08:05+00:00 2026-05-14T03:08:05+00:00

Here’s the scenario: I have 2 tables: CREATE TABLE dbo.API_User ( id int NOT

  • 0

Here’s the scenario:

I have 2 tables:

CREATE TABLE dbo.API_User
    (
    id int NOT NULL,
    name nvarchar(255) NOT NULL,
    authorization_key varchar(255) NOT NULL,
    is_active bit NOT NULL
    )  ON [PRIMARY]

CREATE TABLE dbo.Single_Sign_On_User
    (
    id int NOT NULL IDENTITY (1, 1),
    API_User_id int NOT NULL,
    external_id varchar(255) NOT NULL,
    user_id int NULL
    )  ON [PRIMARY]

What I am trying to return is the following:

  1. is_active for a given authorization_key
  2. The Single_Sign_On_User.id that matches the external_id/API_User_id pair if it exists or NULL if there is no such pair

When I try this query:

SELECT Single_Sign_On_User.id, API_User.is_active
FROM API_User LEFT OUTER JOIN
    Single_Sign_On_User ON Single_Sign_On_User.API_User_id = API_User.id
WHERE     
    Single_Sign_On_User.external_id = 'test_ext_id' AND 
    API_User.authorization_key = 'test'

where the “test” API_User record exists but the “test_ext_id” record does not, and with no other values in either table, I get no records returned.

When I use:

SELECT Single_Sign_On_User.id, API_User.is_active
FROM API_User LEFT OUTER JOIN
    Single_Sign_On_User ON Single_Sign_On_User.API_User_id = API_User.id
WHERE     
    API_User.authorization_key = 'test'

I get the results I expect (NULL, 1), but that query doesn’t allow me to find the “test_ext_id” record if it exists but would give me all records associated with the “test” API_User record.

How can I get the results I am after?

  • 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-14T03:08:05+00:00Added an answer on May 14, 2026 at 3:08 am

    Filtering on the “outer” table” changes the join to INNER.

    4 options, of which the first 2 are best

    • derived table

    Filter inside the derived table

    SELECT SSOU.id, API_User.is_active
    FROM
        API_User
        LEFT OUTER JOIN
        (
        SELECT id FROM Single_Sign_On_User WHERE external_id = 'test_ext_id'
        ) SSOU  ON SSOU.API_User_id = API_User.id
    WHERE     
        API_User.authorization_key = 'test'
    
    • CTE (in place of a derived table) for SQL Server 2005+

    • filter in JOIN

    thus;

    SELECT Single_Sign_On_User.id, API_User.is_active
    FROM API_User LEFT OUTER JOIN
        Single_Sign_On_User ON Single_Sign_On_User.API_User_id = API_User.id
    
        AND Single_Sign_On_User.external_id = 'test_ext_id'
    
    WHERE     
        API_User.authorization_key = 'test'
    
    • OR clause

    Thus:

    SELECT Single_Sign_On_User.id, API_User.is_active
    FROM API_User LEFT OUTER JOIN
        Single_Sign_On_User ON Single_Sign_On_User.API_User_id = API_User.id
    WHERE     
        (Single_Sign_On_User.external_id = 'test_ext_id' OR Single_Sign_On_User.external_id IS NULL)
        AND
        API_User.authorization_key = 'test'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.