Order Union in SQL

Hello,
I am using a query with the UNION statement, something like this

SELECT 'Hello' as Suma
UNION
SELECT 'Saturday' as Suma
UNION
SELECT 'Friday' as Suma
UNION
SELECT 'Wednesday' as Suma


when I run I get :
Friday
Hello
Saturday
Wednesday

but I need to get

Hello
Saturday
Friday
Wednesday

some idea?
LVL 1
tenriquez199Asked:
Who is Participating?
 
Ephraim WangoyaCommented:
SELECT 'Hello' as Suma
UNION  ALL
SELECT 'Saturday' as Suma
UNION ALL
SELECT 'Friday' as Suma
UNION ALL
SELECT 'Wednesday' as Suma
0
 
dqmqCommented:
select suma from
(
SELECT 'Hello' as Suma, 1 as seq
UNION
SELECT 'Saturday', 2
UNION
SELECT 'Friday', 3
UNION
SELECT 'Wednesday', 4
) as x
order by seq asc

0
 
käµfm³d 👽Commented:
How about:
SELECT 'Hello', 1 as Suma
UNION 
SELECT 'Saturday', 2 as Suma
UNION 
SELECT 'Friday', 3 as Suma
UNION 
SELECT 'Wednesday', 4 as Suma
ORDER BY 2

Open in new window

0
 
käµfm³d 👽Commented:
Sorry dqmq...  I guess I'm too slow  = )
0
 
dqmqCommented:
Me too!  Except the accepted answer is only right by coincidence.   The only way to guarantee the ordering of rows returned by a select is with the ORDER BY CLAUSE.  I'm not arguing the the UNION ALL solution did not give the correct answer this time, just that it may not give the correct answer all the time.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.