sort query result without selecting that column but sort by that column?

Another syntax that may be easier, depending on how you think about it is using the with keyword. This explicitly creates a named temporary table with the desired ordering, then queries from that. The order of the new query will be the same as the temporary tables ordering.

WITH temp_table AS (SELECT   *
            FROM emp
        ORDER BY empno)

SELECT empname, salary, status
  FROM temp_table
 WHERE salary > 5000;

The answer by @jaychapani is more concise and functionally does the same thing, but the with syntax is powerful for quite a few other use cases and visually separates the two, which can be helpful especially if you have a long subquery that does other things.


Your syntax seems correct to me except dot(.) at the end. After removing dot if doesn't work...

Try something like

SELECT empname, salary, status
  FROM (SELECT   *
            FROM emp
        ORDER BY empno)
 WHERE salary > 5000

I used below query to solve this problem. In this case we can sort query result without displaying the column:

WITH temp_table
     AS (select distinct(s1.Name),s1.id
         from students s1
         where marks>75
         order by right(s1.Name ,3) asc,s1.id asc
        )

SELECT Name
  FROM temp_table;