List tables by the number of rows in SQL Server database

This query returns list of tables in a database with their number of rows.

Confused about your Teradata database?

You don't have to be. There's an easy way to understand the data in your databases.

I want to understand

Query

select schema_name(tab.schema_id) + '.' + tab.name as [table], 
       sum(part.rows) as [rows]
   from sys.tables tab
        inner join sys.partitions part
            on tab.object_id = part.object_id
where part.index_id IN (1, 0) -- 0 - table without PK, 1 table with PK
group by schema_name(tab.schema_id) + '.' + tab.name
order by sum(part.rows) desc

Columns

  • table - table name with schema name
  • rows - number of rows in a table

Rows

  • One row represents one table
  • Scope of rows: all tables in a database including tables without rows
  • Ordered by number of rows descending, from largest to smallest (in terms of number of rows)

Sample results

Tables by number of rows in our SSRS repository:

Create beautiful and useful documentation of your Teradata

Generate convenient documentation of your databases in minutes and share it with your team. Capture and preserve tribal knowledge in shared repository.

See how it works

Confused about your Teradata database?

You don't have to be. There's an easy way to understand the data in your databases.

I want to understand