Unable to apply order by clause on distinct select in Oracle The query is:

select distinct( Name )
from Students
where marks&gt;75
order by Substr(Name,-3,3),ID asc;


This shows an error message:


order by Substr(Name,-3,3),ID asc 
* 
ERROR at line 4: 
ORA-01791: not a SELECTed expression
You can't order by columns that aren't selected when using DISTINCT, or their alias. 

To keep your order with a substring and another column, you will have to calculate them first (without DISTINCT), then just display the name while ordering by the other columns behind the curtain.

SELECT
    F.name
FROM
    (
        SELECT 
            S.name name,
            Substr(S.name, -3, 3) substrName,
            MAX(S.ID) ID
        FROM
            Students S
        WHERE
            S.marks &gt; 75
        GROUP BY
            S.name
    ) F
ORDER BY
    substrName,
    ID ASC