Query Commands
Execute SQL queries using Trino.
Basic Usage
tdx query [options] [sql]Options
| Option | Description | Default |
|---|---|---|
-f, --file <path> | Read SQL from file | - |
-d, --database <db> | Database to query | information_schema |
--in <db> | Alias for --database | - |
--catalog <catalog> | Trino catalog | td |
--limit <rows> | Maximum rows to display | 40 (interactive terminal only) |
Examples
Inline Query
# Simple query
tdx query "SELECT * FROM mydb.users LIMIT 10"
# With database flag
tdx query "SELECT * FROM users" --in mydb
tdx query "SELECT * FROM users" --database mydb
tdx query "SELECT * FROM users" -d mydbFile Input
Read SQL from file:
# A positional argument that names an existing file is read as a file
tdx query query.sql
# Explicit file option
tdx query --file query.sql
tdx query -f query.sql
# With options
tdx query query.sql --database mydb --json --output results.jsonFile vs inline SQL
Whether a positional argument is read as a file or run as inline SQL is decided purely by whether a file of that name exists on disk — not by its extension. So tdx query query.sql reads the file when query.sql exists, but if it does not exist the argument is treated as inline SQL (and will fail to parse). Use --file to force file input regardless of the argument's shape.
Stdin Input
Read SQL from stdin using -:
# Pipe SQL from echo
echo "SELECT * FROM mydb.users LIMIT 10" | tdx query -
# Pipe from file
cat query.sql | tdx query -
# Pipe from another command
generate-sql.sh | tdx query -Multi-Statement Execution
Execute multiple SQL statements sequentially:
# From command line
tdx query "SELECT COUNT(*) FROM users; SELECT COUNT(*) FROM orders"
# From file
tdx query -f setup-and-query.sqlExample multi-statement file (setup-and-query.sql):
CREATE TABLE temp_users AS SELECT * FROM users WHERE active = true;
CREATE TABLE temp_orders AS SELECT * FROM orders WHERE user_id IN (SELECT id FROM temp_users);
SELECT COUNT(*) as order_count FROM temp_orders;Output behavior:
- Each statement executes sequentially with progress indication
- Console output: All results displayed as they complete
- File output (
--output): All results appended to single file - Fail-fast: Execution stops on first error
Output Formats
# Table format (default)
tdx query "SELECT * FROM users LIMIT 5"
# JSON format
tdx query "SELECT * FROM users" --json
# JSON Lines format
tdx query "SELECT * FROM users" --jsonl
# TSV format
tdx query "SELECT * FROM users" --tsv
# Markdown table, or save as Markdown with a .md file name
tdx query "SELECT * FROM users LIMIT 5" --md
tdx query "SELECT * FROM users LIMIT 5" --output users.md
# Save to file
tdx query "SELECT * FROM users" --output results.jsonRow Limit and Piping
Without --limit, the default cap of 40 rows applies only when results are shown live on an interactive terminal (to avoid flooding the screen). When the output isn't on screen — piped, redirected, or written to a file with --output — all rows flow through, so tdx query composes cleanly with Unix tools and saves complete files:
# Interactive terminal: table paged through less, structured formats capped at 40 rows
tdx query "SELECT * FROM users"
# Piped/redirected (no TTY): no default cap — all rows are returned
tdx query "SELECT * FROM users" --tsv | wc -l
tdx query "SELECT * FROM users" --json > users.json
# Written to a file (--output): no default cap — the full result set is saved
tdx query "SELECT * FROM users" --output users.json
# An explicit --limit is always honored, terminal or not
tdx query "SELECT * FROM users" --limit 5 | headMulti-Statement Output Example
✔ Statement 1/3 - Query completed: Processed 1,000 rows in 2.3s
┌────────────┐
│ user_count │
├────────────┤
│ 1000 │
└────────────┘
✔ Statement 2/3 - Query completed: Processed 5,000 rows in 3.1s
┌─────────────┐
│ order_count │
├─────────────┤
│ 5000 │
└─────────────┘