Watch 10 video solutions for Swap Salary, a easy level problem involving Database. This walkthrough by Everyday Data Science has 13,876 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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'.
Problem Overview: The Swap Salary problem gives you a table where the sex column contains values 'm' and 'f'. The task is to swap these values for every row without using a temporary table. After the update, every 'm' becomes 'f' and every 'f' becomes 'm'.
Approach 1: Single UPDATE with CASE Statement (O(n) time, O(1) space)
The most common solution uses a single UPDATE query combined with a CASE expression. The query scans each row and conditionally replaces the value of sex. If the value is 'm', it updates it to 'f'; otherwise it updates it to 'm'. The key insight is that SQL evaluates the CASE expression for each row during the update, so both swaps happen in one pass through the table. This approach is clean, readable, and works in any standard SQL environment.
Approach 2: Single UPDATE with In-place Logic (O(n) time, O(1) space)
This method uses character arithmetic to compute the swapped value directly. Since the ASCII codes of 'm' and 'f' are known, you can derive the opposite value using an expression like CHAR(ASCII('m') + ASCII('f') - ASCII(sex)). The formula effectively flips the value between the two characters without explicit condition checks. The database still scans the table once and updates each row in place. This technique is compact but less readable, so it’s typically used as a clever trick rather than production-style SQL.
Approach 3: Conditional Mapping with CASE Expression (O(n) time, O(1) space)
A more explicit variant of the first solution maps values using a CASE block that clearly lists both transformations. The statement checks the existing value and assigns the opposite value accordingly. This version prioritizes clarity and maintainability, which is why it often appears in database interview problems. The database engine performs a full table scan and updates the column using the evaluated expression.
Recommended for interviews: The single UPDATE query with a CASE expression is what interviewers expect. It demonstrates solid understanding of SQL conditional logic and avoids unnecessary temporary tables. The arithmetic trick shows creativity, but the CASE solution is clearer and aligns with how production database updates are typically written.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Single UPDATE with CASE Statement | O(n) | O(1) | Best general solution. Clear, readable, and widely accepted in SQL interviews. |
| In-place ASCII Logic | O(n) | O(1) | Useful as a clever trick when demonstrating SQL expression manipulation. |
| Explicit CASE Mapping | O(n) | O(1) | Preferred when clarity and maintainability of the SQL query are priorities. |