R2 SQL now supports window functions, DISTINCT, and set operations
R2 SQL now supports window functions, SELECT DISTINCT, set operations, and additional aggregates, making it easier to write analytical queries without preprocessing your data elsewhere.
R2 SQL is Cloudflare's serverless, distributed SQL engine for querying Apache Iceberg ↗ tables stored in R2 Data Catalog.
- Window functions —
ROW_NUMBER,RANK,DENSE_RANK,PERCENT_RANK,CUME_DIST,NTILE,LAG,LEAD,FIRST_VALUE,LAST_VALUE,NTH_VALUE, and aggregates with anOVER (...)clause, includingPARTITION BYand explicit frames - QUALIFY — filter rows based on a window function result
- DISTINCT —
SELECT DISTINCT,DISTINCT ON (...), and theDISTINCTmodifier on aggregates such asCOUNT(DISTINCT ...) - Set operations —
UNION,UNION ALL,INTERSECT, andEXCEPT - Grouping extensions —
GROUPING SETS,ROLLUP, andCUBE - Exact aggregates —
MEDIAN,PERCENTILE_CONT,ARRAY_AGG, andSTRING_AGG
SELECT customer_id, region, ROW_NUMBER() OVER (PARTITION BY region ORDER BY total_amount DESC) AS rank_in_regionFROM my_namespace.sales_dataSELECT customer_id, region, total_amountFROM my_namespace.sales_dataQUALIFY ROW_NUMBER() OVER (PARTITION BY region ORDER BY total_amount DESC) <= 3SELECT customer_id FROM my_namespace.sales_dataEXCEPTSELECT customer_id FROM my_namespace.archived_salesThe named WINDOW clause is not supported — inline the OVER (...) specification at each call site. For the full syntax reference, refer to the SQL reference. For supported features and performance guidance, refer to Limitations and best practices.