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 |