devxlogo

Displaying the Row Number in a SELECT Query

Question:
How can I display the row number in the results of a SELECT query? For example:

 Select empname from employees where city = "toronto"

I would like to show the row number as follows:

   1 Fred  2 Bill  3 Jeff  4 June

Answer:
You are probably better off handling such formatting issues in your front-end. In T-SQL, you would first have to build a table that mimics what your result set looks like with the addition of an identity column:

 create table #result(   id int identity(1,1),   empname char(20))

Then insert your result set into the temporary table. Lastly, select out of the temporary result set.

I think you can see why a front-end should be able to handle this more easily.

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  How Engineering Leaders Spot Weak Proposals

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.