sql
Run SQL on your data warehouse using existing connections. Two modes: query returns result rows; job runs DDL/DML and polls to completion without returning a result set.
carto sql query <connection> [sql]
Run a SQL query and return results. Queries are sent as a POST (no URL-length limit, 1-minute timeout).
# Query argument
carto sql query <connection> "SELECT * FROM dataset.table LIMIT 10"
# Read SQL from a file
carto sql query <connection> --file query.sql
# Pipe via stdin
echo "SELECT COUNT(*) FROM dataset.table" | carto sql query <connection>
# Bind parameters
carto sql query <connection> "SELECT * FROM t WHERE id = @id" --param id=a-001
# Run a saved named source by name
carto sql query <connection> my_named_source --param id=a-001
# JSON output
carto sql query <connection> "SELECT * FROM dataset.table" --jsonOptions:
--file <path>
Read SQL from a file.
--param <key=value>
Bind a query parameter. Repeatable. Values are JSON-parsed (number, boolean, array), otherwise treated as a string.
--params-json <json>
Bind query parameters from a JSON object or array, for full type control. Not combinable with --param.
carto sql job <connection> [sql]
Run a DDL/DML job. Used when no result set is returned. The CLI polls every second and waits up to 10 minutes for the job to finish. For longer jobs, pass --no-wait and check progress with carto sql job status.
Options:
--file <path>
Read SQL from a file.
--no-wait
Submit the job and return its ID immediately instead of waiting for it to finish.
--param <key=value>
Bind a query parameter. Repeatable. Same behavior as in sql query.
--params-json <json>
Bind query parameters from a JSON object or array. Not combinable with --param.
Use cases:
CREATE TABLEoperations.INSERT,UPDATE,DELETEstatements.Long-running data transformations.
Operations that don't return result sets.
carto sql job status <connection> <job-id>
Check the status of a job submitted with --no-wait.
carto sql job cancel <connection> <job-id>
Cancel a running job.
Input methods (both query and job)
Command argument:
carto sql query myconn "SELECT * FROM table".File:
carto sql query myconn --file query.sql.Stdin:
echo "SELECT * FROM table" | carto sql query myconn.Named source: pass the name of a saved named source instead of SQL, for example
carto sql query myconn my_named_source. CARTO runs the saved query, and--param/--params-jsonbind its parameters.
Last updated
Was this helpful?
