Run a one-off SQL query against local or remote

d1 & hyperdrive

// what it does

Fire an ad-hoc query without opening a SQL client. --local hits the emulated DB that wrangler dev uses; --remote runs against the deployed production D1. Add --json for machine-readable output you can pipe into jq. <database> can be the binding name or the database name.

// shell

$ wrangler d1 execute <database> --command "SELECT * FROM users LIMIT 5" --local
$ wrangler d1 execute <database> --command "SELECT * FROM users LIMIT 5" --remote

// gotcha

execute defaults to --local — omit --remote and you're querying an empty local file, not production.