R2 SQL now supports over 190 new functions, expressions, and complex types
R2 SQL now supports an expanded SQL grammar including 163 scalar functions, 33 aggregate functions, CASE expressions, Common Table Expressions, column aliases, and full struct/array/map access, enabling richer analytical queries on Apache Iceberg tables without exporting data.
R2 SQL now supports an expanded SQL grammar so you can write richer analytical queries without exporting data. This release adds CASE expressions, column aliases, arithmetic in clauses, 163 scalar functions, 33 aggregate functions, EXPLAIN, Common Table Expressions (CTEs),and full struct/array/map access. R2 SQL is Cloudflare's serverless, distributed, analytics query engine for querying Apache Iceberg ↗ tables stored in R2 Data Catalog. This page documents the supported SQL syntax.
Highlights
- Column aliases —
SELECT col AS aliasnow works in all clauses - CASE expressions — conditional logic directly in SQL (searched and simple forms)
- Scalar functions — 163 new functions across math, string, datetime, regex, crypto, encoding, and type inspection categories
- Aggregate functions — statistical (variance, stddev, correlation, regression), bitwise, boolean, and positional aggregates join the existing basic and approximate functions
- Complex types — query struct fields with bracket notation, use 46 array functions, and extract map keys/values
- Common table expressions (CTEs) — use
WITH ... ASto define named temporary result sets. Chained CTEs are supported. All CTEs must reference the same single table. - Full expression support — arithmetic, type casting (
CAST,TRY_CAST,::shorthand), andEXTRACTin SELECT, WHERE, GROUP BY, HAVING, and ORDER BY
Examples
CASE expressions with statistical aggregates
SELECT source,
CASE
WHEN AVG(price) > 30 THEN 'premium'
WHEN AVG(price) > 10 THEN 'mid-tier'
ELSE 'budget'
END AS tier,
round(stddev(price), 2) AS price_volatility,
approx_percentile_cont(price, 0.95) AS p95_price
FROM my_namespace.sales_data
GROUP BY source
Struct and array access
SELECT product_name,
pricing['price'] AS price,
array_to_string(tags, ', ') AS tag_list
FROM my_namespace.products
WHERE array_has(tags, 'Action')
ORDER BY pricing['price'] DESC
LIMIT 10
Chained CTEs with time-series analysis
WITH monthly AS (
SELECT date_trunc('month', sale_timestamp) AS month,
department,
COUNT(*) AS transactions,
round(AVG(total_amount), 2) AS avg_amount
FROM my_namespace.sales_data
WHERE sale_timestamp BETWEEN '2025-01-01T00:00:00Z' AND '2025-12-31T23:59:59Z'
GROUP BY date_trunc('month', sale_timestamp), department
),
ranked AS (
SELECT month, department, transactions, avg_amount,
CASE
WHEN avg_amount > 1000 THEN 'high-value'
WHEN avg_amount > 500 THEN 'mid-value'
ELSE 'standard'
END AS tier
FROM monthly
WHERE transactions > 100
)
SELECT * FROM ranked
ORDER BY month, avg_amount DESC
For the full function reference and syntax details, refer to the SQL reference. For limitations and best practices, refer to Limitations and best practices.
Source: original entry ↗
More from Cloudflare
Follow Cloudflare to get its new changes in your feed and email digest.
Cloudflare One Client for macOS 2026.8.2100.0
GA release for macOS Cloudflare One Client with improved split tunnel handling that no longer briefly blocks traffic during reconnects, support for non-RFC 1918 local IPv4 networks, faster connects with lower memory use, and numerous reliability fixes across DNS, reauthentication, and client stability.
Cloudflare One Client for Windows 2026.8.2100.0
This GA release improves split tunnel reliability, adds support for non-RFC 1918 local networks, optimizes connection performance with faster reconnections and lower memory usage, and includes numerous bug fixes for DNS, registration, and network handling. The client now features a service recovery mechanism that automatically restarts on system unlock and better handles large hosts files without blocking traffic.
Cloudflare One Client for Linux 2026.8.2100.0
New GA release for Linux with improved split tunnel handling that no longer briefly blocks traffic during reconnects, support for non-RFC 1918 local IPv4 networks, faster tunnel reconnections, and lower memory usage. Includes numerous stability and reliability fixes for DNS, reconnection behavior, and crash issues.