627. Swap Salary

Given a table salary, such as the one below, that has m=male and f=female values. 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.

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

Example:

| id | name | sex | salary |
|----|------|-----|--------|
| 1 | A | m | 2500 |
| 2 | B | f | 1500 |
| 3 | C | m | 5500 |
| 4 | D | f | 500 |

After running your update statement, the above salary table should have the following rows:

| id | name | sex | salary |
|----|------|-----|--------|
| 1 | A | f | 2500 |
| 2 | B | m | 1500 |
| 3 | C | f | 5500 |
| 4 | D | m | 500 |

Solution:

update salary set sex= if (sex = 'f', 'm', 'f');

Link

--

--

--

My homepage to record my thought processes for solving SQL and Algorithm questions

Recommended from Medium

the optimism of modernists

The Neutral mind of Software Engineer.

Community Management: the experience of Natalie Pistunovich

Why learning programming (at least the basic level) is needed and getting common now?

Webscraping a Newspaper and Data Cleaning Using Python

Python Is Fun: Tricks of the Trade

Docker and linux distributions

How to Setup Slack Integration for Google Cloud Build using Cloud Functions

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Isabelle

Isabelle

In love with telling stories with data

More from Medium

Leetcode SQL

Leetcode SQL 10 Days Study Plan(Day2) With Oracle SQL(Delete, Decode, Case When, Update)

SQL Server Join Algorithms — and how to benefit from knowing them

Normalization in SQL