The DuckDB command line interface (CLI) provides us with the ability to output query results in various formats. One of these formatting options is HTML.
This article shows you how to output your DuckDB query results as HTML tables, which can be useful when you need to include the results in web pages or documentation.
Checking the Current Mode
Here are two ways to check your current output mode.
The easiest way to check your current output mode is by using the .mode
command:
.mode
Example output:
current output mode: table
My current output mode is table
, which formats the results as a table using ASCII characters.
Another way to check the current output mode is by running a sample query to see the output format:
CREATE TABLE products(id INTEGER, name VARCHAR, price DECIMAL);
INSERT INTO products VALUES
(1, 'Widget', 19.99),
(2, 'Gadget', 24.99);
SELECT * FROM products;
Output:
+----+--------+--------+
| id | name | price |
+----+--------+--------+
| 1 | Widget | 19.990 |
| 2 | Gadget | 24.990 |
+----+--------+--------+
We can see that the results are formatted as a table, but only as ASCII-art (not an HTML table).
Changing to HTML Mode
To switch to HTML output mode, use:
.mode html
Verifying the Change
Check the mode setting:
.mode
This will confirm that HTML mode is active:
current output mode: html
The mode is html
as expected.
Now let’s run a query to see the HTML output:
SELECT * FROM products;
The output will now be formatted as an HTML table:
<tr><th>id</th>
<th>name</th>
<th>price</th>
</tr>
<tr><td>1</td>
<td>Widget</td>
<td>19.990</td>
</tr>
<tr><td>2</td>
<td>Gadget</td>
<td>24.990</td>
</tr>
The output has been returned in HTML. This is the HTML source code for a table. When the HTML code is run in a web browser, it is rendered as a table instead of the above markup code.
Notice that this is not a complete HTML table. HTML tables are defined with the <table>
element (i.e., they start with <table>
and end with </table>
), with each row defined by the <tr>
element, which in turn can have other elements to define each column (such as <th>
for table header, and <td>
for table data).
So the assumption is that you would need to insert the above output in between <table>
and </table>
tags, so that it looks something like this:
<table>
<tr><th>id</th>
<th>name</th>
<th>price</th>
</tr>
<tr><td>1</td>
<td>Widget</td>
<td>19.990</td>
</tr>
<tr><td>2</td>
<td>Gadget</td>
<td>24.990</td>
</tr>
</table>
You could also apply other attributes to the HTML elements, such as a style
attribute to apply styles, etc. Anything like that would depend on your requirements (or the requirements of the business).
Reverting to the Previous Mode
To return to the previous output mode (or another of your choosing):
.mode duckbox
That changed the output mode to duckbox
, which is the default mode in DuckDB.
Listing All Available Output Modes
To see all available output modes in DuckDB CLI:
.help mode
Output:
.mode MODE ?TABLE? Set output mode
MODE is one of:
ascii Columns/rows delimited by 0x1F and 0x1E
box Tables using unicode box-drawing characters
csv Comma-separated values
column Output in columns. (See .width)
duckbox Tables with extensive features
html HTML <table> code
insert SQL insert statements for TABLE
json Results in a JSON array
jsonlines Results in a NDJSON
latex LaTeX tabular environment code
line One value per line
list Values delimited by "|"
markdown Markdown table format
quote Escape answers as for SQL
table ASCII-art table
tabs Tab-separated values
tcl TCL list elements
trash No output
Each mode has its own use cases, for example:
html
for web integrationcsv
for spreadsheet compatibilityjson
for API responsesmarkdown
for documentationinsert
for generating SQLINSERT
statementsndjson
for streaming data processing
The HTML mode is particularly useful when you need to:
- Generate content for web pages
- Create HTML email reports
- Produce documentation with formatted tables
- Share query results that need to be embedded in web-based platforms