Skip to content
Cloudflare Docs

SQL reference

This page documents the R2 SQL syntax based on the currently supported grammar in public beta.


Complete Query Syntax

SELECT column_list
FROM table_name
WHERE conditions --optional
[ORDER BY column_name [DESC | ASC]]
[LIMIT number]

SELECT Clause

Syntax

SELECT column_specification [, column_specification, ...]

Column Specification

  • Column name: column_name
  • All columns: *

Examples

SELECT * FROM table_name
SELECT user_id FROM table_name
SELECT user_id, timestamp, status FROM table_name
SELECT timestamp, user_id, response_code FROM table_name

FROM Clause

Syntax

SELECT * FROM table_name

WHERE Clause

Syntax

SELECT * WHERE condition [AND|OR condition ...]

Conditions

Null Checks

  • column_name IS NULL
  • column_name IS NOT NULL

Value Comparisons

  • column_name BETWEEN value' AND 'value
  • column_name = value
  • column_name >= value
  • column_name > value
  • column_name <= value
  • column_name < value
  • column_name != value
  • column_name LIKE 'value%'

Logical Operators

  • AND - Logical AND
  • OR - Logical OR

Data Types

  • integer - Whole numbers
  • float - Decimal numbers
  • string - Text values (quoted)
  • timestamp - RFC3339 format ('YYYY-DD-MMT-HH:MM:SSZ')
  • date - Date32/Data64 expressed as a string ('YYYY-MM-DD')
  • boolean - Explicitly valued (true, false)

Examples

SELECT * FROM table_name WHERE timestamp BETWEEN '2025-09-24T01:00:00Z' AND '2025-09-25T01:00:00Z'
SELECT * FROM table_name WHERE status = 200
SELECT * FROM table_name WHERE response_time > 1000
SELECT * FROM table_name WHERE user_id IS NOT NULL
SELECT * FROM table_name WHERE method = 'GET' AND status >= 200 AND status < 300
SELECT * FROM table_name WHERE (status = 404 OR status = 500) AND timestamp > '2024-01-01'

ORDER BY Clause

Syntax

--Note: ORDER BY only supports ordering by the partition key
ORDER BY partition_key [DESC]
  • ASC: Ascending order
  • DESC: Descending order
  • Default: DESC on all columns of the partition key
  • Can contain any columns from the partition key

Examples

SELECT * FROM table_name WHERE ... ORDER BY paetition_key_A
SELECT * FROM table_name WHERE ... ORDER BY partition_key_B DESC
SELECT * FROM table_name WHERE ... ORDER BY partitionKey_A ASC

LIMIT Clause

Syntax

LIMIT number
  • Range: 1 to 10,000
  • Type: Integer only
  • Default: 500

Examples

SELECT * FROM table_name WHERE ... LIMIT 100

Complete Query Examples

Basic Query

SELECT *
FROM http_requests
WHERE timestamp BETWEEN '2025-09-24T01:00:00Z' AND '2025-09-25T01:00:00Z'
LIMIT 100

Filtered Query with Sorting

SELECT user_id, timestamp, status, response_time
FROM access_logs
WHERE status >= 400 AND response_time > 5000
ORDER BY response_time DESC
LIMIT 50

Complex Conditions

SELECT timestamp, method, status, user_agent
FROM http_requests
WHERE (method = 'POST' OR method = 'PUT')
AND status BETWEEN 200 AND 299
AND user_agent IS NOT NULL
ORDER BY timestamp DESC
LIMIT 1000

Null Handling

SELECT user_id, session_id, date_column
FROM user_events
WHERE session_id IS NOT NULL
AND date_column >= '2024-01-01'
ORDER BY timestamp
LIMIT 500

Data Type Reference

Supported Types

TypeDescriptionExample Values
integerWhole numbers1, 42, -10, 0
floatDecimal numbers1.5, 3.14, -2.7, 0.0
stringText values'hello', 'GET', '2024-01-01'
booleanBoolean valuestrue, false
timestampRFC3339'2025-09-24T01:00:00Z'
date'YYYY-MM-DD''2025-09-24'

Type Usage in Conditions

-- Integer comparisons
SELECT * FROM table_name WHERE status = 200
SELECT * FROM table_name WHERE response_time > 1000
-- Float comparisons
SELECT * FROM table_name WHERE cpu_usage >= 85.5
SELECT * FROM table_name WHERE memory_ratio < 0.8
-- String comparisons
SELECT * FROM table_name WHERE method = 'POST'
SELECT * FROM table_name WHERE user_agent != 'bot'
SELECT * FROM table_name WHERE country_code = 'US'

Operator Precedence

  1. Comparison operators: =, !=, <, <=, >, >=, LIKE, BETWEEN, IS NULL, IS NOT NULL
  2. AND (higher precedence)
  3. OR (lower precedence)

Use parentheses to override default precedence:

SELECT * FROM table_name WHERE (status = 404 OR status = 500) AND method = 'GET'