Swap the m and f values in a column with a single UPDATE statement

Quick Overview

Practice writing one SQL UPDATE statement that swaps the m and f values of a column across every row of a salary table, then returns the table ordered by id. It tests conditional updates with CASE and how SET expressions read each row's old values.

Swap the m and f values in a column with a single UPDATE statement

Company: Thoughtworks

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

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. ```hint Why two updates fail Changing every `'m'` to `'f'` and then every `'f'` to `'m'` leaves the whole table as `'m'`. Look for a way to choose each row's new value from its current value inside one `SET` clause. ``` ### 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.

Overview: Practice writing one SQL UPDATE statement that swaps the m and f values of a column across every row of a salary table, then returns the table ordered by id. It tests conditional updates with CASE and how SET expressions read each row's old values.

Thoughtworks logo
Thoughtworks
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

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

ColumnTypeMeaning
idINTEmployee id
nameVARCHAR(100)Employee name
sexCHAR(1)'m' or 'f'
salaryINTEmployee 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:

idnamesexsalary
1Am2500
2Bf1500
3Cm5500
4Df500

Expected result:

idnamesexsalary
1Af2500
2Bm1500
3Cf5500
4Dm500

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.