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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:35:21+00:00 2026-05-27T07:35:21+00:00

I have a table TestTable : IntCol 1 4 6 2 3 8 12

  • 0

I have a table TestTable:

IntCol
1
4
6
2
3
8
12
11
9
7
15
13
14

(numbers are from 1 to 15 excluding 10).

When I do…

SELECT * FROM 
    (SELECT IntCol
    FROM TestTable
    WHERE IntCol >= 10
    ORDER BY IntCol ASC) x1
UNION ALL
SELECT * FROM 
    (SELECT IntCol
    FROM TestTable
    WHERE IntCol < 10
    ORDER BY IntCol DESC) x2

… result is ok (first from 11 to 15 ASCending, then from 1 to 9 DECSending):

IntCol
11
12
13
14
15
9
8
7
6
4
3
2
1

But when I omit SELECT * FROM and do…

    (SELECT IntCol
    FROM TestTable
    WHERE IntCol >= 10
    ORDER BY IntCol ASC)
UNION ALL
    (SELECT IntCol
    FROM TestTable
    WHERE IntCol < 10
    ORDER BY IntCol DESC)

… rows aren’t ordered at all (only values greater then 10 are first). The result is:

IntCol
12
11
15
13
14
1
4
6
2
3
8
9
7

My question is: why there is this SELECT * FROM needed when UNIONing ordered rows?

  • 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-27T07:35:22+00:00Added an answer on May 27, 2026 at 7:35 am

    Individual Queries, cannot have Order By clause when there is a union in between them.

    for reference

    http://p2p.wrox.com/sql-language/17907-union-order.html

    here is an example of how Order By is used with Union

    http://www.techonthenet.com/sql/union.php

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

Sidebar

Related Questions

I have table rows of data in html being filled from a CGI application.
I have table with 3 columns A B C. I want to select *
I have a table like below Consider the table id=testtable, and there is no
I have a procedure which selects data from a table called Test. And the
I have a table create table testtable( testtable_rid serial not null, data integer not
Possible Duplicate: Default sort-ordering in MySQL (ALTER TABLE … ORDER BY …;) I have
I have a table structured like this: CREATE TABLE [TESTTABLE] ( [ID] [int] IDENTITY(1,1)
Let's assume I have the following table class: class TestTable(tables.Table): id = tables.Column() description
I have created a test table in MySQL and would like to insert 10
I have 2 databases with same tables. DBProduction and DBLocal both have testTable. How

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.