Swap M and F Values in a Gender Column With a Single UPDATE

Quick Overview

Write one SQL UPDATE that turns every M into F and every F into M in a gender column, then return the table ordered by id. It tests why two separate updates fail, which row values a SET expression reads, and how a CASE expression treats NULL and unexpected values.

Swap M and F Values in a Gender Column With a Single UPDATE

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

The `Students` table has one row per student: | Column | Type | Notes | |---|---|---| | `id` | INT | Primary key | | `name` | VARCHAR(100) | | | `gender` | CHAR(1) | `'M'` or `'F'` | Write a single `UPDATE` statement that swaps the `gender` column of `Students`: every `'M'` becomes `'F'` and every `'F'` becomes `'M'`. Then return all rows of `Students` ordered by `id` ascending. For example, starting from this table: ```text +----+---------+--------+ | id | name | gender | +----+---------+--------+ | 1 | Alice | F | | 2 | Bob | M | | 3 | Charlie | M | | 4 | Diana | F | +----+---------+--------+ ``` the final `SELECT` returns: ```text +----+---------+--------+ | id | name | gender | +----+---------+--------+ | 1 | Alice | M | | 2 | Bob | F | | 3 | Charlie | F | | 4 | Diana | M | +----+---------+--------+ ``` Alice and Diana change from F to M, and Bob and Charlie change from M to F. ```hint Order of operations Picture the table after you have updated the M rows but before you touch the F rows. ``` ### Constraints & Assumptions - `Students` has between 1 and 10,000 rows. - `gender` only contains `'M'` or `'F'`. - `id` is the primary key, so ordering by `id` gives one fixed row order. ### What a Strong Answer Covers - Why two separate `UPDATE` statements, one per value, leave the table wrong. - A single statement that maps each row's current value to the opposite value. - Which version of the row the `SET` expression reads, and why that makes the one-statement swap safe. - What the statement does to a value other than `'M'` or `'F'`, including NULL. - The final `SELECT` with an explicit `ORDER BY id`. ### Follow-up Questions - How would you swap the values of two columns in the same row, and does `SET a = b, b = a` behave the same way in PostgreSQL and MySQL? - Can you write the swap without a `CASE` expression? - The table has 500 million rows and serves live reads. How would you run the swap without one huge transaction? - A `CHECK` constraint limits `gender` to `'M'` and `'F'`. Which multi-step workarounds stop working?

Overview: Write one SQL UPDATE that turns every M into F and every F into M in a gender column, then return the table ordered by id. It tests why two separate updates fail, which row values a SET expression reads, and how a CASE expression treats NULL and unexpected values.

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

The Students table has one row per student:

ColumnTypeNotes
idINTPrimary key
nameVARCHAR(100)
genderCHAR(1)'M' or 'F'

Write a single UPDATE statement that swaps the gender column of Students: every 'M' becomes 'F' and every 'F' becomes 'M'. Then return all rows of Students ordered by id ascending.

For example, starting from this table:

+----+---------+--------+
| id | name    | gender |
+----+---------+--------+
| 1  | Alice   | F      |
| 2  | Bob     | M      |
| 3  | Charlie | M      |
| 4  | Diana   | F      |
+----+---------+--------+

the final SELECT returns:

+----+---------+--------+
| id | name    | gender |
+----+---------+--------+
| 1  | Alice   | M      |
| 2  | Bob     | F      |
| 3  | Charlie | F      |
| 4  | Diana   | M      |
+----+---------+--------+

Alice and Diana change from F to M, and Bob and Charlie change from M to F.

Constraints & Assumptions

  • Students has between 1 and 10,000 rows.
  • gender only contains 'M' or 'F' .
  • id is the primary key, so ordering by id gives one fixed row order.

What a Strong Answer Covers Guidance

  • Why two separate UPDATE statements, one per value, leave the table wrong.
  • A single statement that maps each row's current value to the opposite value.
  • Which version of the row the SET expression reads, and why that makes the one-statement swap safe.
  • What the statement does to a value other than 'M' or 'F' , including NULL.
  • The final SELECT with an explicit ORDER BY id .

Follow-up Questions Guidance

  • How would you swap the values of two columns in the same row, and does SET a = b, b = a behave the same way in PostgreSQL and MySQL?
  • Can you write the swap without a CASE expression?
  • The table has 500 million rows and serves live reads. How would you run the swap without one huge transaction?
  • A CHECK constraint limits gender to 'M' and 'F' . Which multi-step workarounds stop working?