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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:38:56+00:00 2026-05-13T16:38:56+00:00

In Microsoft SQL Server 2005 or above, I would like to get the first

  • 0

In Microsoft SQL Server 2005 or above, I would like to get the first row, and if there is no matching row, then return a row with default values.

SELECT TOP 1 ID,Name
FROM TableName
UNION ALL 
SELECT 0,''
ORDER BY ID DESC

This works, except that it returns two rows if there is data in the table, and 1 row if not.
I’d like it to always return 1 row.
I think it has something to do with EXISTS, but I’m not sure.
It would be something like:

SELECT TOP 1 * FROM Contact 
WHERE EXISTS(select * from contact)

But if not EXISTS, then SELECT 0,”

  • 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-13T16:38:56+00:00Added an answer on May 13, 2026 at 4:38 pm

    What happens when the table is very full and you might want to specify which row of your top 1 to get, such as the first name? OMG Ponies’ query will return the wrong answer in that case if you just change the ORDER BY clause. His query also costs about 8% more CPU than this modification (though it has equal reads)

    SELECT TOP 1 *
    FROM (
       SELECT TOP 1 ID,Name
       FROM TableName
       ORDER BY Name
       UNION ALL
       SELECT 0,''
    ) X
    ORDER BY ID DESC
    

    The difference is that the inner query has a TOP 1 also, and which TOP 1 can be specified there (as shown).

    Just for fun, this is another way to do it which performs very closely to the above query (-15ms to +30ms). While it’s more complicated than necessary for such a simple query, it demonstrates a technique that I don’t see other SQL folks using very often.

    SELECT
       ID = Coalesce(T.ID, 0),
       Name = Coalesce(T.Name, '')
    FROM
       (SELECT 1) X (Num)
       LEFT JOIN (
          SELECT TOP 1 ID, Name
          FROM TableName
          ORDER BY ID DESC
       ) T ON 1 = 1 -- effective cross join but does not limit rows in the first table
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You'll get an EntryPointNotFoundException when you call the method. If… May 13, 2026 at 8:55 pm
  • Editorial Team
    Editorial Team added an answer You can, but it requires you to implement a CHECK… May 13, 2026 at 8:55 pm
  • Editorial Team
    Editorial Team added an answer When it comes to "applied" data mining, for the starters,… May 13, 2026 at 8:55 pm

Related Questions

I have an Orders table that stores the Ordernumber as NVarChar. We manually increment
This question originates from a discussion on whether to use SQL ranking functionality or
I had a heated discussion with a colleague on the usage of stored procedures
In Microsoft SQL Server 2005, there is the option to store within the database
My question is similar to Upgrading SQL Server 2000 to 2005 or 2008 -

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.