The Salary table holds one row per employee, and its sex column stores a single character, either 'm' or 'f'. Write a single UPDATE statement that swaps all 'f' and 'm' values in sex: every 'm' becomes 'f' and every 'f' becomes 'm'. The resulting table is returned ordered by id ascending.
Input Table(s)
Salary
| Column | Type | Meaning |
|---|
| id | INT | Employee id |
| name | VARCHAR(100) | Employee name |
| sex | CHAR(1) | 'm' or 'f' |
| salary | INT | Employee salary |
Primary key: id.
Output Contract
-
The statement changes
Salary
in place. Only the
sex
column changes;
id
,
name
and
salary
keep their values.
-
The result is every row of
Salary
after the swap, with columns
id
,
name
,
sex
,
salary
in that order.
-
Rows are ordered by
id
ascending.
id
is the primary key, so the order is unique.
-
No rounding applies.
Example
Salary before the update:
| id | name | sex | salary |
|---|
| 1 | A | m | 2500 |
| 2 | B | f | 1500 |
| 3 | C | m | 5500 |
| 4 | D | f | 500 |
Expected result:
| id | name | sex | salary |
|---|
| 1 | A | f | 2500 |
| 2 | B | m | 1500 |
| 3 | C | f | 5500 |
| 4 | D | m | 500 |
Rows 1 and 3 change from 'm' to 'f', rows 2 and 4 change from 'f' to 'm', and nothing else moves.
Constraints and Clarifications
-
Salary
has at most 100 rows.
-
sex
is always
'm'
or
'f'
. There are no NULLs and no other values.
-
Use one
UPDATE
statement. Do not
SELECT
into an intermediate table and do not use a temporary table.
-
Every row is updated, so the statement needs no
WHERE
filter.
-
Names and salaries can repeat across rows; that does not affect the swap.