Are you trying to restrict the number of rows returned by your SQL queries without pulling the entire dataset? Using the LIMIT and OFFSET clauses in Snowflake is the most efficient way to achieve this.
Page Content
Introduction
When working with massive datasets, fetching every single row isn’t always practical or necessary. The LIMIT clause in Snowflake is designed to restrict the number of results returned by your main SQL statement or subquery. This is incredibly useful when you want to sample data, test query logic with a limited number of rows, or improve overall query performance.
In this article, we will explore the syntax, parameters, and practical examples of using the Snowflake LIMIT and OFFSET clauses to streamline your data retrieval.
Snowflake LIMIT and OFFSET Overview
By definition, the LIMIT clause constrains the maximum number of rows a query outputs. While this is a well-known PostgreSQL syntax used to limit SELECT statement results, Snowflake also fully supports the ANSI-compliant FETCH to limit rows (e.g., FETCH NEXT <count> ROWS ONLY). Both produce the exact same outcome.
Here is the standard syntax for using Snowflake LIMIT and OFFSET together:
SELECT ...
FROM ...
[ ORDER BY ... ]
LIMIT <count> [ OFFSET <start> ]
[ ... ]
Important Note: Using an ORDER BY clause is not strictly mandatory. However, without it, Snowflake’s relational engine returns results non-deterministically, meaning the output order can change every time you run the query. To guarantee consistent results, always pair LIMIT and OFFSET with an ORDER BY statement. For a deeper technical dive, you can refer to the official Snowflake documentation on LIMIT / FETCH.
Snowflake LIMIT Clause in Action
When you use the Snowflake SQL LIMIT clause with a single argument, the database engine uses that value to determine the absolute maximum number of rows to pull, starting directly from the first row of your sorted result set.
For example, if you want to quickly preview the top 3 rows of a table, you would use the following query:
SELECT * FROM some_table
ORDER BY id
LIMIT 3;
-- Returns: Only the first 3 rows from the sorted result set.
Snowflake LIMIT Clause Parameters
The LIMIT clause takes a single parameter known as count, which must be a non-negative integer. However, Snowflake is flexible and accepts a few alternative parameter types:
- A
NULLvalue. - An empty string (
''). - The literal string
$$$$.
If you pass any of the above parameters to the LIMIT clause, Snowflake treats it as an “unlimited” request—meaning it will ignore the limit and return all rows in the dataset. This quirk is particularly useful for dynamically binding parameters via connectors (like JDBC drivers) when incomplete parameters are passed.
Snowflake OFFSET Clause for Pagination
Often, you need to read rows from a table in batches. This is known as SQL pagination in Snowflake. To do this, you specify an OFFSET parameter, which tells the database exactly where to skip ahead before it starts returning data. If you omit the OFFSET, the query naturally defaults to the first row.
For instance, suppose you need 5 rows, but you want to skip the first two. You simply apply an OFFSET of 2:
SELECT * FROM some_table
ORDER BY id
LIMIT 5 OFFSET 2;
-- Returns: 5 rows total, starting from the 3rd row (skipping rows 1 and 2).
Snowflake OFFSET Clause Parameters
Similar to LIMIT, the OFFSET clause accepts a single parameter known as start. This must also be a non-negative integer constant.
The start parameter also accepts the same flexible fallback values:
- A
NULLvalue. - An empty string (
''). - The literal string
$$$$.
If you use any of these alternatives, Snowflake treats the offset as 0. In other words, it will not skip any rows and will begin fetching right from the top of the result set.
Snowflake OFFSET Without LIMIT
Currently, LIMIT is a required keyword if you want to utilize the OFFSET clause in Snowflake. However, if your goal is to skip a certain number of rows without placing a cap on the maximum rows returned afterward, you can safely set the LIMIT to NULL.
Here is how you execute an offset parameter in Snowflake without limiting the final row count:
SELECT * FROM some_table
ORDER BY id
LIMIT NULL OFFSET 2;
-- Returns: All rows from the table, except the first 2 rows.
Conclusion
Mastering how to limit rows in Snowflake is a foundational skill for data engineers and analysts. By leveraging the Snowflake LIMIT and OFFSET clauses—or their ANSI equivalent FETCH—you can seamlessly implement pagination, test complex queries faster, and take control of your data outputs. Just remember to always tie these clauses to an ORDER BY statement for predictable, reliable results.
Related Articles:
- Snowflake WITH Clause Syntax, Usage and Examples
- Snowflake Merge Statement Syntax, Usage and Examples
- Sequence in Snowflake – How to Create and Use it?
Hope this helps 🙂