Consider the below table: tbl_EmployeeEdu
NSlNo Empid NYEARPASS SQUALIFICATION MarksGot SGradeObt
1 1 2010 MCA 90 NULL
3 3 2006 MCA 60 NULL
4 4 2007 ME 90 NULL
6 6 2010 MTECH 50 NULL
7 7 2011 BCA 90 NULL
8 8 2009 BCA 50 NULL
9 9 2006 MTECH 60 NULL
11 11 2009 BBA 40 NULL
Write a single update query that updates "sGradeObt" column using "MarksgGot" colum with the following condition.
Marks >= 80 -- Merit ; Marks >=60 and < 80 --- firstclass ;Marks >=50 and <60 ---Second class ;Marks < 50 failure.

 Posted by Nagasundar_Tn on 11/27/2012 | Category: Sql Server Interview questions | Views: 2435 | Points: 40
Answer:

	  update tbl_EmployeeEdu set SGradeObt = case WHEN MarksGot >= 80  THEN 'Merit'

WHEN MarksGot >= 60 AND MarksGot < 80 THEN 'First Class'
WHEN MarksGot >= 50 AND MarksGot < 60 THEN 'Second Class'
WHEN MarksGot < 50 THEN 'Failure' END


In the above query I am using update statement with case when statements. Since I need to update all the column in table I didn't use where condition. Using Case When syntax of SQL Server I am assiging the conditions. When MarksGot >= 80 then I am setting the text "Merit" to sGradeObt column. Similarly for First class, Second class and failure conditions


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response