Consider below simple table:
Name Salary --------------- abc 100000 bcd 1000000 efg 40000 ghi 500000
How to find the employee whose salary is second highest. For example, in above table, “ghi” has the second highest salary as 500000.
Below is simple query to find the employee whose salary is highest.
SELECT name, MAX(salary) as salary FROM employee
We can nest the above query to find the second largest salary.
SELECT name, MAX(salary) AS salary
FROM employee
WHERE salary < (SELECT MAX(salary)
FROM employee);
There are other ways also as suggested by RajnishKrJha.
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 Server using Common Table Expression or CTE, we can find the second highest salary:
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 salary?
Simple, we can do one more nesting.
SELECT name, MAX(salary) AS salary
FROM employee
WHERE salary < (SELECT MAX(salary)
FROM employee
WHERE salary < (SELECT MAX(salary)
FROM employee)
);
Note that instead of nesting for second, third, etc largest salary, we can find nth salary using general query 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 salary.
Suppose you have to find 4th highest salary
SELECT * FROM employee WHERE salary= (SELECT DISTINCT(salary) FROM employee ORDER BY salary LIMIT 3,1);
Generic query will be
SELECT * FROM employee WHERE salary= (SELECT DISTINCT(salary) FROM employee ORDER BY salary LIMIT n-1,1);
This Solution is provided by Mohit
This article is contributed by Kartik. 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 important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
Recommended Posts:
- SQL query to find unique column values from table
- SQL | SELECT Query
- Structured Query Language (SQL)
- SQL | Query Processing
- Features of Structured Query Language (SQL)
- SQL query using COUNT and HAVING clause
- SQL Query Complexity
- SQL | Query to select NAME from table using different options
- How to find the highest normal form of a relation
- SQL | Difference between functions and stored procedures in PL/SQL
- Difference between T-SQL and PL-SQL
- Difference between SQL and T-SQL
- Query to find 2nd largest value in a column in Table
- Query Optimization in Relational Algebra
- Join operation Vs Nested query in DBMS
- Neo4j Query Cypher Language
- Virtual machine installation for Cassandra CQL query
- Database Roles in CQL (Cassandra Query Language)
- Additional Functions in CQL (Cassandra Query Language)
- Query Execution in HP Vertica

