How to get Row Index in MySQL?

 Posted by vishalneeraj-24503 on 11/24/2013 | Category: Sql Server Interview questions | Views: 2551 | Points: 40

select employee_id,employee_name,@index := @index + 1 as 'sr. no.' from employee_master,(select @index := 0) Emp order by employee_name;

@index > Is an User Defined variable.
:= -> Is an Assignment operator.
@index := @index + 1 -> It sets the variable to the Next Incremental Value for each rows.

Setting @index in the sub-selection or sub-query makes it simple to put it into 1 query.The incrementing number can be adjusted.

After the "Emp" which stands for derived and it is mandatory to have an alias,we can use order by or group by clause.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response