Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/r2-sql/api.md
1# R2 SQL API Reference23SQL syntax, functions, operators, and data types for R2 SQL queries.45## SQL Syntax67```sql8SELECT column_list | aggregation_function9FROM [namespace.]table_name10WHERE conditions11[GROUP BY column_list]12[HAVING conditions]13[ORDER BY column | aggregation_function [DESC | ASC]]14[LIMIT number]15```1617## Schema Discovery1819```sql20SHOW DATABASES; -- List namespaces21SHOW NAMESPACES; -- Alias for SHOW DATABASES22SHOW SCHEMAS; -- Alias for SHOW DATABASES23SHOW TABLES IN namespace; -- List tables in namespace24DESCRIBE namespace.table; -- Show table schema, partition keys25```2627## SELECT Clause2829```sql30-- All columns31SELECT * FROM logs.http_requests;3233-- Specific columns34SELECT user_id, timestamp, status FROM logs.http_requests;35```3637**Limitations:** No column aliases, expressions, or nested column access3839## WHERE Clause4041### Operators4243| Operator | Example |44|----------|---------|45| `=`, `!=`, `<`, `<=`, `>`, `>=` | `status = 200` |46| `LIKE` | `user_agent LIKE '%Chrome%'` |47| `BETWEEN` | `timestamp BETWEEN '2025-01-01T00:00:00Z' AND '2025-01-31T23:59:59Z'` |48| `IS NULL`, `IS NOT NULL` | `email IS NOT NULL` |49| `AND`, `OR` | `status = 200 AND method = 'GET'` |5051Use parentheses for precedence: `(status = 404 OR status = 500) AND method = 'POST'`5253## Aggregation Functions5455| Function | Description |56|----------|-------------|57| `COUNT(*)` | Count all rows |58| `COUNT(column)` | Count non-null values |59| `COUNT(DISTINCT column)` | Count unique values |60| `SUM(column)`, `AVG(column)` | Numeric aggregations |61| `MIN(column)`, `MAX(column)` | Min/max values |6263```sql64-- Multiple aggregations with GROUP BY65SELECT region, COUNT(*), SUM(amount), AVG(amount)66FROM sales.transactions67WHERE sale_date >= '2024-01-01'68GROUP BY region;69```7071## HAVING Clause7273Filter aggregated results (after GROUP BY):7475```sql76SELECT category, SUM(amount)77FROM sales.transactions78GROUP BY category79HAVING SUM(amount) > 10000;80```8182## ORDER BY Clause8384Sort results by:85- **Partition key columns** - Always supported86- **Aggregation functions** - Supported via shuffle strategy8788```sql89-- Order by partition key90SELECT * FROM logs.requests ORDER BY timestamp DESC LIMIT 100;9192-- Order by aggregation (repeat function, aliases not supported)93SELECT region, SUM(amount)94FROM sales.transactions95GROUP BY region96ORDER BY SUM(amount) DESC;97```9899**Limitations:** Cannot order by non-partition columns. See [gotchas.md](gotchas.md#order-by-limitations)100101## LIMIT Clause102103```sql104SELECT * FROM logs.requests LIMIT 100;105```106107| Setting | Value |108|---------|-------|109| Min | 1 |110| Max | 10,000 |111| Default | 500 |112113**Always use LIMIT** to enable early termination optimization.114115## Data Types116117| Type | SQL Literal | Example |118|------|-------------|---------|119| `integer` | Unquoted number | `42`, `-10` |120| `float` | Decimal number | `3.14`, `-0.5` |121| `string` | Single quotes | `'hello'`, `'GET'` |122| `boolean` | Keyword | `true`, `false` |123| `timestamp` | RFC3339 string | `'2025-01-01T00:00:00Z'` |124| `date` | ISO 8601 date | `'2025-01-01'` |125126### Type Safety127128- Quote strings with single quotes: `'value'`129- Timestamps must be RFC3339: `'2025-01-01T00:00:00Z'` (include timezone)130- Dates must be ISO 8601: `'2025-01-01'` (YYYY-MM-DD)131- No implicit conversions132133```sql134-- ✅ Correct135WHERE status = 200 AND method = 'GET' AND timestamp > '2025-01-01T00:00:00Z'136137-- ❌ Wrong138WHERE status = '200' -- string instead of integer139WHERE timestamp > '2025-01-01' -- missing time/timezone140WHERE method = GET -- unquoted string141```142143## Query Result Format144145JSON array of objects:146147```json148[149{"user_id": "user_123", "timestamp": "2025-01-15T10:30:00Z", "status": 200},150{"user_id": "user_456", "timestamp": "2025-01-15T10:31:00Z", "status": 404}151]152```153154## See Also155156- [patterns.md](patterns.md) - Query examples and use cases157- [gotchas.md](gotchas.md) - SQL limitations and error handling158- [configuration.md](configuration.md) - Setup and authentication159