Description

Write a SQL UPDATE to swap all 'f' and 'm' values in the sex column. Then SELECT all rows.

Table:salary
ColumnType
idINT
nameTEXT
sexTEXT
salaryINT

Examples

Input:CREATE TABLE salary (id INT, name TEXT, sex TEXT, salary INT); INSERT INTO salary VALUES (1, 'Alice', 'f', 1000), (2, 'Bob', 'm', 2000);
Output:1|Alice|m|1000 2|Bob|f|2000
Explanation:

Alice's sex changed from 'f' to 'm', and Bob's sex changed from 'm' to 'f'. The UPDATE swaps all values.

Input:CREATE TABLE salary (id INT, name TEXT, sex TEXT, salary INT); INSERT INTO salary VALUES (1, 'Carol', 'f', 3500), (2, 'David', 'm', 4200), (3, 'Emma', 'f', 2800), (4, 'Frank', 'm', 5100);
Output:1|Carol|m|3500 2|David|f|4200 3|Emma|m|2800 4|Frank|f|5100
Explanation:

Carol and Emma had 'f' which changes to 'm'. David and Frank had 'm' which changes to 'f'. The UPDATE statement swaps all gender values simultaneously using a CASE expression, regardless of how many rows exist.

Input:CREATE TABLE salary (id INT, name TEXT, sex TEXT, salary INT); INSERT INTO salary VALUES (1, 'Grace', 'f', 7500);
Output:1|Grace|m|7500
Explanation:

Grace is the only employee with sex='f', which gets swapped to 'm'. This demonstrates the swap operation works correctly even with a single row, and when all rows have the same initial gender value.

Constraints

  • Use UPDATE with CASE

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!