[1873_EASY] Calculate Special Bonus

2022. 8. 4. 23:47·SQL/LeetCode

Table: Employees

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| employee_id | int     |
| name        | varchar |
| salary      | int     |
+-------------+---------+
employee_id is the primary key for this table.
Each row of this table indicates the employee ID, employee name, and salary.

 

Write an SQL query to calculate the bonus of each employee. The bonus of an employee is 100% of their salary if the ID of the employee is an odd number and the employee name does not start with the character 'M'. The bonus of an employee is 0 otherwise.

Return the result table ordered by employee_id.

The query result format is in the following example.

 

Example 1:

Input: 
Employees table:
+-------------+---------+--------+
| employee_id | name    | salary |
+-------------+---------+--------+
| 2           | Meir    | 3000   |
| 3           | Michael | 3800   |
| 7           | Addilyn | 7400   |
| 8           | Juan    | 6100   |
| 9           | Kannon  | 7700   |
+-------------+---------+--------+
Output: 
+-------------+-------+
| employee_id | bonus |
+-------------+-------+
| 2           | 0     |
| 3           | 0     |
| 7           | 7400  |
| 8           | 0     |
| 9           | 7700  |
+-------------+-------+
Explanation: 
The employees with IDs 2 and 8 get 0 bonus because they have an even employee_id.
The employee with ID 3 gets 0 bonus because their name starts with 'M'.
The rest of the employees get a 100% bonus.

문제 조건 

employee_id가 홀수이고 이름의 첫 번째 글자가 'M'이 아니면 급여의 100%를 보너스로 받고, 그렇지 않을 시 보너스가 없는 형식으로 결과를 출력하시오.

 

문제 풀이

SELECT employee_id,
        CASE WHEN LEFT(name,1) != 'M' AND MOD(employee_id,2) != 0 THEN salary ELSE 0 END AS bonus
FROM Employees 
ORDER BY employee_id
저작자표시 (새창열림)

'SQL > LeetCode' 카테고리의 다른 글

[1321#_MEDIUM] Restaurant Growth  (0) 2022.08.05
[1890_EASY] The Latest Login in 2020  (0) 2022.08.05
[1853#_EASY] Convert Date Format  (0) 2022.08.04
[1308#_MEDIUM] Running Total for Different Genders  (0) 2022.08.03
[1285#_MEDIUM] Find the Start and End Number of Continuous Ranges  (0) 2022.08.03
'SQL/LeetCode' 카테고리의 다른 글
  • [1321#_MEDIUM] Restaurant Growth
  • [1890_EASY] The Latest Login in 2020
  • [1853#_EASY] Convert Date Format
  • [1308#_MEDIUM] Running Total for Different Genders
소금깨
소금깨
  • 소금깨
    고군분투 인생살이
    소금깨
  • 전체
    오늘
    어제
    • 분류 전체보기 (328)
      • SQL (271)
        • 프로그래머스 (27)
        • LeetCode (198)
        • Hacker Rank (27)
        • Solve SQL (1)
        • 개념 (15)
      • 데이터 분석 (16)
        • 참고하며 공부하기 (14)
      • 기타 (15)
        • 통계 (14)
      • 오류 (6)
      • 인생살이 (0)
        • 리뷰 (0)
        • 일기 (0)
      • 中文 (0)
      • TABLEAU (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    easy
    프로그래머스
    group by
    해커랭크
    패캠챌린지
    파이썬을활용한시계열데이터분석
    HACKER_RANK
    파이썬을활용한시계열데이터분석AtoZ올인원패키지Online
    패스트캠퍼스후기
    패스트캠퍼스
    medium
    SQL
    시계열데이터분석
    solvesql
    LeetCode
    직장인인강
    직장인자기계발
    프리미엄
    MySQL
    Hard
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
소금깨
[1873_EASY] Calculate Special Bonus
상단으로

티스토리툴바