Write an SQL query to find the second highest distinct salary from the employee table ?
Simply we can find using the window function i.e rank function
Query Explanation:
We want the second highest salary, but some salaries may be repeated (like 90000 appears twice).
First, we use DISTINCT salary to remove duplicates.
Then, we use the RANK() function to rank the salaries from highest to lowest.
The highest salary gets rank 1, the next gets rank 2, and so on.
Finally, we pick the salary where the rank is 2 — that’s the second highest.
o/p of the query:80000