Skip to content

Commit 312b11f

Browse files
committed
add 627
1 parent 44a8250 commit 312b11f

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

627-swap_salary.sql

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
-- 627. Swap Salary
2+
3+
-- Given a table salary, such as the one below, that has m=male and f=female values.
4+
-- Swap all f and m values (i.e., change all f values to m and vice versa)
5+
-- with a single update query and no intermediate temp table.
6+
7+
-- For example:
8+
9+
-- | id | name | sex | salary |
10+
-- |----|------|-----|--------|
11+
-- | 1 | A | m | 2500 |
12+
-- | 2 | B | f | 1500 |
13+
-- | 3 | C | m | 5500 |
14+
-- | 4 | D | f | 500 |
15+
16+
-- After running your query, the above salary table should have the following rows:
17+
18+
-- | id | name | sex | salary |
19+
-- |----|------|-----|--------|
20+
-- | 1 | A | f | 2500 |
21+
-- | 2 | B | m | 1500 |
22+
-- | 3 | C | f | 5500 |
23+
-- | 4 | D | m | 500 |
24+
25+
26+
UPDATE salary
27+
SET
28+
sex = CASE sex
29+
WHEN 'm' THEN 'f'
30+
ELSE 'm'
31+
END;

0 commit comments

Comments
 (0)