For the last few weeks, I have been doing Friday Puzzles and I am really loving it. Yesterday I received a very interesting question by Navneet Chaurasia on Facebook Page. He was asked this question in one of the interview questions for job. Please read the original thread for a complete idea of the conversation. I am presenting the same question here.
Let us assume there is a single column in the table called Gender. The challenge is to write a single update statement which will flip or swap the value in the column. For example if the value in the gender column is ‘male’ swap it with ‘female’ and if the value is ‘female’ swap it with ‘male’.
Here is the quick setup script for the puzzle.
CREATE TABLE SimpleTable (ID INT, Gender VARCHAR(10))
INSERT INTO SimpleTable (ID, Gender)
SELECT 1, 'female'
SELECT 2, 'male'
SELECT 3, 'male'
The above query will return following result set.
The puzzle was to write a single update column which will generate following result set.
There are multiple answers to this simple puzzle. Let me show you three different ways. I am assuming that the column will have either value ‘male’ or ‘female’ only.
Method 1: Using CASE Statement
I believe this is going to be the most popular solution as we are all familiar with CASE Statement.
SET Gender = CASE Gender WHEN 'male' THEN 'female' ELSE 'male' END
Method 2: Using REPLACE Function
I totally understand it is the not cleanest solution but it will for sure work in giving situation.
SET Gender = REPLACE(('fe'+Gender),'fefe','')
Method 3: Using IIF in SQL Server 2012
If you are using SQL Server 2012 you can use IIF and get the same effect as CASE statement.
SET Gender = IIF(Gender = 'male', 'female', 'male')
You can read my article series on SQL Server 2012 various functions over here.
- SQL SERVER – Denali – Logical Function – IIF() – A Quick Introduction
- SQL SERVER – Detecting Leap Year in T-SQL using SQL Server 2012 – IIF, EOMONTH and CONCAT Function
Let us clean up.
DROP TABLE SimpleTable
Question to you:
I came up with three simple tricks where there is a single UPDATE statement which swaps the values in the column. Do you know any other simple trick? If yes, please post here in the comments. I will pick two random winners from all the valid answers. Winners will get 1) Print Copy of SQL Server Interview Questions and Answers 2) Free Learning Code for Online Video Courses
I will announce the winners on coming Monday.
Reference: Pinal Dave (https://blog.sqlauthority.com)
being practical, some times gender column will be blank… may be data unavailable…. i prefer first approach because it is reliable… by the way great discussion thread
I don’t have a writable schema at hand, but this should do the trick.
We define a CTE with the replacement values, then join on that to find the replacement value for each value.
OriginalValues AS (
SELECT 1 AS ID, ‘female’ AS Gender
SELECT 2, ‘male’
SELECT 3, ‘male’
Replacements AS (
SELECT ‘male’ AS FromValue, ‘female’ AS ToValue
SELECT ‘female’, ‘male’
og.Gender AS OriginalValue,
rp.ToValue AS ReplacementValue
FROM OriginalValues og
JOIN Replacements rp ON (og.Gender = rp.FromValue)