Problem 627

Abstract / Summary

Change the sex value in each employee row from m to f and from f to m.

Note

The full problem description, including helpful examples, can be found on LeetCode.

MySQL 0627.sql

UPDATE Salary
   SET sex = IF(sex = 'f', 'm', 'f')
2 lines | 53 bytes | submitted September 6, 2023 | GitHub  

Difficulty

This problem is ranked as
easy medium hard

and has an acceptance rate of about 85% (600k+ submissions).

Typical patterns to solve this problem:
  • Database
I solved 663 easy, 1250 medium and 309 hard problems

AI Disclaimer

Every single line of code was written by me, 100% from scratch, between August 2023 and August 2024.
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.
LeetCode profile | GitHub repo

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.

written by openai gpt

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.

written by google gemini

Code Quality

Several LLMs ranked my code quality on a scale from 1 (very poor) to 10 (outstanding):
n = 27 | median = 7 | μ = 6.15 | σ = 2.12

Complexity Analysis

Time: linear
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
generated automatically

Other Solutions

I wrote a basic crawler to find other solutions to that problem.
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
18 links | 11 authors | 5 languages

Videos

2 videos   did my crawler come up with invalid videos ? drop me a line.

Solved On The Same Day

25 easy 22 medium 5 hard
53 solutions   September 6, 2023

Extra

There are a few extra pages giving you some insights into my setup:
  • 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