Using ranking to select the top 10 tuples in Oracle SQL

I have a relationship instructor (ID, name, department name, salary).

The question in our assignment asks us: to use the rank function in SQL to write a query on the identifier and name of those instructors from the top 10 highest paid.

I can rank instructors with select id, name, rank() over(order by(salary) desc) as sal from instructor order by sal

What is my next step in grabbing the top 10 instructors? Thank.

+3
source share
2 answers

Your solution is close to the answer, but it is usually better to use the value set by the RANK () function:

select id
       , name
       , sal
from (
  select id
        , name
        , sal
        , rank() over(order by(salary) desc) as sal_rank
    from instructor
  )
where sal_rank <=10
/

rownum , . , , . ROWNUM .

ROWNUM - ROW_NUMBER() - . -.

+2

, rownum...

select id, name from(
  select id, name, rank() over(order by(salary) desc) as sal
    from instructor order by sal
  )
where rownum <=10

, - !

-1

Source: https://habr.com/ru/post/1734440/


All Articles