//
you're reading...
SQL Server Query

SQL Server Co-related Query – Select the second highest

This a good example of the co-related query.

For example we have table with 5 employees and their salary.

EmplyeeTable

If we want to query the employee with second highest salary, we can use the query below.

SELECT * FROM Employee e1
WHERE 2 = ( SELECT COUNT(*) FROM Employee e2
WHERE e2.EmployeeSalary >= e1.EmployeeSalary );
Advertisements

About daviewning

I am an Oracle DBA

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: