Interview : update table values using select statement

Interviewer asked me one question, which seems very easy, but I couldn't figure out, how to solve this

Name  | Gender
--------------
A     | F
B     | M
C     | F
D     | F
E     | M

From the above data, gender was wrongly entered, which means in place of F it should be M and in place of M it should F. How to update whole table with a single line sql query (don't use pl/sql block). Since, if I will update gender column one by one, then possible error would be all rows values of gender column becomes either F or M.

Final output should be

Name  | Gender
--------------
A     | M
B     | F
C     | M
D     | M
E     | F

Answers


Try this..

Update TableName Set Gender=Case when Gender='M' Then 'F' Else 'M' end

On OP request..update using Select...

Update TableName T Set Gender=(
Select Gender from TableName B where  T.Gender!=B.Gender and rownum=1);

SQL FIDDLE DEMO


Need Your Help

Checkbox in Django - What is best practice?

django

I am aware that on djangoproject.com there is a explanation that you should use NullBooleanField for a checkbox in Django, however, I am wondering if it would be better to use something like this?

EditText inside AlertDialog always null

android alertdialog android-edittext

I've search over the threads but so far I have not found what I'm looking for.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.