Skip to content

Commit 921e437

Browse files
authored
Update swap-salary.sql
1 parent 702c284 commit 921e437

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

MySQL/swap-salary.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
# Time: O(n)
22
# Space: O(1)
33

4+
# Given a table salary, such as the one below, that has m=male and f=female values.
5+
# Swap all f and m values (i.e., change all f values to m and vice versa) with a single update query and no intermediate temp table.
6+
#
7+
# For example:
8+
# | id | name | sex | salary |
9+
# |----|------|-----|--------|
10+
# | 1 | A | m | 2500 |
11+
# | 2 | B | f | 1500 |
12+
# | 3 | C | m | 5500 |
13+
# | 4 | D | f | 500 |
14+
#
15+
# After running your query, the above salary table should have the following rows:
16+
# | id | name | sex | salary |
17+
# |----|------|-----|--------|
18+
# | 1 | A | f | 2500 |
19+
# | 2 | B | m | 1500 |
20+
# | 3 | C | f | 5500 |
21+
# | 4 | D | m | 500 |
22+
423
UPDATE salary SET sex = IF(sex = 'm', 'f', 'm')

0 commit comments

Comments
 (0)