Query Data Using SQL from the CLI¶
This page explains how to run SQL queries against kdb Insights Enterprise databases using the CLI. For SQL via the web interface or REST API, refer to SQL Query Support.
kxi query --sql lets you run SQL queries directly from the CLI.
Permissions
You need the insights.query.data and insights.query.sql roles, which are included in the Views-only preconfigured high-level role. Refer to Roles for details.
Running a SQL query¶
Pass your SQL query to the --sql flag:
kxi query --sql "SELECT * FROM trades LIMIT 10"
date time sym side price size
2026-06-10 2026-06-10T09:30:00.000000 AAPL buy 173.25 500
2026-06-10 2026-06-10T09:30:01.123456 MSFT buy 419.15 200
2026-06-10 2026-06-10T09:30:02.456789 AMZN sell 183.15 750
2026-06-10 2026-06-10T09:30:03.789012 AAPL sell 174.50 300
2026-06-10 2026-06-10T09:30:04.012345 MSFT buy 418.90 150
2026-06-10 2026-06-10T09:30:05.234567 AMZN buy 182.75 600
2026-06-10 2026-06-10T09:30:06.567890 AAPL buy 173.80 400
2026-06-10 2026-06-10T09:30:07.890123 MSFT sell 419.30 250
2026-06-10 2026-06-10T09:30:08.123456 AMZN sell 183.40 350
2026-06-10 2026-06-10T09:30:09.456789 AAPL buy 174.10 700
Output formats¶
--output-format and --output-file control how query results are returned.
--output-format |
--output-file |
Action |
|---|---|---|
tabular / unset |
— | Print tabular text to console |
tabular / unset |
✓ | Write serialized q bytes to file |
csv |
either | Write / print CSV |
json |
either | Write / print columnar JSON |
json_records |
either | Write / print one JSON object per row |
pandas |
✓ | Write pandas pickle to file |
Comparison with kxi query uda¶
kxi query --sql and kxi query uda support the same output formats, except pandas requires --output-file for --sql and is available without a file when running a named UDA.
| Format | --sql |
uda (list) |
uda .ns.func |
|---|---|---|---|
tabular |
✓ | ✓ | ✓ |
csv |
✓ | ✓ | ✓ |
json |
✓ | ✓ | ✓ |
json_records |
✓ | ✓ | ✓ |
pandas |
✓ (file only) | — | ✓ (file only) |