Skip to content
Advertisement

Query result with added column number auto incremented dynamically

How do I add extra column to get row number added to my query result and increment dynamically with query resulted rows.

I have tried using How to generate serial number in a query?

But the result is like below

Sr.No is getting scuffled.

Advertisement

Answer

If you want the sequence number to be the same as the result set, use the same order by keys. In fact, you can order by the column itself!

Note that I added table aliases and qualified the column references. This is a best practice so queries are understandable, maintainable, and do what you really intend.

Also, if e_id is the primary key in employee, then you can just use group by e.e_id.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement