Monday, May 02, 2005

ORACLE: Using ROWNUM or SORT with UNION

[Pulled from dbforums]

If you want to use ROWNUM or SORT across the results of a UNION:

SELECT *
FROM (
    SELECT employee_id, last_name, first_name
    FROM   EMPLOYEES
    UNION
    SELECT department_id, 'DEPT', department_name
    FROM   DEPARTMENTS
)
WHERE ROWNUM < 100
ORDER BY last_name;


* I realize this is a brutaly contrived example, but hey, whaddaya expect?
** I have no idea how this performs.

No comments: