Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

In which clauses of a query subqueries may appear?

user6027184Sep 24 2021

Hi.
We know that subqueries can be placed in the WHERE, HAVING, FROM and SELECT clauses of a query (under certain conditions, especially in SELECT). One can also use subqueries in the START WITH clause of a hierarchical query. However, I have never come across a statement in any book or documentation that a subquery may appear in the ORDER BY clause.
But a query
select *
from emp e
order by (select empno from emp where e.mgr = empno);
(on a standard emp table) works. Of course, this is a specific case, because it is a correlated query and returns exactly one row - the boss of a given employee. But why does it even work? Is it mentioned somewhere in the documentation? Are there any other surprising places where subqueries might appear?
Thanks for help.

This post has been answered by Frank Kulash on Sep 24 2021
Jump to Answer
Comments
Post Details
Added on Sep 24 2021
3 comments
800 views