返回介绍

solution / 2800-2899 / 2853.Highest Salaries Difference / README_EN

发布于 2024-06-17 01:02:59 字数 2616 浏览 0 评论 0 收藏 0

2853. Highest Salaries Difference

中文文档

Description

Table: Salaries

+-------------+---------+ 
| Column Name | Type  | 
+-------------+---------+ 
| emp_name  | varchar | 
| department  | varchar | 
| salary    | int   |
+-------------+---------+
(emp_name, department) is the primary key (combination of unique values) for this table.
Each row of this table contains 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 engineering department. Output the absolute difference in salaries.

Return_ the result table._

The result format is in the following example.

 

Example 1:

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.

Solutions

Solution 1: GROUP BY Clause

We can first calculate the highest salary for each department, and then calculate the difference between the two highest salaries.

# Write your MySQL query statement below
SELECT MAX(s) - MIN(s) AS salary_difference
FROM
  (
    SELECT MAX(salary) AS s
    FROM Salaries
    GROUP BY department
  ) AS t;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文