0Pricing
Edge Computing with Cloudflare Workers & Deno · Lesson

Querying with D1 SQL at the Edge

Use Cloudflare D1, a serverless SQLite database, to run relational queries from your Workers at the edge.

Querying with D1 SQL at the Edge is a free Edge Computing with Cloudflare Workers & Deno lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Edge Computing with Cloudflare Workers & Deno learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is D1?

Cloudflare D1 is a serverless SQL database built on SQLite. It brings relational queries to the edge, complementing key-value (KV) and Durable Objects.

KV vs D1

When to use which:

  • KV: simple key-value, eventual consistency
  • D1: structured data, joins, transactions, SQL

Creating a Database

Create a D1 database from the CLI, then bind it to your Worker.

wrangler d1 create my-db

Binding D1

Add the binding to wrangler.toml so the database appears on the env object.

# wrangler.toml
[[d1_databases]]
binding = 'DB'
database_name = 'my-db'
database_id = 'xxxx'

Creating a Schema

Define tables with standard SQL, executed via the CLI or a migration file.

CREATE TABLE users (
  id INTEGER PRIMARY KEY,
  email TEXT NOT NULL UNIQUE
);

Prepared Statements

Query D1 from your Worker using prepared statements with bound parameters to prevent SQL injection.

const row = await env.DB
  .prepare('SELECT * FROM users WHERE id = ?')
  .bind(userId)
  .first();

Reading Multiple Rows

Use .all() to fetch every matching row as an array of objects.

const { results } = await env.DB
  .prepare('SELECT email FROM users LIMIT 10')
  .all();

Writing Data

Inserts and updates use the same prepared-statement API with .run().

await env.DB
  .prepare('INSERT INTO users (email) VALUES (?)')
  .bind('a@b.com')
  .run();

Batching Statements

Run several statements atomically in a single round trip with batch, reducing latency.

await env.DB.batch([
  env.DB.prepare('INSERT INTO users (email) VALUES (?)').bind('x@y.com'),
  env.DB.prepare('INSERT INTO users (email) VALUES (?)').bind('p@q.com')
]);

Consistency & Replication

D1 offers strong consistency on its primary, with read replication for low-latency reads near users. Plan writes around the primary location.

When to Choose D1

Pick D1 when you need relational structure, transactions, and SQL at the edge; pick KV for fast simple lookups and Durable Objects for coordinated state.

Quick Check

Test your D1 knowledge.

Recap

You learned to create and bind a D1 database, define schemas, run safe parameterized queries, batch statements, and choose D1 over KV or Durable Objects when you need relational data at the edge.

Frequently asked questions

Is the “Querying with D1 SQL at the Edge” lesson free?

Yes — the full text of “Querying with D1 SQL at the Edge” is free to read here on the web, and the Edge Computing with Cloudflare Workers & Deno course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Edge Computing with Cloudflare Workers & Deno course, upgrade to CoddyKit PRO.

What will I learn in “Querying with D1 SQL at the Edge”?

Use Cloudflare D1, a serverless SQLite database, to run relational queries from your Workers at the edge. You practise Edge Computing with Cloudflare Workers & Deno with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Edge Computing with Cloudflare Workers & Deno?

No prior experience is required. Edge Computing with Cloudflare Workers & Deno on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Querying with D1 SQL at the Edge” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Edge Computing with Cloudflare Workers & Deno lesson?

Yes. Every Edge Computing with Cloudflare Workers & Deno lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Cloudflare KV Store
  2. Durable Objects Explained
  3. Integrating Deno with Edge Storage
  4. Querying with D1 SQL at the Edge
← Back to Edge Computing with Cloudflare Workers & Deno