You can't usually have an order-by clause in a subquery, because it's meaningless, though it is generally allowed (but ignored) in an inline view. The order of the results is irrelevant to the outer query (with the exception of rownum handling). When used in a select list as you have it here it has to be a scalar subquery returning exactly one value, so ordering that single value would be pointless, if it were allowed.
The parser is expecting to see a )
instead of that order by
, so the error does make some sense, once you know what it wrong; but it doesn't really help you narrow it down if that's all you see.
It's perhaps not obvious that this restriction exists from the documentation, but it is mentioned in Oracle support document 731577.1:
Getting ORA-00907: missing right parenthesis
when using an ORDER BY
clause in a subquery. When the ORDER BY
clause is removed the query runs without error.
...
This is expected behavior per Bug 4944718
ORDER BY
in a subquery shouldn't work, since the order of the rows is passed to the outer query and has no impact.
From your question you already know that the order by
is causing the issue, so you can just remove that clause. It isn't obvious why you have a subquery there at all.