You could use the COUNT() OVER() analytic function.
For example, using the standard employees
table in HR
schema:
SQL> SELECT DISTINCT department_id,
2 COUNT(department_id) OVER(PARTITION BY department_id) rn
3 FROM hr.employees t
4 ORDER BY rn DESC;
DEPARTMENT_ID RN
------------- ----------
50 45
80 34
30 6
100 6
60 5
90 3
20 2
110 2
10 1
40 1
70 1
0
12 rows selected.
SQL>
To find the department_id
with maximum employees from above result set:
SQL> SELECT department_id
2 FROM
3 (SELECT department_id,
4 row_number() OVER(ORDER BY rn DESC) rn
5 FROM
6 ( SELECT DISTINCT department_id,
7 COUNT(department_id) OVER(PARTITION BY department_id) rn
8 FROM hr.employees t
9 )
10 )
11 WHERE rn = 1;
DEPARTMENT_ID
-------------
50
SQL>