Skip to content
pvmehta.com

pvmehta.com

  • Home
  • About Me
  • Toggle search form
  • Postgres DB user and OS user. POSTGRESQL
  • On solaris 10, “S” link is not part of $ORACLE_HOME/bin/oracle as default. ( For 9.2.0.8) Oracle
  • backspace in SQL Plus not working then..? Linux/Unix
  • move_arch_files.ksh Linux/Unix
  • Windows based Command line mailing program like mailx (Sednmail for windows) PHP/MYSQL/Wordpress
  • Changing default shell Linux/Unix
  • Rman Notes -1 Oracle
  • Find Time Consuming SQL Statements in Oracle 10g Oracle
  • Oracle 11g Environment Setup Oracle
  • Follwoing korn shell is used to move files from source folder to destination folder and gzip it in destination folder. mv_iotto.ksh Linux/Unix
  • tuning commmand for cpu, ip and memory stats Linux/Unix
  • move_arch_files.ksh Linux/Unix
  • Guide to Linux System Command Mastery Linux/Unix
  • oracle_env_10g_CADEV Linux/Unix
  • Renaming Oracle Instance Name Oracle

Display the top 5 salaries for each department using single SQL

Posted on 03-May-2010 By Admin No Comments on Display the top 5 salaries for each department using single SQL

Ok, given the clarification I can show you a way to get this in 815 and before (slow on a

big table) and in 816 and up (fast with analytic functions!)

ops$tkyte@ORA817.US.ORACLE.COM> select * from t;

DEPTNO SAL

———- ———-

10 100

10 100

10 200

10 300

10 400

10 500

20 100

20 200

20 300

20 400

20 500

20 600

20 700

20 700

30 100

30 200

30 300

30 400

18 rows selected.

Thats my sample data. Now to get it in 815 and before:

ops$tkyte@ORA817.US.ORACLE.COM>

ops$tkyte@ORA817.US.ORACLE.COM>

ops$tkyte@ORA817.US.ORACLE.COM> select distinct *

2 from t t1

3 where 5 >= ( select count(distinct t2.sal)

4 from t t2

5 where t2.deptno = t1.deptno

6 and t2.sal >= t1.sal )

7 /

DEPTNO SAL

———- ———-

10 100

10 200

10 300

10 400

10 500

20 300

20 400

20 500

20 600

20 700

30 100

30 200

30 300

30 400

14 rows selected.

And now using a new feature of 816:

ops$tkyte@ORA817.US.ORACLE.COM> select distinct *

2 from ( select deptno, sal,

3 dense_rank() over ( partition by deptno order by sal desc ) rank

4 from t )

5 where rank <= 5
6 /

DEPTNO SAL RANK

———- ———- ———-

10 100 5

10 200 4

10 300 3

10 400 2

10 500 1

20 300 5

20 400 4

20 500 3

20 600 2

20 700 1

30 100 4

30 200 3

30 300 2

30 400 1

14 rows selected.

Oracle, SQL scripts

Post navigation

Previous Post: import-export with multiple files
Next Post: find_longsql.sql

Related Posts

  • How to check current redo log progress redo_progress.sql Oracle
  • UNderstand and eliminate Latch contention. Oracle
  • 339939.1 Running Cluster Verification Utility to Diagnose Install Problems Oracle
  • Good links for x$ tables in oracle. Oracle
  • Rman Notes -1 Oracle
  • eplan.sql Oracle

Leave a Reply Cancel reply

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

Categories

  • Ansible (0)
  • AWS (2)
  • Azure (1)
  • Django (0)
  • GIT (1)
  • Linux/Unix (149)
  • MYSQL (5)
  • Oracle (400)
  • PHP/MYSQL/Wordpress (10)
  • POSTGRESQL (1)
  • Power-BI (0)
  • Python/PySpark (7)
  • RAC (18)
  • rman-dataguard (26)
  • shell (150)
  • SQL scripts (348)
  • SQL Server (6)
  • Uncategorized (3)
  • Videos (0)

Recent Posts

  • Running PDB on single node in RAC09-Apr-2026
  • find_arc.sql09-Apr-2026
  • pvm_pre_change.sql08-Apr-2026
  • find_encr_wallet.sql08-Apr-2026
  • find_pdbs.sql08-Apr-2026
  • Creating a Container Database using dbaascli08-Apr-2026
  • track_autoupgrade_copy_progress.sql01-Apr-2026
  • refre.sql for multitenant01-Apr-2026
  • prepfiles.sh for step by step generating pending statistics files10-Mar-2026
  • tracksqltime.sql05-Mar-2026

Archives

  • 2026
  • 2025
  • 2024
  • 2023
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006
  • 2005
  • Wait time tuning research Oracle
  • How does one SELECT a value from a table into a Unix variable? From SQL to Shell Linux/Unix
  • Oracle Support Metalink ID 161818.1 Oracle
  • oracle_env_10g_CADEV Linux/Unix
  • Implementing Listener Security Oracle
  • Jai Shree Ram PHP/MYSQL/Wordpress
  • find_encr_wallet.sql Uncategorized
  • handling filenname with space Linux/Unix

Copyright © 2026 pvmehta.com.

Powered by PressBook News WordPress theme