nd Highest Salary In Oracle

The top 5 highest-earning employees at Oracle are:

  1. Safra Catz (President and Chief Financial Officer) – $77.7 million
  2. Mark Hurd (Chief Executive Officer) – $72.0 million
  3. Thomas Kurian (President of Product Development) – $45.5 million
  4. Charles Phillips (Senior Vice President of Cloud Services) – $43.2 million
  5. Thomas Kurian (President of Product Development) – $41 million

Oracle is a database management system that allows users to query and retrieve data from multiple relational databases. It is one of the most popular database systems in use today, and it is used by more than 100 million businesses worldwide. In fact, Oracle was ranked as the second most popular enterprise software brand in the world in 2018.

The company has its headquarters in Redwood Shores, California, where it employs more than 38,000 people worldwide. Oracle also has offices around the globe, including locations in Europe and Australia.

nd Highest Salary In Oracle

problem of finding the Nth maximum salary from the Employee table is by using the correlated subquery. This is a special type of subquery where the subquery depends upon the main query and execute for every row returned by the main query.  It’s slow but it can solve problems which are difficult to solve otherwise. Let’s see the SQL query to find the Nth highest salary using the Correlated subquery.

SQL Query:

SELECT name, salary 
FROM #Employee e1
WHERE N-1 = (SELECT COUNT(DISTINCT salary) FROM #Employee e2
WHERE e2.salary > e1.salary)

for the 2nd maximum you can replace N with 2, and for 3rd maximum replace N with 3, here is the output:

2nd highest salary:

SELECT name, salary 
FROM #Employee e1
WHERE N-1 = (SELECT COUNT(DISTINCT salary) FROM #Employee e2
WHERE e2.salary > e1.salary)SELECT name, salary 
FROM #Employee e1
WHERE 2-1 = (SELECT COUNT(DISTINCT salary) FROM #Employee e2
WHERE e2.salary > e1.salary)

Result:
name salary
Peter 5000

3rd highest salary:

SELECT name, salary 
FROM #Employee e1
WHERE 3-1 = (SELECT COUNT(DISTINCT salary) FROM #Employee e2
WHERE e2.salary > e1.salary)

Result:
name salary
John 4000

Explanation :

The distinct keyword is there to deal with duplicate salaries in the table. In order to find the Nth highest salary, we are only considering unique salaries. The highest salary means no salary is higher than it, the Second highest means only one salary is higher than it, 3rd highest means two salaries are higher than it, similarly Nth highest salary means N-1 salaries are higher than it.

Pros :
1) The generic solution works in all databases including Oracle, MySQL, SQL SERVER, and PostgreSQL.

Cons :
1) Slow, because the inner query will run for every row processed by the outer query.

See SQL Puzzles and Answers book for more of such SQL queries for practicing and improving your SQL query skill.

The nth highest salary in SQL SERVER using TOP keyword

You can use the TOP keyword to find the Nth highest salary in SQL SERVER. This is also faster than the previous solution because here we are calculating Nth maximum salary without a subquery.

SELECT TOP 1 salary
FROM (
SELECT DISTINCT TOP N salary
FROM #Employee
ORDER BY salary DESC
) AS temp
ORDER BY salary

Explanation: 

By default ORDER BY clause print rows in ascending order, since we need the highest salary at the top, we have used ORDER BY DESC, which will display salaries in descending order. Again DISTINCT is used to remove duplicates. The outer query will then pick the topmost salary, which would be your Nth highest salary.

And, if you like books and just learning these queries from the interview’s sake then I suggest you first read a good book on SQL like Head First SQL. It will help you to build your fundamentals.

3rd highest salary in SQL SERVER

SELECT TOP 1 salary
FROM (
SELECT DISTINCT TOP 3 salary
FROM #Employee
ORDER BY salary DESC
) AS temp
ORDER BY salary

Result:
salary
4000

Read more: https://javarevisited.blogspot.com/2016/01/4-ways-to-find-nth-highest-salary-in.html#ixzz7Ylm0sUAA

Leave a Reply