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

The Archive Base Latest Questions

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

I have a query: select * from (select top 100 percent … from [table]

  • 0

I have a query:

select * from (select top 100 percent ... from [table] 
    where [field1] is not null
    order by [field1]) a 
union all 
select * from (select top 100 percent ... from [table]
    where [field1] is null 
    order by [field2]) b

The subqueries are ordering their results correctly. I want the exact results of the first subquery, THEN the exact results of the second subquery. However, I’m actually getting the concatenated results ordered in some odd way that I can’t even discern a pattern from. I have [field2] values scattered about the result set apparently at random.

How can I get the first query results on top, and the second query results on bottom?

Edit: To clarify, I want the top query ordered by its own conditions, and then I want the bottom query ordered by its own conditions.

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

    I am not sure, whether or not UNION ALL is meant to preserve the order of rows.
    You can force the order you want by using the following statement:

    select [field1] from
    (
        select * from
        (select 1 as query_number, [field1], [field1] as [order_field] from [table] 
            where [field1] is not null) a 
        union all 
        select * from
        (select 2 as query_number, [field1], [field2] as [order_field] from [table]
            where [field1] is null) b
    )
    order by query_number, [order_field];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query: select column_name, count(column_name) from table group by column_name having
SELECT TOP (100) PERCENT dbo.EmployeeInfo.id, MIN(dbo.EmployeeInfo.EmpNo) AS EmpNo, SUM(dbo.LeaveApplications.DaysAuthorised) AS DaysTaken FROM dbo.EmployeeInfo LEFT
I have this SQL query: SELECT * FROM IMAGES WHERE IMAGENAME in ('IMG1', 'IMG2',
I have the following query: SELECT c.* FROM companies AS c JOIN users AS
If I have a query like: Select EmployeeId From Employee Where EmployeeTypeId IN (1,2,3)
I seem to have a problem with this SQL query: SELECT * FROM appts
I have a simple query like this: select * from mytable where id >
I have a query on my database as such: SELECT * FROM expenses WHERE
I currently have the query running on Postgres: SELECT * FROM addenda.users WHERE users.username
say I have this $result = mysql_query('SELECT views FROM post ORDER BY views ASC');

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.