Get the essential data observability guide
Download this guide to learn:
What is data observability?
4 pillars of data observability
How to evaluate platforms
Common mistakes to avoid
The ROI of data observability
Unlock now
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Sign up for a free data observability workshop today.
Assess your company's data health and learn how to start monitoring your entire data stack.
Book free workshop
Sign up for news, updates, and events
Subscribe for free
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Getting started with Data Observability Guide

Make a plan to implement data observability across your company’s entire data stack

Download for free
Book a data observability workshop with an expert.

Assess your company's data health and learn how to start monitoring your entire data stack.

Book free workshop

Three Ways to Retrieve Row Counts for Snowflake Tables and Views

Determining the number of rows in a table or view is often essential when working with Snowflake. This information can prove valuable for various purposes, such as performance optimization, data analysis, and monitoring. In this article, we will explore different approaches to obtain row counts in Snowflake, ranging from simple SQL queries using `COUNT(*)` to leveraging table statistics. We will also highlight essential considerations and provide SQL snippets to ensure correct execution.

and
May 12, 2023

Co-founder / Data and ML

May 12, 2023
Three Ways to Retrieve Row Counts for Snowflake Tables and Views

Method 1: COUNT(*)

The most straightforward way to retrieve row counts for both tables and views in Snowflake is by using the `COUNT(*)` function in SQL. This method provides an accurate count but can be resource-intensive for larger tables and views.

```sql

SELECT COUNT(*) AS row_count

FROM database.schema.table;

```

Important note: Replace `database`, `schema`, and `table` with the appropriate identifiers for your Snowflake environment.

Method 2: Snowflake Metadata Queries

Snowflake metadata views contain information about databases, schemas, tables, and views, but the row counts for views are not stored in these metadata. By querying the appropriate metadata view and filtering on the desired table, we can efficiently obtain row counts of tables, but this method is not applicable to views.

```sql

SELECT 

  TABLE_CATALOG AS database,

  TABLE_SCHEMA AS schema,

  TABLE_NAME AS table,

  ROW_COUNT AS row_count

FROM 

  SNOWFLAKE.ACCOUNT_USAGE.TABLES

WHERE 

  TABLE_NAME = 'table'

  AND TABLE_SCHEMA = 'schema'

  AND TABLE_CATALOG = 'database';

```

Important note: Replace `database`, `schema`, and `table` with the correct identifiers for your Snowflake environment. Additionally, ensure your user has the necessary privileges to access the metadata views.

{{inline-a}}

Method 3: Using Snowflake Information Schema

Similar to metadata views, Snowflake provides an INFORMATION_SCHEMA containing metadata about databases, schemas, tables, and views. However, like the Snowflake metadata views, the row counts for views are not stored here, so this method is also not applicable to views. By querying the appropriate INFORMATION_SCHEMA view and filtering on the desired table, we can effectively obtain row counts of tables.

```sql

SELECT 

  TABLE_CATALOG AS database,

  TABLE_SCHEMA AS schema,

  TABLE_NAME AS table,

  ROW_COUNT AS row_count

FROM 

  INFORMATION_SCHEMA.TABLES

WHERE 

  TABLE_NAME = 'table'

  AND TABLE_SCHEMA = 'schema'

  AND TABLE_CATALOG = 'database';

```

Important note: Replace `database`, `schema`, and `table` with the correct identifiers for your Snowflake environment. Ensure your user has the necessary privileges to access the INFORMATION_SCHEMA.

Conclusion

Obtaining row counts in Snowflake tables or views is crucial for various use cases. While the `COUNT(*)` method provides an accurate count, it can be resource-intensive for large tables. Alternatively, leveraging Snowflake metadata queries or INFORMATION_SCHEMA enables efficient row count retrieval. However, it's essential to note the necessary privileges required to access metadata views or INFORMATION_SCHEMA. Additionally, table statistics provide approximate row counts and may not always reflect the latest count. Choose the method that best suits your requirements based on table size, desired accuracy, and associated costs in your Snowflake environment.

We’re hard at work helping you improve trust in your data in less time than ever. We promise to send a maximum of 1 update email per week.

Your email
Ensure trust in data

Start monitoring your data in minutes.

Connect your warehouse and start generating a baseline in less than 10 minutes. Start for free, no credit-card required.