devxlogo

Department Wise Employee Ranking With Single T-SQL Query

Department Wise Employee Ranking With Single T-SQL Query

This code will retrieve the employee details with a salary-wise ranking from each dept.EMP table with different departments and different salaries. If two employees in the same department have the same salary, then they’ll have the same rank.

 EMP Table----------DEPT        NAME                      SALARY$----------- ------------------------- ----------------10          JAMES                     2000.0000010          ADAMS                     1500.0000010          GEORGE                    2000.0000010          JAY                       3000.0000020          LARANCE                   3000.0000020          NICOLES                   4000.0000020          DAVID                     2000.0000020          BORDER                    1500.0000010          BOB                       3000.0000010          SMITH                     1000.00000select b1.dept,b1.name,b1.salary$,rank= _(select count(distinct salary$)+1from emp b2 whereb1.dept=b2.dept and b2.salary$>b1.salary$)from emp b1 order by dept,b1.salary$ descResult-------dept      name                   salary$              rank--------- ---------------------- -------------------- -----10        JAY                    3000.00000           110        BOB                    3000.00000           110        JAMES                  2000.00000           210        GEORGE                 2000.00000           210        ADAMS                  1500.00000           310        SMITH                  1000.00000           420        NICOLES                4000.00000           120        LARANCE                3000.00000           220        DAVID                  2000.00000           320        BORDER                 1500.00000           4
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist