MySQL 0627.sql
UPDATE Salary
SET sex = IF(sex = 'f', 'm', 'f')
Difficulty
and has an acceptance rate of about 85% (600k+ submissions).
Typical patterns to solve this problem:
- Database
AI Disclaimer
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.
See this dedicated page for more details.
Code Walkthrough
Overview
The statement updates every row in the Salary table, toggling the value of the sex column between the two literal values 'f' and 'm'. It relies on the conditional function IF to decide which literal to write based on the current content of the column. By issuing a single UPDATE command, the operation is performed in one pass over the table, which keeps the execution straightforward and efficient.
Key Steps
The first logical step is to locate each row that belongs to the target table. The UPDATE Salary clause designates the table that will be modified.
UPDATE Salary
The second step evaluates the current value of the sex column for each row. The IF function checks whether the column equals 'f'. If the condition is true, the function returns 'm'; otherwise it returns 'f'. This expression is placed on the right-hand side of the assignment.
SET sex = IF(sex = 'f', 'm', 'f')
The final step writes the result of the conditional expression back into the sex column, completing the toggle for that row. Because the SET clause is part of the same UPDATE statement, the write occurs immediately after the evaluation for each row.
Edge Cases Handled
When the column already contains 'f', the condition sex = 'f' evaluates to true, and the IF function produces 'm'. Conversely, any value that is not exactly 'f'-including 'm', other unexpected strings, or even NULL-causes the condition to evaluate to false, and the function yields 'f'. This behavior means that rows with unexpected or missing values are coerced into 'f', ensuring that after the statement runs every row will contain either 'm' or 'f'. The implicit handling of NULL values avoids runtime errors and guarantees a deterministic result for every possible original content.
Performance Optimizations
The use of a single UPDATE statement eliminates the overhead of issuing separate commands for each possible value of sex. By embedding the conditional logic directly in the SET clause, the database engine can compute the new value on the fly while scanning the table, avoiding the need for temporary tables or additional joins. If an index exists on the sex column, the optimizer may still choose a full table scan because every row is a candidate for modification, but the lack of auxiliary structures keeps I/O to a minimum. The statement also benefits from the database’s internal batch update mechanisms, which can group row modifications into larger write operations, reducing transaction log activity and improving throughput.
ELI5 - Simplified Explanation
Imagine a giant book where every page is a person who works at a busy company. On each page, there is a special box for their gender. The code is like a little robot helper walking through every single page of that book to look at the box for gender. If the robot sees the letter f, it reaches out with its eraser and a pencil to change that f into an m. If the robot sees an m instead, it flips the switch and changes it into an f. It does this for every single person until the whole book is updated.
Code Quality
Complexity Analysis
As a rule of thumb, the maximum input size for one second execution time is
- factorial: up to 10 elements
- exponential: up to 20 elements
- polynomial: a few thousand elements
- loglinear: up to 100,000 elements
- linear: a few million elements
- logarithmic: a billion elements
- constant: practically unlimited
Other Solutions
Obviously, I did that after I solved the problem myself.
In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.
And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.
(click on a column header to sort the table)
| author | ||||||
|---|---|---|---|---|---|---|
| author | solutions | |||||
| www | AlgoMonster | C++ | Java | Python | TS | |
| WalkCCC | SQL | |||||
| leetcode.ca | SQL | |||||
| LeetcodeHelp | C++ | Java | Python | |||
| Progiez | C++ | Java | Python | |||
| github | everthis | SQL | ||||
| fsq | SQL | |||||
| keineahnung2345 | SQL | |||||
| Krazune | SQL | |||||
| lzl124631x | SQL | |||||
| suxrobGM | SQL | |||||
| AlgoMonster | C++, Python, Java, TypeScript | |||||
| WalkCCC | SQL | |||||
| leetcode.ca | SQL | |||||
| LeetcodeHelp | C++, Java, Python | |||||
| Progiez | C++, Java, Python | |||||
| everthis | SQL | |||||
| fsq | SQL | |||||
| keineahnung2345 | SQL | |||||
| Krazune | SQL | |||||
| lzl124631x | SQL | |||||
| suxrobGM | SQL |
Videos
Solved On The Same Day
- problem 175: Combine Two Tables
- problem 181: Employees Earning More Than Their Managers
- problem 182: Duplicate Emails
- problem 183: Customers Who Never Order
- problem 193: Valid Phone Numbers
- problem 196: Delete Duplicate Emails
- problem 197: Rising Temperature
- problem 203: Remove Linked List Elements
- problem 511: Game Play Analysis I
- problem 577: Employee Bonus
- problem 584: Find Customer Referee
- problem 586: Customer Placing the Largest Number of Orders
- problem 595: Big Countries
- problem 596: Classes With at Least 5 Students
- problem 607: Sales Person
- problem 610: Triangle Judgement
- problem 619: Biggest Single Number
- problem 620: Not Boring Movies
- problem 1050: Actors and Directors Who Cooperated At Least Three Times
- problem 1068: Product Sales Analysis I
- problem 1075: Project Employees I
- problem 1084: Sales Analysis III
- problem 1141: User Activity for the Past 30 Days I
- problem 1148: Article Views I
- problem 1179: Reformat Department Table
- problem 176: Second Highest Salary
- problem 177: Nth Highest Salary
- problem 178: Rank Scores
- problem 180: Consecutive Numbers
- problem 184: Department Highest Salary
- problem 192: Word Frequency
- problem 194: Transpose File
- problem 550: Game Play Analysis IV
- problem 570: Managers with at Least 5 Direct Reports
- problem 585: Investments in 2016
- problem 602: Friend Requests II: Who Has the Most Friends
- problem 608: Tree Node
- problem 626: Exchange Seats
- problem 725: Split Linked List in Parts
- problem 1045: Customers Who Bought All Products
- problem 1070: Product Sales Analysis III
- problem 1164: Product Price at a Given Date
- problem 1174: Immediate Food Delivery II
- problem 1193: Monthly Transactions I
- problem 1204: Last Person to Fit in the Bus
- problem 1321: Restaurant Growth
- problem 1393: Capital Gain/Loss
- problem 174: Dungeon Game
- problem 185: Department Top Three Salaries
- problem 218: The Skyline Problem
- problem 262: Trips and Users
- problem 601: Human Traffic of Stadium
Extra
- Tech Stack - my development environment and tools
- AI - how AI helped me create this website (NOT solving the problems)
- Progress - progression over time
- Quality - code quality