Skip to content
pvmehta.com

pvmehta.com

  • Home
  • About Me
  • Toggle search form
  • Find execution plan from dba_hist_sql_plan for a specific SQL_ID and PLAN_HASH_VALUE fplan.sql Oracle
  • Committing distributed transaction using commit force Oracle
  • Adding a new disk and mount it automatically. on VMWARE LINUX Linux/Unix
  • moving lob object to other tablespace lob_mvmt.sql Oracle
  • lck.sql Oracle
  • Oracle vs MYSQL Architecture differences (For DBAs) MYSQL
  • OEM-Commnds Oracle
  • online_bkup.sql Oracle
  • shutdown linux Linux/Unix
  • export import with parameter file. Oracle
  • Some useful Unix Commands Linux/Unix
  • UTL_FILE test program Oracle
  • checking connectivity between two servers Linux/Unix
  • Find total file sizes Linux/Unix
  • find_idle_cpu.sql Oracle

TOP-N Sql to find Nth max or Top N rows

Posted on 01-May-2007 By Admin No Comments on TOP-N Sql to find Nth max or Top N rows

PURPOSE

This note explains the TOP-N Query, a new feature (as of 8.1.5) that allows

users to query on ROWNUM and order results with ORDER BY. This functionality

is similar to the offerings of other database software such as the “Top

Values’ property option in Microsoft Access.

Top-N Query to Solve ORDER BY…ROWNUM Inaccuracy:

==================================================

What is a Top-N Query?

———————-

Top-N queries use a consistent nested query structure to show the largest or

smallest values of a column. For example, what are the 10 best selling products,

what are the 10 worst selling products?

Top-N queries are useful in scenarios where we need to find the top-n or

bottom-n records from a table based on a condition.

The syntax for a Top-N query is:

SELECT [column_list], ROWNUM

FROM (SELECT [column_list] FROM table

ORDER BY Top-N_column)

WHERE ROWNUM <= N; A subquery or an inline view generates the list of sorted data. The subquery
includes the ORDER BY clause to ensure that the ranking is in the desired

order. The outer query is used to limit the number of rows in the final result

set. The ROWNUM pseudo-column assigns a sequential value starting with 1 to

each of the rows returned from the subquery. A WHERE clause with ROWNUM

specifies the n rows to be returned.

‘Select ROWNUM…ORDER BY’ Returns Correct Results with TOP-N Query:

——————————————————————–

You want to issue a query to return rows based on a specific value. For

example, you want to see the rows for the 10 highest paid employees. You issue

a query similar to:

SQL> select empno, ename, sal

from emp

where rownum <=10
order by sal desc;

However, you know that the results this query returns are not correct.

In 8.1.5.x and greater you can restructure the query as follows:

SQL> select empno, ename, sal

from

(select empno, ename, sal

from emp

order by sal desc)

where rownum <=10; This is a TOP-N Query. Structuring the query in this manner allows Oracle to
use SQL operations in the proper order to return the proper results. The nested

query first selects ALL rows from the table and sorts them in descending order

of the sal column.

Oracle, SQL scripts

Post navigation

Previous Post: Session_info.ksh
Next Post: DBMS_Shared_pool pinning triggers

Related Posts

  • My Test Case On 21-OCT-2005 Oracle
  • CTAS with LONG Column for 7.x and 8 and 8i Oracle
  • Gather Stats manually using DBMS_STATS after disabling DBMS_SCHEDULER jobs as previous entry Oracle
  • EXTPROC Oracle
  • Optimizer SORT Operations Oracle
  • Multiple listeners Oracle

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Ansible (0)
  • AWS (2)
  • Azure (1)
  • Linux/Unix (149)
  • MYSQL (5)
  • Oracle (392)
  • PHP/MYSQL/Wordpress (10)
  • POSTGRESQL (0)
  • Power-BI (0)
  • Python/PySpark (7)
  • RAC (17)
  • rman-dataguard (26)
  • shell (149)
  • SQL scripts (341)
  • SQL Server (6)
  • Uncategorized (0)
  • Videos (0)

Recent Posts

  • SQL Server Vs Oracle Architecture difference25-Jul-2025
  • SQL Server: How to see historical transactions25-Jul-2025
  • SQL Server: How to see current transactions or requests25-Jul-2025
  • T-SQL Vs PL/SQL Syntax25-Jul-2025
  • Check SQL Server edition25-Jul-2025
  • Checking SQL Server Version25-Jul-2025
  • Oracle vs MYSQL Architecture differences (For DBAs)24-Jul-2025
  • V$INSTANCE of Oracle in MYSQL24-Jul-2025
  • Day to day MYSQL DBA operations (Compared with Oracle DBA)24-Jul-2025
  • MYSQL and Oracle Comparison for Oracle DBA24-Jul-2025

Archives

  • 2025
  • 2024
  • 2023
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006
  • 2005
  • Ports used by Oracle Software Oracle
  • Temporary Tablespace Information and restriction. Oracle
  • moving lob object to other tablespace lob_mvmt.sql Oracle
  • SQLPLUS COPY command Precautions. Oracle
  • handling filenname with space Linux/Unix
  • Btee and Bitmap Plans in Oracle 9i and higher Oracle
  • Useful Solaris Commands on 28-SEP-2005 Linux/Unix
  • Processes Parameter decision Oracle

Copyright © 2025 pvmehta.com.

Powered by PressBook News WordPress theme