Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Mailing Lists -> Oracle-L -> RE: Top-N records.
This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.
------_=_NextPart_001_01C07CE7.CC140E90
Content-Type: text/plain;
charset="iso-8859-1"
> -----Original Message----- > From: Aldi Barco [mailto:ipal_at_hotmail.com] > Sent: vendredi, 12. janvier 2001 13:24 > > Could you please send me the trick to select top-20 > salary of scott.employee table ?
At the end of each e-mail coming from the list it says the following:
<< Please see the official ORACLE-L FAQ: http://www.orafaq.com >>
I found the answer there.
http://www.orafaq.com/faqsql.htm#TOP
How does one select the TOP N rows from a table? Form Oracle8i one can have an inner-query with an ORDER BY clause. Look at this example:
SELECT * FROM (SELECT * FROM my_table ORDER BY col_name_1 DESC) WHERE ROWNUM < 10; Use this workaround with prior releases: SELECT * FROM my_table a WHERE 10 >= (SELECT COUNT(DISTINCT maxcol) FROM my_table b WHERE b.maxcol >= a.maxcol) ORDER BY maxcol DESC;
Jacques R. Kilchoer
(949) 754-8816
Quest Software, Inc.
8001 Irvine Center Drive
Irvine, California 92618
U.S.A.
http://www.quest.com
------_=_NextPart_001_01C07CE7.CC140E90
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Diso-8859-1">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
5.5.2652.35">
<TITLE>RE: Top-N records.</TITLE>
</HEAD>
<BODY>
<P><FONT SIZE=3D2>> -----Original Message-----</FONT>
<BR><FONT SIZE=3D2>> From: Aldi Barco [<A =
HREF=3D"mailto:ipal_at_hotmail.com">mailto:ipal_at_hotmail.com</A>]</FONT>
<BR><FONT SIZE=3D2>> Sent: vendredi, 12. janvier 2001 13:24</FONT>
<BR><FONT SIZE=3D2>> </FONT>
<BR><FONT SIZE=3D2>> Could you please send me the trick to select =
top-20</FONT>
<BR><FONT SIZE=3D2>> salary of scott.employee table ?</FONT>
</P>
<P><FONT SIZE=3D2>At the end of each e-mail coming from the list it =
says the following:</FONT>
</P>
<P><FONT SIZE=3D2><< Please see the official ORACLE-L FAQ: <A =
HREF=3D"http://www.orafaq.com" =
TARGET=3D"_blank">http://www.orafaq.com</A> >></FONT>
</P>
<P><FONT SIZE=3D2>I found the answer there.</FONT>
</P>
<P><FONT SIZE=3D2><A HREF=3D"http://www.orafaq.com/faqsql.htm#TOP" =
TARGET=3D"_blank">http://www.orafaq.com/faqsql.htm#TOP</A></FONT>
</P>
<P><FONT SIZE=3D2>How does one select the TOP N rows from a =
table?</FONT>
<BR><FONT SIZE=3D2>Form Oracle8i one can have an inner-query with an =
ORDER BY clause. Look at this example: </FONT>
<BR><FONT SIZE=3D2> SELECT =
*</FONT>
<BR><FONT SIZE=3D2> =
FROM (SELECT * FROM my_table ORDER BY col_name_1 =
DESC)</FONT>
<BR><FONT SIZE=3D2> =
WHERE ROWNUM < 10;</FONT>
</P>
<P><FONT SIZE=3D2>Use this workaround with prior releases: </FONT>
<BR><FONT SIZE=3D2> SELECT =
*</FONT>
<BR><FONT =
SIZE=3D2> FROM =
my_table a</FONT>
<BR><FONT SIZE=3D2> =
WHERE 10 >=3D (SELECT COUNT(DISTINCT maxcol)</FONT>
<BR><FONT =
SIZE=3D2> &nb=
sp; &nb=
sp; FROM my_table b</FONT>
<BR><FONT =
SIZE=3D2> &nb= sp; = WHERE b.maxcol >=3D a.maxcol)</FONT>
<P><FONT SIZE=3D2>------</FONT>
<BR><FONT SIZE=3D2>any ignorant comments made are the sole =
responsibility of J. R. Kilchoer and should not reflect adversely upon =
my employer.</FONT></P>
<P><FONT SIZE=3D2> </FONT>
<BR><FONT SIZE=3D2>Jacques R. Kilchoer</FONT>
<BR><FONT SIZE=3D2>(949) 754-8816</FONT>
<BR><FONT SIZE=3D2>Quest Software, Inc.</FONT>
<BR><FONT SIZE=3D2>8001 Irvine Center Drive</FONT>
<BR><FONT SIZE=3D2>Irvine, California 92618</FONT>
<BR><FONT SIZE=3D2>U.S.A.</FONT>
<BR><FONT SIZE=3D2><A HREF=3D"http://www.quest.com" =
Received on Fri Jan 12 2001 - 16:34:16 CST
![]() |
![]() |