Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a table (emp) with two columns in ORACLE:

9999      00001     emp1
9999      00001     emp2
9999      00002     emp3
9999      00002     emp4
0001      99999     emp5
0001      00001     emp6
0001      00002     emp7
0002      00001     emp8
0002      99999     emp9
0003      99999     emp10
0003      00001     emp11
0003      00002     emp12

I want to get value in EMP column with following statement:

ORDER BY COMPANY /*(But 9999 is first)*/, OFFICE /*(But 99999 is first).*/

I don't know how to do.

share|improve this question
I don't understand the output you want from your query. Can you write down the output you're expecting and then we can help you with your query. – Trygve Laugstøl Nov 23 '12 at 23:51
Thanks dezso, my problem was solved, by Bary's way, thank you very much. – TOMMY Dec 8 '12 at 16:23

I guess you want the results in ascending order but with 9999 always first?

Select EMP
From Emp
Order By Case When Company = 9999 Then -1 else Company End,
         Case When Office = 9999 Then -1 else Office End;
share|improve this answer
It helps me. Thank Barry very much! – TOMMY Nov 23 '12 at 16:36

If I understand you correctly, you want to sort based on company in descending order, and then by office also in descending order

SELECT emp FROM emp ORDER BY company DESC, office DESC;
share|improve this answer
Thanks Roy for your answer. – TOMMY Nov 23 '12 at 16:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.