Skip to content Skip to sidebar Skip to footer

How Do I Combine Both My Top 5 Salaries And Bottom 5 To Display In Oracle?

So I know how to get the top 5 and bottom 5 to display by themselves. Problem is how do I combine both to display at the same time. This is what I have but it only shows the bottom

Solution 1:

Use UNION or UNION ALL:

SELECT SAL FROM
(
SELECTDISTINCT SAL FROM EMP WHERE SAL ISNOTNULLORDERBY SAL DESC
)
WHERE ROWNUM <6UNIONALLSELECT SAL FROM
(
SELECTDISTINCT SAL FROM EMP WHERE SAL ISNOTNULLORDERBY SAL ASC
)
WHERE ROWNUM <6;

You can also write this more succinctly as:

SELECT SAL
FROM (SELECTDISTINCT SAL,
             DENSE_RANK() OVER (ORDERBY sal) as seqnum_asc,
             DENSE_RANK() OVER (ORDERBY sal) as seqnum_desc
      FROM EMP
      WHERE SAL ISNOTNULL
     ) s
WHERE seqnum_asc <6OR seqnum_desc <6;

Post a Comment for "How Do I Combine Both My Top 5 Salaries And Bottom 5 To Display In Oracle?"