금일은 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가 홀수이고, name이 'M'으로 시작하지 않는 나머지 모든 사람이 보너스를 받는다고 명시되어있다.
하지만 나머지는 0으로 표현해야한다.
위 조건을 잘못보고 7,9번 id값만 출력하는일이 없어야한다.
select employee_id , case when mod(employee_id,2)=1 and name not like 'M%' then salary else 0 end as 'bonus' from employees order by employee_id;
처음 필자는 mod명령어를 사용하였다. 프로그래밍을 기초를 배우다보면 mod*가 나오는데 나머지에대한 기능을 수행한다
따라서 case when 중에서 첫번째로 mod명렁어로 홀수인 값과 and를 통해 M으로 시작하지 않는 값은 salary 값을 출력하고 그렇지 않으면 0을 출력하게 해두었다.
이 방법 말고도 select employee_id , if(employee_id%2=1 and name not like'M%', salary,0) as bonus from Employees order by employee_id asc
if절을 통한 구현이 가능하다.
추후 이러한 기능말고도 union 명령어로도 해당조건값과 아닌값을 따로 불러와서 합치는 기능으로도 가능하다. 이부분은 추후 다른문제에서 구현할 수 있으면 하고자 한다.
*mod(속성값,나눌값)=나머지
Table: Salary
+-------------+----------+
| Column Name | Type |
+-------------+----------+
| id | int |
| name | varchar |
| sex | ENUM |
| salary | int |
+-------------+----------+
id is the primary key for this table.
The sex column is ENUM value of type ('m', 'f').
The table contains information about an employee.
Write an SQL query to swap all 'f' and 'm' values (i.e., change all 'f' values to 'm' and vice versa) with a single update statement and no intermediate temporary tables.
Note that you must write a single update statement, do not write any select statement for this problem.
The query result format is in the following example.
Example 1:
Input:
Salary table:
+----+------+-----+--------+
| id | name | sex | salary |
+----+------+-----+--------+
| 1 | A | m | 2500 |
| 2 | B | f | 1500 |
| 3 | C | m | 5500 |
| 4 | D | f | 500 |
+----+------+-----+--------+
Output:
+----+------+-----+--------+
| id | name | sex | salary |
+----+------+-----+--------+
| 1 | A | f | 2500 |
| 2 | B | m | 1500 |
| 3 | C | f | 5500 |
| 4 | D | m | 500 |
+----+------+-----+--------+
Explanation:
(1, A) and (3, C) were changed from 'm' to 'f'.
(2, B) and (4, D) were changed from 'f' to 'm'.
서로 스왑 문제이다.
이 문제를 보고 select 로 구현하고자 하지 말아야 한다. 여기서 보면 Note that you must write a single update statement, do not write any select statement for this problem.
select 문이 아닌 단일 update 문을 사용하라고 명시되어있다.
update salary set sex = case when sex='m' then 'f' else 'm' end;
case 문 뿐만 아니라 if절로도 가능하다 필자는 case 문을 사용하였다.
Table: Person
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| email | varchar |
+-------------+---------+
id is the primary key column for this table.
Each row of this table contains an email. The emails will not contain uppercase letters.
Write an SQL query to delete all the duplicate emails, keeping only one unique email with the smallest id. Note that you are supposed to write a DELETE statement and not a SELECT one.
After running your script, the answer shown is the Person table. The driver will first compile and run your piece of code and then show the Person table. The final order of the Person table does not matter.
The query result format is in the following example.
Example 1:
Input:
Person table:
+----+------------------+
| id | email |
+----+------------------+
| 1 | john@example.com |
| 2 | bob@example.com |
| 3 | john@example.com |
+----+------------------+
Output:
+----+------------------+
| id | email |
+----+------------------+
| 1 | john@example.com |
| 2 | bob@example.com |
+----+------------------+
Explanation: john@example.com is repeated two times. We keep the row with the smallest Id = 1.
이 문제는 조건에서 delete 명령어를 사용하라고 명시되어있다.
필자는 처음에
delete p1 from Person as p1 inner join Person as p2 on p1.email=p2.email and p1.id>p2.id;
첫번쨰 조건을 적용시켜서 p1 과 p2를 데이터를 나타내면
1.john@example.com 1.john@example.com
1.john@example.com 3.john@example.com
3.john@example.com 1.john@example.com
3.john@example.com 3.john@example.com
따라서 3번째 행이 일치하므로 3번쨰 행을 삭제하게된다(p1)
또한
delete from Person where Id not in
(select min_id from (select min(Id) as min_id from Person group by Email) as person2) ;
이 명령어는 delete 문안에 not in으로 이값이 포함하지 않는 데이터를 where로 선언하였다 따라서
서브쿼리문을 해석하면 group by email로 잡고 select 시키면 1, 2행만 나온다 따라서 현재 person 테이블 3번째 행만 삭제한다
만약 여기서 delete from Person where Id not in (select min(Id) as min_id from Person group by Email); 로 할 수있지 않냐 생각들겠지만 이렇게 작동하면
You can't specify target table 'Person' for update in FROM clause
에러가 뜬다 왜냐 mysql은 delete , update시에 자기 date를 바로 처리못하므로 서브쿼리를 하나 더 넣어줘야한다! <-이거 정말 중요
'군대 활동 > 공부' 카테고리의 다른 글
SQL 공부 (leetcode 3편) (0) | 2022.10.09 |
---|---|
알고리즘 공부 Leetcode Day5 (0) | 2022.10.09 |
알고리즘 공부 Leetcode Day4 (0) | 2022.10.06 |
SQL 공부 (leetcode 1편) (0) | 2022.10.04 |
leetcode 공부 Day3 (BinarySearch Final) (0) | 2022.10.04 |