1
votes

I'm a novice. I have the following Employee table.

ID  Name  Country  Salary  ManagerID

I retrieved the 3rd max salary using the following.

select name , salary From (
    select name, salary from 
    employee sort by salary desc limit 3)
    result sort by salary limit 1;

How to do the same to display 3rd max salary for each country? can we use OVER (PARTITION BY country)? I tried looking in the languageManual Windowing and Analytics but I'm finding it difficult to understand. Please help!

1

1 Answers

2
votes

You're definitely on the right track with windowing functions. row_number() is a good function to use here.

select name, salary
from (
  select name
    , salary
    , row_number() over (partition by country order by salary desc) idx
  from employee ) x
where idx = 3

when you order by salary, make sure that it is a numerical type, or it will not be sorted correctly.