SQL tutorial | Interview questions | Oracle
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Can Group Functions Be Used in the ORDER BY Clause?

Go down

Can Group Functions Be Used in the ORDER BY Clause? Empty Can Group Functions Be Used in the ORDER BY Clause?

Post by Micheal Sun Jan 17, 2010 3:08 am

If the query output is aggregated as groups, you can sort the groups by using group functions in
the ORDER BY clause. The following statement returns how many employees are having the
same salary in each department. The group output is sorted by the count in each group in
descending order:
SQL> SELECT department_id, salary, count(*)
FROM employees GROUP BY department_id, salary HAVING count(*) > 1
ORDER BY COUNT(*) DESC;
DEPARTMENT_ID SALARY COUNT(*)
------------- ---------- ----------
50 2500 5
50 3200 4
50 2800 3
80 10000 3
80 9500 3
50 3100 3
50 2600 3

Micheal
Admin

Posts : 243
Join date : 2010-01-10

http://sql-tutorial.co.cc

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum