Exporting a SQL query
How to export a query?
Shell mode
.format json.format csv.format plain.format htmlFlag argument
anyquery -q "SELECT * FROM table" --jsonanyquery -q "SELECT * FROM table" --csvanyquery -q "SELECT * FROM table" --plainanyquery -q "SELECT * FROM table" --format htmlIntroduction
Section titled “Introduction”Anyquery allows you to export the result of a query to various formats. See below for the full list of supported formats.
Specify the format
Section titled “Specify the format”Shell mode
Section titled “Shell mode”To specify the format once you have entered the shell mode, run:
.format <format>You can also use the short version for some formats:
.json.csvTo revert back to the pretty format (the default one), run:
.format pretty-- or.mode prettyFlag argument
Section titled “Flag argument”To specify the format as a flag argument, run:
# JSONanyquery -q "SELECT * FROM table" --json# CSVanyquery -q "SELECT * FROM table" --csv# Plain textanyquery -q "SELECT * FROM table" --plain# HTMLanyquery -q "SELECT * FROM table" --format htmlRedirecting the output
Section titled “Redirecting the output”You can redirect the output to a file using the > operator. For example:
anyquery -q "SELECT * FROM table" --json > output.jsonIn shell mode, you can use .output to specify the output file:
.output output.jsonSELECT * FROM table;All the next queries will be written to the output.json file. To revert back to the standard output, run:
.outputSupported formats
Section titled “Supported formats”Export the result of a query to a CSV (RFC 4180) file.
.format csv-- or.csvanyquery -q "SELECT * FROM table" --csvExample
anyquery> .csvOutput mode set to CSV
anyquery> SELECT name, stargazers_count as stars FROM github_repositories_from_user('simonw') ORDER BY stars DESC LIMIT 3;name,starsdatasette,9133llm,3556shot-scraper,1591Export the result of a query as an HTML table.
.format htmlanyquery -q "SELECT * FROM table" --format htmlExample
anyquery> .mode htmlOutput mode set to html
anyquery> SELECT name, stargazers_count as stars FROM github_repositories_from_user('simonw') ORDER BY stars DESC LIMIT 3;<table>    <thead>    <tr>        <th>name</th>        <th>stars</th>    </tr>    </thead>    <tbody>    <tr>        <td>datasette</td>        <td>9133</td>    </tr>    <tr>        <td>llm</td>        <td>3556</td>    </tr>    <tr>        <td>shot-scraper</td>        <td>1591</td>    </tr>  </tbody></table>Export the result of a query as a JSON array of objects. Each column is a key in the object.
.format json-- or.jsonanyquery -q "SELECT * FROM table" --jsonExample
anyquery> .jsonOutput mode set to JSON
anyquery> SELECT name, stargazers_count as stars FROM github_repositories_from_user('simonw') ORDER BY stars DESC LIMIT 3;[  {    "name": "datasette",    "stars": 9133  },  {    "name": "llm",    "stars": 3556  },  {    "name": "shot-scraper",    "stars": 1591  }]Export the result of a query as a JSON Lines file. Each line is a JSON object representing a row separated by a newline.
.format jsonlanyquery -q "SELECT * FROM table" --format jsonlExample
anyquery> .mode jsonlOutput mode set to jsonl
anyquery> SELECT name, stargazers_count as stars FROM github_repositories_from_user('simonw') ORDER BY stars DESC LIMIT 3;{"name":"datasette","stars":9133}{"name":"llm","stars":3556}{"name":"shot-scraper","stars":1591}Line by line
Section titled “Line by line”Export the result of a query where each line is a column of a row. Rows are separated by ---.
.format linebylineanyquery -q "SELECT * FROM table" --format linebylineExample
anyquery> .mode linebylineOutput mode set to linebyline
anyquery> SELECT name, stargazers_count as stars FROM github_repositories_from_user('simonw') ORDER BY stars DESC LIMIT 3;name: datasettestars: 9133---name: llmstars: 3556---name: shot-scraperstars: 1591Markdown
Section titled “Markdown”Export the result of a query as a markdown table that can be pasted to GitHub, Notion, and pretty much any markdown editor.
.format markdownanyquery -q "SELECT * FROM table" --format markdownExample
anyquery> .mode markdownOutput mode set to markdown
anyquery> SELECT name, stargazers_count as stars FROM github_repositories_from_user('simonw') ORDER BY stars DESC LIMIT 3;| name | stars |  || ---- | ----- || datasette    | 9133  || llm          | 3556  || shot-scraper | 1591  |Plain text
Section titled “Plain text”Export the result of a query as plain text (column values separated by a tab and rows separated by a newline). This is the default one if stdout is not a terminal.
.format plainanyquery -q "SELECT * FROM table" --plainExample
anyquery> .mode plainOutput mode set to plain
anyquery> SELECT name, stargazers_count as stars FROM github_repositories_from_user('simonw') ORDER BY stars DESC LIMIT 3;datasette  9133llm  3556shot-scraper  1591Plain with headers
Section titled “Plain with headers”Similar to the plain text format, but with the column names as the first row.
.format plainheaderanyquery -q "SELECT * FROM table" --format plainheaderExample
anyquery> .mode plainheaderOutput mode set to plainheader
anyquery> SELECT name, stargazers_count as stars FROM github_repositories_from_user('simonw') ORDER BY stars DESC LIMIT 3;name  starsdatasette  9133llm  3556shot-scraper  1591Pretty
Section titled “Pretty”Export the result of a query into a nice ASCII table. This is the default one if stdout is a terminal.
.format prettyanyquery -q "SELECT * FROM table" --format prettyExample
anyquery> .format prettyOutput mode set to pretty
anyquery> SELECT name, stargazers_count as stars FROM github_repositories_from_user('simonw') ORDER BY stars DESC LIMIT 3;+--------------+-------+|     name     | stars |+--------------+-------+| datasette    |  9133 || llm          |  3556 || shot-scraper |  1591 |+--------------+-------+3 resultsExport the result of a query as a tab-separated values file (alias to plain with headers).
.format tsvanyquery -q "SELECT * FROM table" --format tsvExample
anyquery> .format tsvOutput mode set to tsv
anyquery> SELECT name, stargazers_count as stars FROM github_repositories_from_user('simonw') ORDER BY stars DESC LIMIT 3;name  starsdatasette  9133llm  3556shot-scraper  1591Ugly json
Section titled “Ugly json”Similar to the JSON format, but without any indentation.
.format uglyjsonanyquery -q "SELECT * FROM table" --format uglyjsonExample
anyquery> .format uglyjsonOutput mode set to uglyjson
anyquery> SELECT name, stargazers_count as stars FROM github_repositories_from_user('simonw') ORDER BY stars DESC LIMIT 3;[{"name":"datasette","stars":9133},{"name":"llm","stars":3556},{"name":"shot-scraper","stars":1591}]Missing format?
Section titled “Missing format?”If you need a format that is not supported, please open an issue to request it.
