Select top rows from a sqlplus query

user2610333 picture user2610333 · Apr 26, 2014 · Viewed 8.5k times · Source

I have a query

select sum(pur.purchase_net_invoice_value), par.party_desc  from purchase pur
join party par
on par.party_id = pur.party_id
group by par.party_desc;

which runs fine.

I just want to have top five rows of the query and discard the others.

Answer

Peter Lang picture Peter Lang · Apr 26, 2014

You can use the ROWNUM Pseudocolumn to limit the number of rows.
Add an ORDER BY clause in the sub-query to define which rows to show.

SELECT *
FROM (
    select sum(pur.purchase_net_invoice_value), par.party_desc
    from purchase pur
    join party par on par.party_id = pur.party_id
    group by par.party_desc
)
WHERE ROWNUM <= 5;