Update the Status

      

Write an SQL query to interchange the values of the Status column (i.e, change all 'Employed' to 'Unemployed' and vice-versa) using an update statement.



Input :-

Data table :-
ID is the Primary Key column for the Data table.

ID

Name

Status

Department

1

Harish

Employed

IT

2

Yogesh

Unemployed

HR

3

Manju

Employed

Manager

4

Shekar

Unemployed

Sales


Output :-

ID

Name

Status

Department

1

Harish

Unemployed

IT

2

Yogesh

Employed

HR

3

Manju

Unemployed

Manager

4

Shekar

Employed

Sales



Query :-
update Data
set Status=(
    case when Status='Employed' then 'Unemployed'
    else 'Employed'
    End
)

Explanation : - 

The given query is an SQL update statement that updates the ‘Status’ column of a table named "Data". The update is performed based on a conditional expression using a case statement.


The query starts by specifying the column to be updated, which is ‘Status’. It then uses a case statement to define the logic for the update.


The case statement evaluates the current value of the ‘Status’ column for each row. If the current value is 'Employed', it changes it to 'Unemployed'. Conversely, if the current value is anything other than 'Employed', it changes it to 'Employed'.


In other words, the query effectively toggles/interchanges the ‘Status’ values from 'Employed' to 'Unemployed' and vice-versa for all rows in the "Data" table.






No comments

darkmode