Skip to main content

Finding nth highest & lowest salary example and explanation

Finding the nth highest &lowest salary in SQL have lots of query over the google,but i found good explanation here.

   
SELECT * /*This is the outer query part */
FROM Employee Emp1
WHERE (N-1) = ( /* Subquery starts here */
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)

1. How to find 2nd highest or 3rd highest salary of an employee. 
   I am providing you a genral query to pick the nth highest salary from emp table it is not database specific query.
    select salary from emp e1 where (n-1)=(select count(*)  from emp where salary > e1.salary );

2.  You can also pick nth lowest salary of an employee.
     select salary from emp e1 where (n-1)=(select count(*)  from emp where salary < e1.salary ) ;



How does the query above work?

The query above can be quite confusing if you have not seen anything like it before – pay special attention to the fact that “Emp1″ appears in both the subquery (also known as an inner query) and the “outer” query. The outer query is just the part of the query that is not the subquery/inner query – both parts of the query are clearly labeled in the comments.

Understanding and visualizing how the query above works

Let’s assume that we are using this data:
Employee
Employee IDSalary
3200
4800
7450

3rd highest salary:

select * from Employeee as e1 where (N-1) =(select count(disticnt(e1.salary)
from emplyee as e2 where e2.salary>e1.salary)


select * from Employeee as e1 where (2) =(select count(disticnt(e1.salary)
from emplyee as e2 where e2.salary>200) 

Result:
2=3


select * from Employeee as e1 where (2) =(select count(disticnt(e1.salary)
from emplyee as e2 where e2.salary>800) 

Result:
2=1


select * from Employeee as e1 where (2) =(select count(disticnt(e1.salary)
from emplyee as e2 where e2.salary>450) 

Result:
2=2
Result is true so our o/p successfully got

O/p
450

Comments

Popular posts from this blog

Asp.net- Encrypt and Decrypt connection strings in web.config file

In this article I will explain how to encrypt or decrypt connectionStrings in web.config file using asp.net. If we are using applications in our internal servers with security then it’s ok if we deploy our applications in shared host environment then we have chance to arise security problems to avoid these problems asp.net 2.0 provided built in protected configuration model functionality to encrypt or decrypt few sections of web.config file those are RSAProtectedConfigurationProvider :   This is default provider and uses the RSA public key encryption algorithm to encrypt and decrypt data. DataProtectionConfgurationProvider : This provider uses windows data protection application programming interface to encrypt and decrypt the data. The encrypting and decrypting of connection strings in web.config file will do by using aspnet_regiis.exe  command line tool and  code behind . First Method : First we will do encryption and decryption using  aspnet...

CRM Automate build Solution using Powershell commands

In CRM if there is any solution movement from other enviornment like DEV,PROD,STG we use to export the solution and import the solution to the respective environment by logging into MS Dynamics CRM. This will tends to extra effort to do manually for developers or release managers. So what if release user or test user can build and deploy the Solution deployment without CRM intervention(CRM loggin in) We need to do some automate build and deployment. In this article i write about how Import/Export solution automate to the various environment without intervention of logging into MS Dynamics CRM. XRM CI Framework,this is one tool use to automate the build in CRM       Below link is for download the XRM CI Framework https://xrmciframework.codeplex.com/releases/view/125516 Download and extract the ZIP file . First HelloWorld example “WhoAmIRequest” 1.Open "Powershell" and "Run as a Administrator" 2.Navigate to Extrac...

Auto number generation using C# and SQL

Auto number generation using C# and SQL ------------------------------------------------------ Req :  Create a Autonumber for Particular Application form(Eg:Order or Quote) Approach : ------------- 1.Create a Separate autonumber configuration table which looks like below AutoNumConfig table --------------------------   AutoNumberId  AutoNumber   FormNameorFormId   -------------------------------------------    1             ORD-0002     Order or 1066    2             QUO-0002     Quote or 1067 2.Create Transaction table for Application  Form(Eg:Order/Quote) which looks like below OrderTable -------------- OrderId   OrderName  OrderAutoNum -------------------------------------------   1       Spartan      ORD-0001   2       Xamarin      ORD-0002 QuoteT...