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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:01:32+00:00 2026-05-16T21:01:32+00:00

i have two queries being combined with a UNION ALL 1 : –Query 1

  • 0

i have two queries being combined with a UNION ALL1:

--Query 1
SELECT Flavor, Color
FROM Friends

 

--Query 2
SELECT Flavor,
    (SELECT TOP 1 Color
     FROM Rainbows
     WHERE Rainbows.StrangerID = Strangers.StrangerID
     ORDER BY Wavelength DESC
    ) AS Color
FROM Strangers

Both of which, of course, work fine separately, but when combined with a UNION ALL:

SELECT Flavor, Color
FROM Friends

UNION ALL

SELECT Flavor,
    (SELECT TOP 1 Color
     FROM Rainbows
     WHERE Rainbows.StrangerID = Strangers.StrangerID
     ORDER BY Wavelength DESC
    ) AS Color
FROM Strangers

The query fails with the error:

Msg 104, Level 15, State 1, Line 3
ORDER BY items must appear in the select list if the statement contains a UNION operator.

How do i use an ORDER BY in a statement with a UNION ALL?

Copy-Pasteable Example

CREATE TABLE Friends (Flavor int, Color int)
CREATE TABLE Strangers (Flavor int, StrangerID int)
CREATE TABLE Rainbows (StrangerID int, Color int, Wavelength int)
go

SELECT Flavor, Color
FROM Friends

UNION ALL

SELECT Flavor,
    (SELECT TOP 1 Color
     FROM Rainbows
     WHERE Rainbows.StrangerID = Strangers.StrangerID
     ORDER BY Wavelength DESC
    ) AS Color
FROM Strangers
go

DROP TABLE Rainbows
DROP TABLE Strangers
DROP TABLE Friends

Server: Msg 104, Level 15, State 1, Line 2
ORDER BY items must appear in the select list if the statement contains a UNION operator.

Footnotes

  • 1Contrived hypothetical example. Or not.

See also

  • SQL Query – Using Order By in UNION
  • SQL UNION and ORDER BY
  • SQL: Using Top 1 in UNION query with Order By
  • Impact of ordering of correlated subqueries within a projection
  • 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-16T21:01:32+00:00Added an answer on May 16, 2026 at 9:01 pm

    A bit of a hack, but this will work.

    CREATE TABLE Friends (Flavor int, Color int)
    CREATE TABLE Strangers (Flavor int, StrangerID int)
    CREATE TABLE Rainbows (StrangerID int, Color int, Wavelength int)
    go
    
    SELECT Flavor, Color
    FROM Friends
    
    UNION ALL
    
    SELECT Flavor,
        (SELECT Color FROM 
            (SELECT TOP 1 Color, Wavelength
             FROM Rainbows
             WHERE Rainbows.StrangerID = Strangers.StrangerID
             ORDER BY Wavelength DESC
             ) AS Foo
        ) AS Color
    FROM Strangers
    go
    
    DROP TABLE Rainbows
    DROP TABLE Strangers
    DROP TABLE Friends
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two queries, as following: SELECT SQL_CALC_FOUND_ROWS Id, Name FROM my_table WHERE Name
I have two queries, the only difference being the GROUP BY clause SELECT *
I have two queries with subqueries for dateparts which I like to join. SELECT
I have two queries use [DatabaseAA] select * ,DATEDIFF(MINUTE,SomeDate,'3/28/2012 12:52:25 PM +00:00') This passes,
I have a small sql question for you. I have two queries: SELECT tbl_CostPrevisions.Month,
I have two select queries to use in my project for getting data and
I have a SQL query that I'm currently solving by doing two queries. I
I have two queries being returned by my sproc. I want to be able
I'm using parameterized queries with PHP I have the following two queries: SELECT username,question_text
Given: Two queries that require filtering: select top 2 t1.ID, t1.ReceivedDate from Table t1

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.