Write a query for this problem?

Given a table Employee in which we have DeptId for each employee. Write a single SQL query to move the employees from DeptID 1 to 2 and move employees from DeptId 2 to 1.

Employee
Id Name DeptId
1 John 1
2 George 2
3 Jane 1
4 Smith 2

Answer:
We can use CASE statement here.

UPDATE Employee SET DeptId =
CASE DeptId
WHEN ‘1’ THEN ‘2’
WHEN ‘2’ THEN ‘1’
ELSE DeptId END;

Read the full book at www.amazon.com
Posted in SQL, SQL Interview Questions

Leave a Reply

Your email address will not be published. Required fields are marked *

*