+-------------+---------+
|Column Name |Type|+-------------+---------+
| emp_name | varchar || department | varchar || salary | int |+-------------+---------+
(emp_name, department) is the primarykey (combination ofuniquevalues) for this table.
Eachrowof this tablecontains emp_name, department and salary. There will be **at least one** entry for the engineering and marketing departments.
Write a solution to calculate the difference between the highest salaries in the marketing and engineeringdepartment. Output the absolute difference in salaries.
Input:
Salaries table:+----------+-------------+--------+| emp_name | department | salary |+----------+-------------+--------+| Kathy | Engineering |50000|| Roy | Marketing |30000|| Charles | Engineering |45000|| Jack | Engineering |85000|| Benjamin | Marketing |34000|| Anthony | Marketing |42000|| Edward | Engineering |102000|| Terry | Engineering |44000|| Evelyn | Marketing |53000|| Arthur | Engineering |32000|+----------+-------------+--------+Output:
+-------------------+| salary_difference |+-------------------+|49000|+-------------------+Explanation:
- The Engineering and Marketing departments have the highest salaries of 102,000 and 53,000, respectively. Resulting in an absolute difference of 49,000.
We need the highest salary in each of the two departments, then compute their absolute difference. Aggregation functions like MAX and ABS are perfect for this.
SELECTABS(
(SELECTMAX(salary) FROM Salaries WHERE department ='engineering') - (SELECTMAX(salary) FROM Salaries WHERE department ='marketing')
) AS salary_difference;
1
2
3
4
SELECTABS(
(SELECTMAX(salary) FROM Salaries WHERE department ='engineering') - (SELECTMAX(salary) FROM Salaries WHERE department ='marketing')
) AS salary_difference;
1
2
3
4
5
defhighest_salaries_difference(df):
eng = df[df['department'] =='engineering']['salary'].max()
mkt = df[df['department'] =='marketing']['salary'].max()
ans = abs(eng - mkt)
return ans