SQL Server TOP in Snowflake, Databricks and BigQuery
Snowflake accepts SELECT TOP n. Databricks and BigQuery use LIMIT n at the end of the query. TOP n PERCENT and WITH TIES have no direct equivalent in any of them.
SQL Server syntax: SELECT TOP (expression) [PERCENT] [WITH TIES] ... · SQL Server docs
Equivalents
| Target | Support | Syntax | Notes | Source |
|---|---|---|---|---|
| Snowflake | Same name | SELECT TOP n ... | TOP n and LIMIT n are equivalent in Snowflake. | Snowflake docs |
| Databricks | Rewrite | SELECT ... LIMIT n | LIMIT goes at the end of the query. | Databricks docs |
| BigQuery | Rewrite | SELECT ... LIMIT n | LIMIT goes at the end of the query; OFFSET is optional. | BigQuery docs |
Behavior differences to check
- TOP n PERCENT and TOP n WITH TIES need a rewrite with window functions, for example QUALIFY RANK() OVER (ORDER BY score DESC) <= n for WITH TIES (QUALIFY works in all three targets).
- TOP in UPDATE and DELETE statements is SQL Server only; rewrite those with a key filter.
Examples
Ten most recent orders
SELECT TOP 10 order_id, ordered_at FROM orders ORDER BY ordered_at DESC;SELECT TOP 10 order_id, ordered_at FROM orders ORDER BY ordered_at DESC;SELECT order_id, ordered_at FROM orders ORDER BY ordered_at DESC LIMIT 10;SELECT order_id, ordered_at FROM orders ORDER BY ordered_at DESC LIMIT 10;