0% found this document useful (0 votes)
255 views

SQL Query To Find Second Highest Salary - GeeksforGeeks

sql

Uploaded by

SRILATHA VANGARU
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
255 views

SQL Query To Find Second Highest Salary - GeeksforGeeks

sql

Uploaded by

SRILATHA VANGARU
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

6/19/2021 SQL query to find second highest salary?

- GeeksforGeeks

SQL query to find second highest salary?


Difficulty Level :
Easy ● Last Updated :
01 Dec, 2020

Consider below simple table:

Name Salary

---------------

abc 100000

bcd 1000000

efg 40000

ghi 500000

How to find the employee whose salar y is second highest. For example, in above table,

“ghi” has the second highest salar y as 500000.

Below is simple quer y to find the employee whose salar y is highest.

SELECT name, MAX(salary) as salary FROM employee

We can nest the above quer y to find the second largest salar y.

SELECT name, MAX(salary) AS salary

FROM employee

WHERE salary < (SELECT MAX(salary)

FROM employee);

There are other ways also as suggested by RajnishKrJha.

https://www.geeksforgeeks.org/sql-query-to-find-second-largest-salary/ 1/6
6/19/2021 SQL query to find second highest salary? - GeeksforGeeks

Related Articles

SELECT name, MAX(salary) AS salary

FROM employee

WHERE salary IN

(SELECT salary FROM employee MINUS SELECT MAX(salary)

FROM employee);

SELECT name, MAX(salary) AS salary

FROM employee

WHERE salary (SELECT MAX(salary)

FROM employee);

One way as suggested by Arka Poddar.

IN SQL Ser ver using Common Table Expression or CTE, we can find the second highest

salar y:

WITH T AS

SELECT *

DENSE_RANK() OVER (ORDER BY Salary Desc) AS Rnk

FROM Employees

SELECT Name

FROM T

WHERE Rnk=2;

How to find the third largest salar y?

Simple, we can do one more nesting.

SELECT name, MAX(salary) AS salary

FROM employee

WHERE salary < (SELECT MAX(salary)



https://www.geeksforgeeks.org/sql-query-to-find-second-largest-salary/ 2/6
6/19/2021 SQL query to find second highest salary? - GeeksforGeeks

FROM employee

WHERE salary < (SELECT MAX(salary)

FROM employee)

);

Note that instead of nesting for second, third, etc largest salar y, we can find nth salar y

using general quer y like in MySQL:

SELECT salary

FROM employee

ORDER BY salary desc limit n-1,1

SELECT name, salary

FROM employee A

WHERE n-1 = (SELECT count(1)

FROM employee B

WHERE B.salary>A.salary)

If multiple employee have same salar y.

Suppose you have to find 4th highest salar y

SELECT * FROM employee

WHERE salary= (SELECT DISTINCT(salary)

FROM employee ORDER BY salary LIMIT 3,1);

Generic quer y will be

SELECT * FROM employee

WHERE salary= (SELECT DISTINCT(salary)

FROM employee ORDER BY salary LIMIT n-1,1);

1. 2nd Highest Salary (Top 50 SQL Interview Questions)| GeeksforGeeks

This Solution is provided by Mohit

https://www.geeksforgeeks.org/sql-query-to-find-second-largest-salary/ 3/6
6/19/2021 SQL query to find second highest salary? - GeeksforGeeks

This ar ticle is contributed by Kar tik. Please write comments if you find anything incorrect,

or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the impor tant CS Theor y

concepts for SDE inter views with the CS Theor y Course at a student-friendly price and

become industr y ready.

Like 0

Previous Next

RECOMMENDED ARTICLES Page : 1 2 3

SQL Query to find an employee SQL Query to find All Sundays


01 05
whose salary is equal to or greater Between Two Dates
than a specific number 01, Apr 21

01, Apr 21

Query to find 2nd largest value in a


06
How to find the highest normal column in Table
02
form of a relation 11, Jun 19

21, Dec 15

SQL query to find unique column


07
SQL Query to Find the Year from values from table
03
Date 11, Aug 20

10, Apr 21

SQL Query to find the Nth Largest


08
SQL Query to Find the Sum of all Value in a Column using Limit and
04
Values in a Column Offset
11, Apr 21 22, Oct 20

https://www.geeksforgeeks.org/sql-query-to-find-second-largest-salary/ 4/6
6/19/2021 SQL query to find second highest salary? - GeeksforGeeks

Ar ticle Contributed By :

GeeksforGeeks

Vote for difficulty

Current difficulty :
Easy

Easy Normal Medium Hard Expert

Improved By : p3arl, pradeepthyadi, hespacelti

Article Tags : DBMS-SQL, DBMS

Practice Tags : DBMS

Improve Article Report Issue

Writing code in comment?


Please use ide.geeksforgeeks.org,
generate link and share the link here.

Load Comments

5th Floor, A-118,

Sector-136, Noida, Uttar Pradesh - 201305

[email protected]

Company Learn
About Us Algorithms
Careers Data Structures

Privacy Policy Languages
https://www.geeksforgeeks.org/sql-query-to-find-second-largest-salary/ 5/6
6/19/2021
y y g g
SQL query to find second highest salary? - GeeksforGeeks

Contact Us CS
Subjects
Copyright Policy Video Tutorials

Practice Contribute
Courses Write an Article
Company-wise Write Interview
Experience
Topic-wise Internships
How to begin? Videos

@geeksforgeeks
, Some rights reserved

https://www.geeksforgeeks.org/sql-query-to-find-second-largest-salary/ 6/6

You might also like