返回介绍

solution / 2600-2699 / 2668.Find Latest Salaries / README_EN

发布于 2024-06-17 01:03:01 字数 3747 浏览 0 评论 0 收藏 0

2668. Find Latest Salaries

中文文档

Description

Table: Salary

+---------------+---------+ 
| Column Name   | Type  | 
+---------------+---------+ 
| emp_id    | int   | 
| firstname   | varchar |
| lastname    | varchar |
| salary    | varchar |
| department_id | varchar |
+---------------+---------+
(emp_id, salary) is the primary key (combination of columns with unique values) for this table.
Each row contains employees details and their yearly salaries, however, some of the records are old and contain outdated salary information. 

Write a solution to find the current salary of each employee assuming that salaries increase each year. Output their emp_id, firstname, lastname, salary, and department_id.

Return the result table ordered by emp_id in ascending order_._

The result format is in the following example.

 

Example 1:

Input:
Salary table:
+--------+-----------+----------+--------+---------------+
| emp_id | firstname | lastname | salary | department_id |
+--------+-----------+----------+--------+---------------+ 
| 1    | Todd    | Wilson   | 110000 | D1006     |
| 1    | Todd    | Wilson   | 106119 | D1006     | 
| 2    | Justin  | Simon  | 128922 | D1005     | 
| 2    | Justin  | Simon  | 130000 | D1005     | 
| 3    | Kelly   | Rosario  | 42689  | D1002     | 
| 4    | Patricia  | Powell   | 162825 | D1004     |
| 4    | Patricia  | Powell   | 170000 | D1004     |
| 5    | Sherry  | Golden   | 44101  | D1002     | 
| 6    | Natasha   | Swanson  | 79632  | D1005     | 
| 6    | Natasha   | Swanson  | 90000  | D1005     |
+--------+-----------+----------+--------+---------------+
Output:
+--------+-----------+----------+--------+---------------+
| emp_id | firstname | lastname | salary | department_id |
+--------+-----------+----------+--------+---------------+ 
| 1    | Todd    | Wilson   | 110000 | D1006     |
| 2    | Justin  | Simon  | 130000 | D1005     | 
| 3    | Kelly   | Rosario  | 42689  | D1002     | 
| 4    | Patricia  | Powell   | 170000 | D1004     |
| 5    | Sherry  | Golden   | 44101  | D1002     | 
| 6    | Natasha   | Swanson  | 90000  | D1005     |
+--------+-----------+----------+--------+---------------+

Explanation:
- emp_id 1 has two records with a salary of 110000, 106119 out of these 110000 is an updated salary (Assuming salary is increasing each year)
- emp_id 2 has two records with a salary of 128922, 130000 out of these 130000 is an updated salary.
- emp_id 3 has only one salary record so that is already an updated salary.
- emp_id 4 has two records with a salary of 162825, 170000 out of these 170000 is an updated salary.
- emp_id 5 has only one salary record so that is already an updated salary.
- emp_id 6 has two records with a salary of 79632, 90000 out of these 90000 is an updated salary.

Solutions

Solution 1

# Write your MySQL query statement below
SELECT
  emp_id,
  firstname,
  lastname,
  MAX(salary) AS salary,
  department_id
FROM Salary
GROUP BY emp_id
ORDER BY emp_id;

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

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

发布评论

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