Skip to content
pvmehta.com

pvmehta.com

  • Home
  • About Me
  • Toggle search form
  • How to choose Driver table in SQL statement Oracle
  • Oracle vs MYSQL Architecture differences (For DBAs) MYSQL
  • How to find where datafile is created dbf_info.sql Oracle
  • 276434.1 Modifying the VIP or VIP Hostname of a 10g or 11g Oracle Clusterware Node Oracle
  • fkwoindex.sql /* Find FK without Index */ Oracle
  • SYSOPER Mystery Oracle
  • Wait.sql Oracle
  • Locktree.sql Oracle
  • DBMS_Shared_pool pinning triggers Oracle
  • catall.sh Linux/Unix
  • send email from unix mailx with attachment. Linux/Unix
  • Process Map for CPU and Memory for OS processes Linux/Unix
  • Generating XML from SQLPLUS Oracle
  • Oracle Standby Database Library Index from Metalink Oracle
  • oracle fast start failover best practice 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

  • SQL Tracker by SID sqltrackerbysid.sql Oracle
  • To see how much time or progress of long transaction Oracle
  • create PLAN_TABLE command. Oracle
  • Export With Query Another Example. Oracle
  • Best approach for Oracle database patching sequence to latest/required patchset along with CPU/PSU/any-other-one-off patch ID 865255.1 Oracle
  • shr1.sql for MTS or Shared server configuration 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 (393)
  • PHP/MYSQL/Wordpress (10)
  • POSTGRESQL (0)
  • Power-BI (0)
  • Python/PySpark (7)
  • RAC (17)
  • rman-dataguard (26)
  • shell (149)
  • SQL scripts (342)
  • SQL Server (6)
  • Uncategorized (0)
  • Videos (0)

Recent Posts

  • Trace a SQL session from another session using ORADEBUG30-Sep-2025
  • 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

Archives

  • 2025
  • 2024
  • 2023
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006
  • 2005
  • Default User Profile Oracle
  • chk_space_SID.ksh Linux/Unix
  • Kernel Parameter setting explaination for Processes Parameter Linux/Unix
  • Small sample shell program Linux/Unix
  • On solaris 10, “S” link is not part of $ORACLE_HOME/bin/oracle as default. ( For 9.2.0.8) Oracle
  • find the files that are 1 day old. Linux/Unix
  • Sending SQLPLUS output in HTML format Oracle
  • pvmehta.com SQL scripts
    Find which sessions is accessing object that prevent your session to have exclusive locks in Oracle Oracle

Copyright © 2025 pvmehta.com.

Powered by PressBook News WordPress theme