ORDER BY items must appear in the select list if SELECT DISTINCT is specified
ZPA rule for PL/SQL and Oracle SQL · NotASelectedExpression
In a SELECT DISTINCT, the rows are made distinct before they are sorted, so the ORDER BY can use only what each distinct row has: the selected expressions, their aliases, and expressions made of them. A column that is not selected has no single value per row, also when a selected expression uses it: with SELECT DISTINCT UPPER(r.label), ORDER BY r.label is rejected. Oracle rejects such a query with ORA-01791 ("not a SELECTed expression"). Older releases accepted some of these queries, which then fail after an upgrade.
This rule reports an ORDER BY expression of a SELECT DISTINCT that uses a column of the query that is not selected. A selected expression is recognized as Oracle compares it: unquoted names without regard to case, a column with or without the name of its table or alias, and parentheses, spacing and comments aside. An expression made of selected expressions, aliases, literals, functions, PL/SQL names and values such as SYSDATE is accepted (ORDER BY LOWER(alias)), and so is a position (ORDER BY 1). A query that selects * is not checked.
Examples
select distinct r.label
from rack r
order by r.capacity; -- Noncompliant
select distinct r.label, r.capacity
from rack r
order by r.capacity;