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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:20:32+00:00 2026-06-15T18:20:32+00:00

I am selecting an int value from two different tables as shown below select

  • 0

I am selecting an int value from two different tables as shown below

select col1 from tablea
union 
select col1 from tableb

The requirement is if result is found in first query, use that; otherwise, look in second table.

select top 1 * from (select col1 from tablea union select col1 from tableb) as a

Problem is the highest numerical value of the result set is returned as top 1 – not the first result found.

I don’t care about the numerical value as in it’s order – I just want to apply precedence to if find a value from select 1 don’t bother to run second query.

Without the top 1 * I get returned 3 and 6. When I do top 1 * I get 6 and same result if I do the other select first.

Help!

  • 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-06-15T18:20:34+00:00Added an answer on June 15, 2026 at 6:20 pm

    When you UNION these two selects the order IS NOT DEFINED you can think of it as a RANDOM order. So you should define order to get right results. For example:

    select top 1 C1 from 
    (select col1 as C1, 1 as c2 from tablea 
         union 
     select col1 as C1, 2 as c2 from tableb) as a 
    ORDER BY C2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When i am selecting a full table i.e. select * from product , Hibernate
I have created a stored procedure for selecting value from the table if that
when selecting a column that can contains a NULL value I get an exception
For selecting birthdays between two months where FROMDATE and TODATE are some parameters in
I'm selecting products from a database table. Currently the query is launched from Coldfusion,
I written the following code to randomly select items n items from a list
I just creates this stored procedure in selecting palletnumber=datatype is int. SerialNumber is varchar.
I'm selecting a variety of columns from my database, and my query looks like
Question: What is the correct syntax to use when selecting a column value into
I have taken to selecting random values from enums like so: import std.random :

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.