Swap Sex of Employees solution and explanation
9 more videos on this problem
LeetCode Interview SQL Question with Detailed Explanation | Practice SQL | LeetCode 627Everyday Data Science · 7:24 · 15K views
LeetCode 627: Swap Salary [SQL]Frederik Müller · 4:23 · 5K views
[MySQL] Leetcode 627. Swap Salary [SQL #1]Eric Programming · 5:09 · 2K views
刷题 - LeetCode in MySQL 627. Swap Salary - Michelle小梦想家Michelle小梦想家 · 14:29 · 1K views
Swap Salary | Leetcode 627 | Week 4 | Qs 21 | SQLThe Big Data Show · 4:41 · 675 views
LeetCode in MySQL 627. Swap Salary - Michelle小梦想家 (English Version)Michelle小梦想家 · 6:32 · 303 views
627. Swap Salary Leetcode Solution | leetcode sql solutionsrealNameHidden · 3:03 · 234 views
LeetCode 627 - Swap Salary - Update Table Interview QuestionBig Tech Mindset · 1:25 · 169 views
627. Swap Salary | LeetCode | SQL | Solution | Interview ProblemData Alchemist · 2:46 · 114 views
LeetCode - 627. Swap Salary (SQL)Beyond The Model · 2:52 · 98 views
The problem
Table: Salary
+-------------+----------+
| Column Name | Type |
+-------------+----------+
| id | int |
| name | varchar |
| sex | ENUM |
| salary | int |
+-------------+----------+
id is the primary key (column with unique values) for this table.
The sex column is ENUM (category) value of type ('m', 'f').
The table contains information about an employee.
Write a solution 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 temporary tables.
Note that you must write a single update statement, do not write any select statement for this problem.
The result format is in the following example.
Example 1:
Input: Salary table: +----+------+-----+--------+ | id | name | sex | salary | +----+------+-----+--------+ | 1 | A | m | 2500 | | 2 | B | f | 1500 | | 3 | C | m | 5500 | | 4 | D | f | 500 | +----+------+-----+--------+ Output: +----+------+-----+--------+ | id | name | sex | salary | +----+------+-----+--------+ | 1 | A | f | 2500 | | 2 | B | m | 1500 | | 3 | C | f | 5500 | | 4 | D | m | 500 | +----+------+-----+--------+ Explanation: (1, A) and (3, C) were changed from 'm' to 'f'. (2, B) and (4, D) were changed from 'f' to 'm'.
Approach overview
Problem Overview: The Salary table has an id, name, sex ('m' or 'f'), and salary column. You must flip every row's sex value in place: 'm' becomes 'f' and 'f' becomes 'm'. No SELECT, no new table, and no rows can be deleted or added.
Approach 1: Two-Step Update with a Temporary Value (O(n) time, O(1) extra space)
Run three UPDATE statements. First, change every 'm' to a placeholder such as 'x'. Then change every 'f' to 'm'. Finally, change every 'x' to 'f'. The placeholder prevents the second update from re-matching rows you already flipped. Each statement scans the table once, so the total work is three passes over n rows. Use this when you want a pattern that works in any SQL dialect and makes the intermediate states easy to reason about.
Approach 2: Single UPDATE with CASE or IF (O(n) time, O(1) extra space)
Write one statement: UPDATE Salary SET sex = CASE sex WHEN 'm' THEN 'f' ELSE 'm' END;. MySQL evaluates the SET expression for each row using that row's original value, so the swap happens in a single pass without a temporary value. The MySQL-specific shorthand IF(sex = 'm', 'f', 'm') works the same way. This is the approach to use in production code and in interviews because it is one statement, one table scan, and no intermediate state.
Approach 3: Arithmetic on ASCII Codes (O(n) time, O(1) extra space)
Since 'm' and 'f' have fixed ASCII codes, you can compute the partner character directly: UPDATE Salary SET sex = CHAR(ASCII('m') + ASCII('f') - ASCII(sex));. This is clever but harder to read, and it breaks silently if the data contains any value other than 'm' or 'f'. Mention it only to show you know the trick.
Recommended for interviews: Interviewers expect the single UPDATE with CASE. The two-step version shows you understand that updates can overwrite values you still need to read. The single statement shows you know how MySQL evaluates SET expressions per row. Both are O(n) time, so the difference is clarity and correctness, not speed. This problem sits under database problems, and it is a good warm-up for SQL conditional logic.
Solution
Thinking
Every
'f'/'m'must flip, and the problem asks for a singleUPDATE.
SET sex = IF(sex='f','m','f')rewrites each row in place.
According to the problem requirements, we only need to use a single UPDATE statement to swap the sex of all employees. We can achieve this using conditional expressions in SQL.
# Write your MySQL query statement below
UPDATE Salary
SET sex = IF(sex = 'f', 'm', 'f');Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Two-step update with temporary value | O(n), three passes | O(1) extra | Portable SQL, or when a single expression is not possible |
| Single UPDATE with CASE or IF | O(n), one pass | O(1) extra | General case, interviews, production code |
| ASCII arithmetic | O(n), one pass | O(1) extra | Only when the value set is strictly 'm' and 'f' |
Frequently asked questions
Is Swap Sex of Employees easy or hard?
It is rated Easy, with an acceptance rate of about 85.1%. The logic is a single conditional swap. The main pitfall is writing two separate UPDATE statements that flip the same rows twice, which is why the single CASE statement is preferred.
How to solve Swap Sex of Employees in O(n)?
Use one UPDATE statement with a conditional expression: UPDATE Salary SET sex = IF(sex = 'm', 'f', 'm'). Every row is visited once, and each row is flipped based on its current value. Avoid multiple UPDATE statements that could re-flip rows you already changed.
Swap Sex of Employees Python/Java solution?
This is a SQL problem, so there is no Python or Java solution. Write the answer in MySQL: UPDATE Salary SET sex = CASE sex WHEN 'm' THEN 'f' ELSE 'm' END. The platform runs it against a sample Salary table to check the updated rows.
What is the best approach for Swap Sex of Employees?
The best approach is a single UPDATE statement with a CASE expression: UPDATE Salary SET sex = CASE sex WHEN 'm' THEN 'f' ELSE 'm' END. It runs in O(n) time with one table pass and O(1) extra space. MySQL evaluates each row's SET expression against the original values, so no temporary value is needed.
What data structure is used in Swap Sex of Employees?
No data structure is needed. The problem operates directly on a relational table, Salary, with columns id, name, sex, and salary. The only logic is a conditional expression inside the UPDATE statement, using CASE or IF.
What is the time complexity of Swap Sex of Employees?
The time complexity is O(n), where n is the number of rows in the Salary table. The single UPDATE with CASE scans the table once. The two-step approach with a temporary value scans it three times, but that is still O(n). Extra space is O(1) for all standard approaches.
Is Swap Sex of Employees asked at Google, Amazon, or Meta?
This problem is a LeetCode Easy database question and is not tied to a specific company in public reports. It appears in SQL practice sets used by many companies, so it is a good warm-up for SQL interviews at any firm that tests conditional UPDATE logic. Focus on writing clean single-statement updates.
