Amazon SQL interview questions – Leetcode 627 – Swap Salary

Problem Description –

Write an SQL query 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 temp table(s).

Note that you must write a single update statement, DO NOT write any select statement for this problem.

The query result format is in the following example:

Difficulty Level – Easy

Solution –

UPDATE Salary
SET sex = if(sex = 'm','f','m')

Here, we are asked to not write more than one update statement and also we can not use any select statement. This can be done using simply the If statement. If the sex is ‘m’ then the value will be ‘f’ else it will be ‘m’.

Rating: 1 out of 5.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s