+------------------+---------+
| Column Name | Type |
+------------------+---------+
| emp_id | int |
| salary | int |
| dept | varchar |
+------------------+---------+
emp_id is the unique key for this table.
Each row of this table contains information about an employee including their ID, salary, and department.
Write a solution to find the employees who earn the second-highest salary in each department. If multiple employees have the second-highest salary ,
includeall employees with that salary.
Return the result tableordered byemp_idinascendingorder.
Input:
employees table:+--------+--------+-----------+| emp_id | salary | dept |+--------+--------+-----------+|1|70000| Sales ||2|80000| Sales ||3|80000| Sales ||4|90000| Sales ||5|55000| IT ||6|65000| IT ||7|65000| IT ||8|50000| Marketing ||9|55000| Marketing ||10|55000| HR |+--------+--------+-----------+Output:
+--------+-----------+| emp_id | dept |+--------+-----------+|2| Sales ||3| Sales ||5| IT ||8| Marketing |+--------+-----------+Explanation:
***Sales Department**:* Highest salary is90000(emp_id:4)* Second-highest salary is80000(emp_id:2,3)* Both employees with salary 80000 are included
***IT Department**:* Highest salary is65000(emp_id:6,7)* Second-highest salary is55000(emp_id:5)* Only emp_id 5is included as they have the second-highest salary
***Marketing Department**:* Highest salary is55000(emp_id:9)* Second-highest salary is50000(emp_id:8)* Employee 8is included
***HR Department**:* Only has one employee
* Not included in the result as it has fewer than 2 employees
We want all employees with the second-highest salary in each department. This can be done by ranking salaries within each department and selecting those with rank 2. In pandas, we can group and filter accordingly.
SELECT emp_id, dept
FROM employees e
WHERE salary = (
SELECTDISTINCT salary FROM employees e2
WHERE e2.dept = e.dept
ORDERBY salary DESCLIMIT1OFFSET1)
ORDERBY emp_id ASC;
1
2
3
4
5
6
7
8
SELECT emp_id, dept
FROM (
SELECT emp_id, dept, salary,
DENSE_RANK() OVER (PARTITION BY dept ORDERBY salary DESC) as rnk
FROM employees
) t
WHERE rnk =2ORDERBY emp_id ASC;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# employees is a pandas DataFrameimport pandas as pd
defsecond_highest_salary(df):
res = []
for dept, group in df.groupby('dept'):
uniq = sorted(group['salary'].unique(), reverse=True)
if len(uniq) <2:
continue sec = uniq[1]
res.append(group[group['salary'] == sec][['emp_id', 'dept']])
if res:
result = pd.concat(res).sort_values('emp_id').reset_index(drop=True)
else:
result = pd.DataFrame(columns=['emp_id', 'dept'])
return result
# result = second_highest_salary(employees)