MS SQL
Question\n\n627. Swap Salary\nEasy\n\nSQL Schema\nTable: Salary\n\n+-------------+----------+\n| Column Name | Type     |\n+-------------+----------+\n| id     
#### Question\n```\n627. Swap Salary\nEasy\n\nSQL Schema\nTable: Salary\n\n+-------------+----------+\n| Column Name | Type     |\n+-------------+----------+\n| id          | int      |\n| name        | varchar  |\n| sex         | ENUM     |\n| salary      | int      |\n+-------------+----------+\nid is the primary key for this table.\nThe sex column is ENUM value of type (\'m\', \'f\').\nThe table contains information about an employee.\n \n\nWrite 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 temp table(s).\n\nNote that you must write a single update statement, DO NOT write any select statement for this problem.\n\nThe query result format is in the following example:\n\n \n\nSalary table:\n+----+------+-----+--------+\n| id | name | sex | salary |\n+----+------+-----+--------+\n| 1  | A    | m   | 2500   |\n| 2  | B    | f   | 1500   |\n| 3  | C    | m   | 5500   |\n| 4  | D    | f   | 500    |\n+----+------+-----+--------+\n\nResult table:\n+----+------+-----+--------+\n| id | name | sex | salary |\n+----+------+-----+--------+\n| 1  | A    | f   | 2500   |\n| 2  | B    | m   | 1500   |\n| 3  | C    | f   | 5500   |\n| 4  | D    | m   | 500    |\n+----+------+-----+--------+\n(1, A) and (3, C) were changed from \'m\' to \'f\'.\n(2, B) and (4, D) were changed from \'f\' to \'m\'.\n\n```\n\n#### Answer\n\n##### Approach 1\n```SQL\nupdate salary set sex = IF (sex = "m", "f", "m");\n```\n\n##### Approach 2\n```SQL\nUPDATE salary\nSET\n    sex = CASE sex\n        WHEN \'m\' THEN \'f\'\n        ELSE \'m\'\n    END;\n```
['MS SQL Server']