Check our online training feedbacks Here

08 August 2011

Sql Practice - 1


Hi all,
One of my friend have been asked in of the interview following question,

Consider you have at table Say TblEmployee with a column say EmployeeName
which contain Records as A,B,A,A,B,C,B,C,A

your task is to write a query which will change the EmployeeName A to B and B to A.
Means after the query been executed Records will be as B,A,B,B,A,C,A,C,B
.
.
.
.
.
Think what will be the query..and try to post some answers..after two day i will post the answer

4 comments :

  1. update TableTest
    set EmpName = 'AA'
    where EmpName = 'A'

    update TableTest
    set EmpName = 'A'
    where EmpName = 'B'

    update TableTest
    set EmpName = 'B'
    where EmpName = 'AA'

    **********very simple way************

    ReplyDelete
  2. Declare @Emp Varchar(50)

    update TableTest
    set EmpName = CASE EmpName
    WHEN 'A' THEN 'B'
    WHEN 'B' THEN 'A'
    ELSE EmpName
    END
    ***** second way ************

    ReplyDelete
  3. UPDATE `employee` SET EMPLOYEENAME = (
    CASE
    WHEN
    EMPLOYEENAME = 'A'
    THEN
    'B'
    WHEN
    EMPLOYEENAME = 'B'
    THEN
    'A'
    ELSE
    'C'
    END
    )

    ReplyDelete
  4. Excellent Sai..
    second query given by u is awesome one.
    i.e,
    update TableTest
    set EmpName = CASE EmpName
    WHEN 'A' THEN 'B'
    WHEN 'B' THEN 'A'
    ELSE EmpName
    END

    Even your First query works,but performance wise thats poor.

    ReplyDelete