Include Column Names in SQLite Query Results

By default, the SQLite command line interface displays query results using a pipe-separated list, without column headers.

This might be fine if you’re using the results in an application that doesn’t need the column names, but if you’re just a human, it can make it hard to read the data, especially if there are lots of columns.

Fortunately, there’s a quick way to present the column names in your query results.

Actually, there are at least a couple of ways to do this, and I explain them in this article.

The .headers Command

The most obvious way to display column headers in your query results is with the .headers command. This accepts one parameter, and the value of that parameter must be either on or off.

Pretty simple really. Therefore, to enable column headers, simply use .headers on.

Here’s an example of doing that, then running a query:

.headers on
SELECT * FROM Products;

Result:

ProductId|ProductName|Price
1|Blue Widgets (6 Pack)|389.45
2|Widget Holder|139.5
3|Widget Opener|89.27
4|Foobar Set|120.0

As mentioned, you can disable column headers using .headers off.

Here’s the same query, but with column headers disabled:

.headers off
SELECT * FROM Products;

Result:

1|Blue Widgets (6 Pack)|389.45
2|Widget Holder|139.5
3|Widget Opener|89.27
4|Foobar Set|120.0

If you also want to display the results in columns, see How to Display SQLite Results in Columns.

Line Mode

The SQLite command line shell also allows you to use “line” mode. When you do this, the query results are displayed vertically, so that each column is listed on a new line. When you use this mode, the column names are also displayed, using the format column_name = value.

Here’s an example:

.headers off
.mode line
SELECT * FROM Products;

Result:

  ProductId = 1
ProductName = Blue Widgets (6 Pack)
      Price = 389.45


  ProductId = 2
ProductName = Widget Holder
      Price = 139.5


  ProductId = 3
ProductName = Widget Opener
      Price = 89.27


  ProductId = 4
ProductName = Foobar Set
      Price = 120.0

Notice that I explicitly disabled column headers, but they were still printed out – using .mode line displays the column headers anyway. Probably not such a bad thing. Imagine how hard it would be trying to decipher which column each line represented if we couldn’t see the column names.