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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:52:34+00:00 2026-06-02T11:52:34+00:00

I have a very simple requirement. SELECT NULL as ProjectId, ‘All’ as ProjectName UNION

  • 0

I have a very simple requirement.

SELECT NULL as ProjectId, 'All' as ProjectName

UNION

(
  SELECT  Project.ProjectId,  Project.ProjectName
  FROM Project Order by 2
)

Original order of entries:

ProjectId       ProjectName     
 24               Beta           
 56               Alpha          
 57               Gamma          
 120              Aap            

EXPECTED Result SET:

ProjectId      ProjectName         
______________________________
NULL           All                 
120            Aap                 
56             Alpha               
24             Beta                
57             Gamma              

What I Need: I want to add a single row on top of the ordered result set of a query

Problems:

  1. Subquerys are not allowed to have Order By clause
  2. Doing Top 100 Percent destroys the order, and row having ‘All’ doesn’t come at top
  3. Declaring a Table variable inserting all entries in order and then performing union on this table
    i.e.

     Select NULL as ProjectId, 'All' as ProjectName...
      UNION
     select * from @myTable
    

    again destroys the order

help me please

  • 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-02T11:52:35+00:00Added an answer on June 2, 2026 at 11:52 am

    You can order result of UNION, but you need to provide additional column to identify where the data came from – here it is SortOrder:

    SELECT NULL as ProjectId, 'All' as ProjectName, 1 SortOrder
    UNION ALL
    SELECT  Project.ProjectId,  Project.ProjectName, 2 SortOrder
    FROM Project
    order by SortOrder, ProjectName
    

    Note I replaced UNION with UNION ALL as you do not need to DISTINCT result set.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP ListView, and have a very simple requirement to display numbers
I have a very simple webforms app that will allow field techs to order
I have a very simple Setup project that copies three dlls into the GAC.
I have a very simple iPhone app that requires a random integer from 1-100.
I have a very simple requirement. I have two div. Div1 with a Link
I have very simple window where I have 2 buttons - one for cancel,
I have some very simple XML: <properties> <property> <name>BobFish</name> <explaination>Bob is a fish.</explaination> </property>
I have a very simple table with two columns, but has 4.5M rows. CREATE
I have a very simple Mac Cocoa app that just has a Web View
I have a very simple python script that should scan a text file, which

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.