100

I need to send a SQL query to a database that tells me how many rows there are in a table. I could get all the rows in the table with a SELECT and then count them, but I don't like to do it this way. Is there some other way to ask the number of the rows in a table to the SQL server?

CyberDude
  • 8,541
  • 5
  • 29
  • 47
Celeste Capece
  • 1,306
  • 2
  • 11
  • 20
  • Does this answer your question? [Fastest way to count exact number of rows in a very large table?](https://stackoverflow.com/questions/6069237/fastest-way-to-count-exact-number-of-rows-in-a-very-large-table) – Liam Jul 10 '20 at 10:31

7 Answers7

187

Yes, SELECT COUNT(*) FROM TableName

CyberDude
  • 8,541
  • 5
  • 29
  • 47
  • 2
    wouldn't it be more computationally intensive and more inefficient to select EVERY column instead of a single column??? – oldboy Mar 30 '18 at 22:15
  • 3
    No, the optimizer is smart enough to recognize this situation and generate the proper execution plan. Look it up, there are plenty of questions on the subject on SO. – CyberDude Apr 15 '18 at 08:56
  • 3
    i got such a huge list of things to do and thus no time to read about it right now unfortunately. this will go on the list of things to do, however – oldboy Apr 17 '18 at 17:50
  • 9
    this is not the best answer as it places unnecesary locks and pressure on production systems especially for large tables. Use sys.partitions instead to return the exact number of rows from table metadata directly! – Tarek Salha Jun 15 '20 at 03:52
  • 1
    This was failing for me because I, out of habit with aggregation functions, threw in a "GROUP BY" statement - don't do that! – PoloHoleSet Sep 22 '20 at 14:29
57
select sum([rows])
from sys.partitions
where object_id=object_id('tablename')
 and index_id in (0,1)

is very fast but very rarely inaccurate.

benjamin moskovits
  • 5,261
  • 1
  • 12
  • 22
10

Here is the Query

select count(*) from tablename

or

select count(rownum) from studennt
Liam
  • 27,717
  • 28
  • 128
  • 190
Lova Chittumuri
  • 2,994
  • 1
  • 30
  • 33
7

Why don't you just right click on the table and then properties -> Storage and it would tell you the row count. You can use the below for row count in a view. Use your table's name in the WHERE clause.

SELECT SUM (row_count) 
FROM sys.dm_db_partition_stats 
WHERE object_id=OBJECT_ID('TableName')    
AND (index_id=0 or index_id=1)`
srm
  • 3,062
  • 16
  • 30
Dustin
  • 81
  • 1
  • 1
  • What is the difference between "sys.dm_db_partition_stats" and "sys.partitions" used in benjamin's answer? – srm Dec 15 '21 at 17:37
6

Use This Query :

Select
    S.name + '.' + T.name As TableName ,
    SUM( P.rows ) As RowCont 

From sys.tables As T
    Inner Join sys.partitions As P On ( P.OBJECT_ID = T.OBJECT_ID )
    Inner Join sys.schemas As S On ( T.schema_id = S.schema_id )
Where
    ( T.is_ms_shipped = 0 )
    AND 
    ( P.index_id IN (1,0) )
    And
    ( T.type = 'U' )

Group By S.name , T.name 

Order By SUM( P.rows ) Desc
Ardalan Shahgholi
  • 11,967
  • 21
  • 108
  • 144
0

The index statistics likely need to be current, but this will return the number of rows for all tables that are not MS_SHIPPED.

select o.name, i.rowcnt 
from sys.objects o join sys.sysindexes i 
on o.object_id = i.id
where o.is_ms_shipped = 0
and i.rowcnt > 0
order by o.name
0

try this for all tables also can apply conditions:

SELECT SCHEMA_NAME([schema_id])[Schema],t.[name][ObjectName],i.[rowcnt][RowsCount] FROM sys.tables AS t
INNER JOIN sysobjects AS o ON o.[id]=t.[object_id]
INNER JOIN sysindexes AS i ON i.[id]=o.[id]
WHERE SCHEMA_NAME([schema_id])='dbo'
AND i.[indid]<2  
AND OBJECTPROPERTY(o.[id], 'IsMSShipped')=0
ORDER BY i.[rowcnt]
Haseeb
  • 746
  • 7
  • 22