R2 SQL - R2 SQL now supports JOINs, subqueries, and multi-table queries
Key Points
- JOINs across Iceberg tables
- Subqueries in SELECT/WHERE/HAVING
- Multi-table CTEs, self/multi-way joins
Summary
R2 SQL now supports joining multiple Apache Iceberg tables stored in R2 Data Catalog, plus subqueries and multi-table CTEs. Queries execute on Cloudflare's global network with no infrastructure to manage, enabling richer analytical patterns directly over R2 data (joins, EXISTS/IN, derived tables, self- and multi-way joins).
Key Points
- Supported JOIN types:
INNER,LEFT,RIGHT,FULL OUTER,CROSS, and implicit (comma-separated) joins. - Subqueries:
IN/NOT IN,EXISTS/NOT EXISTS, scalar subqueries inSELECT/WHERE/HAVING, and derived tables inFROM. - Multi-table CTEs:
WITHclauses can reference multiple tables and include JOINs; self-joins and joins across three or more tables are supported. - Practical tips for engineers:
- Push predicates into individual scans or subqueries to reduce data movement.
- Use table aliases for clarity in self- and multi-way joins.
- Review the SQL reference and the "Limitations and best practices" guide for syntax details and performance guidance.
Examples
- Two-table aggregation:
SELECT z.domain, COUNT(*) FROM my_namespace.zones z INNER JOIN my_namespace.http_requests h ON z.zone_id = h.zone_id GROUP BY z.domain. - EXISTS subquery:
WHERE EXISTS (SELECT 1 FROM my_namespace.firewall_events f WHERE f.zone_id = z.zone_id AND f.action = 'block'). - Multi-table CTE:
WITH top_zones AS (...), zone_threats AS (...) SELECT tz.zone_id FROM top_zones tz LEFT JOIN zone_threats zt ON tz.zone_id = zt.zone_id.
For full syntax and performance guidance, refer to the SQL reference and the Limitations and best practices documentation.