Search code examples
sqloraclesql-order-byrownum

how to pick out after using order by?


I run:

select aname, APERCENT from AGENTS order by APERCENT DESC;

and I get this result:

ANAME APERCENT

john    7

jane    6

teddy   6

bob     6

allen   5

airon   5

Now I want to pick up first column so I add rownum=1.

select aname, APERCENT from AGENTS order by APERCENT DESC where ROWNUM=1;

But SQL command not properly ended, and I want to fix it. What should I do?


Solution

  • You should add rownum=1 AFTER ordering. So it will be

    select *
    from
    (
    select aname, APERCENT 
    from AGENTS 
    order by APERCENT DESC 
    )
    where ROWNUM=1;