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

  • Home
  • SEARCH
  • 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 218621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:46:07+00:00 2026-05-11T18:46:07+00:00

I need to write a a query that gets a set of information from

  • 0

I need to write a a query that gets a set of information from a table, but if there is no information for that specific client, then use the default set. I was wondering if there is a way to avoid query duplication in an if(exists()) statement.

For example:

IF( EXISTS( SELECT * FROM UserTable WHERE Name = @UserName))
BEGIN
    SELECT * FROM UserTable WHERE Name = @UserName))
END
ELSE
BEGIN
    SELECT * FROM UserTable WHERE Name = 'Jon Skeet'))
END

The first two selects (exists and the true part of the if) are the exact same. I want to avoid running the same query twice if the statement is true. I know the exists stops once the first true condition is met but that is still O(n) worst case.

Another option I know of, is put the information in a temp table and check if information is there, if not return the default information.

Both ways would work but what is the best way to do it? Are there other ways to do this? Is there some way to do this in the WHERE clause since that’s the only thing that is different?

Edit: Fixed example to return a row not just a single item. So the answers of dumping the select to a single variable would be equal to a temp table I assume. Also, to not anger Jon Skeet, spelled his name right

  • 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-11T18:46:07+00:00Added an answer on May 11, 2026 at 6:46 pm

    You could do this:

    SELECT TOP 1
         UserID
    FROM
         UserTable
    WHERE
         Name IN (@UserName, 'John Skeet')
    ORDER BY
         CASE WHEN Name = 'John Skeet' THEN 2 ELSE 1 END
    

    (or use LIMIT or whatever the method is for your RDBMS)

    Or you could do this:

    DECLARE @UserID INT
    
    SELECT
         @UserID = UserID
    FROM
         UserTable
    WHERE
         Name = @UserName
    
    IF (@UserID IS NULL)
         SELECT
              @UserID = UserID
         FROM
              UserTable
         WHERE
              Name = 'John Skeet'
    
    SELECT @UserID AS UserID
    

    Or this:

    SELECT
         COALESCE(T2.UserID, T1.UserID)
    FROM
         UserTable T1
    LEFT OUTER JOIN UserTable T2 ON
         T2.Name = @UserName
    WHERE
         T1.Name = 'John Skeet'
    

    Or this:

    SELECT
         UserID
    FROM
         UserTable
    WHERE
         Name = @UserName
    
    IF (@@ROWCOUNT = 0)  -- MS SQL Server specific, your RDBMS method will vary
         SELECT
              UserID
         FROM
              UserTable
         WHERE
              Name = 'John Skeet'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 274k
  • Answers 274k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use Google Earth for that. May 13, 2026 at 2:18 pm
  • Editorial Team
    Editorial Team added an answer Linq to SQL is no longer being actively developed -… May 13, 2026 at 2:18 pm
  • Editorial Team
    Editorial Team added an answer You have three options: Explicitly implement the interface: You can… May 13, 2026 at 2:18 pm

Related Questions

I have a SQL Table UDF That gets a Standard Deviation from a 20
I have a table with multiple date columns per employee: Emp 1, Date1, Date2,
This is a very very important sql query after which my whole website is
alt text http://img3.imageshack.us/img3/1488/advancedsearch.png I'm building an Advanced Search interface in an ASP.NET application. I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.