How can I get the count in SQL?

How can I get the rows along with row count in SQL?

  • I have written the following query: select *, count(pk_id) as row_count from employee group by pk_id But I am not getting 1 as the row_count value for every column. How can I get the total number of rows returned as the result of the query? Can someone please help? And one more thing is I don't want to write any subquery :(

  • Answer:

    Have you considered either just counting the rows as you receive them in whatever's consuming this result set, or just using http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows? Is there some reason you need the rowcount to appear as a column in the result set?

Deviprasad Das at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

Try with: select *, count(*) as row_count from employee group by pk_id

Tudor Constantin

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.