Skip to main content

How to Rename a Result Column in SQLite with AS

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A result's headings are the column names, and a calculated column gets the calculation's text as its heading. AS gives a result column a name of your choice. This guide renames two book columns and names a calculation, with real output from SQLite 3.54.1.

Syntax​

Syntax
SELECT <expression> AS <name> FROM <table>;
PartWhat it means
<expression>A column, or a calculation such as pages > 400
AS <name>The name the result column gets. Put a name with a space in double quotes

The new name belongs to the result only: the table's column keeps its own. SQLite's documentation on result column names: "The name of a result column is the value of the "AS" clause for that column, if there is an AS clause. If there is no AS clause then the name of the column is unspecified and may change from one release of SQLite to the next."

Prerequisites​

  • The sqlite3 command-line shell. This guide uses SQLite 3.54.1. Run sqlite3 --version to see yours; the SQLite download page has the command-line tools for Windows, macOS and Linux.

Step 1: Write the Script​

Save this as column_alias.sql:

column_alias.sql
CREATE TABLE books (
title TEXT NOT NULL,
author TEXT NOT NULL,
pages INTEGER NOT NULL
);

INSERT INTO books (title, author, pages) VALUES ('Emma', 'Jane Austen', 474);
INSERT INTO books (title, author, pages) VALUES ('Persuasion', 'Jane Austen', 249);
INSERT INTO books (title, author, pages) VALUES ('Dune', 'Frank Herbert', 412);

SELECT title AS book, author AS written_by FROM books;
SELECT title, pages > 400 AS long_read FROM books;

The column_alias.sql script in a macOS-style code window: a books table with three books, a SELECT of title AS book and author AS written_by, and a SELECT of title and pages > 400 AS long_read.

  • title AS book and author AS written_by rename two columns in the result.
  • pages > 400 AS long_read names a calculated column: 1 when a book has more than 400 pages, 0 when it doesn't.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
sqlite3 -table library.db ".read column_alias.sql"

sqlite3 opens the database file library.db, and creates it when it isn't there. -table prints each result as a table, and .read runs the statements in the script. The command is the same in bash, zsh and PowerShell.

Output
+------------+---------------+
| book | written_by |
+------------+---------------+
| Emma | Jane Austen |
| Persuasion | Jane Austen |
| Dune | Frank Herbert |
+------------+---------------+
+------------+-----------+
| title | long_read |
+------------+-----------+
| Emma | 1 |
| Persuasion | 0 |
| Dune | 1 |
+------------+-----------+

The output in a terminal: the books under the headings book and written_by, then each title with a long_read column, 1 for Emma and Dune and 0 for Persuasion.

The first result has the headings book and written_by. The second SELECT reads title by its own name again, because AS renamed it only in the first result. long_read is 1 for Emma and Dune, and 0 for Persuasion, with its 249 pages.

Common Mistakes​

MistakeWhat happensDo this instead
A name with a space, as in AS book titlenear "title": syntax errorPut the name in double quotes: AS "book title" worked
No AS on a calculated columnThe heading was the calculation's text, pages > 400, and the documentation says a name without AS may change between releasesName every calculated column with AS

Troubleshooting​

SymptomLikely causeFix
near "<word>": syntax error just after an ASThe new name has a space in itPut the name in double quotes
Parse error near line 1 of column_alias.sql: table books already existsThe script already ran on library.dbDelete library.db and run the script again

FAQ​

Can I use the new name in WHERE and ORDER BY?​

Yes, SQLite accepts it in both: SELECT title, pages > 400 AS long_read FROM books WHERE long_read ORDER BY long_read, title returned Dune and Emma. The documentation on ORDER BY: "If the ORDER BY expression is an identifier that corresponds to the alias of one of the output columns, then the expression is considered an alias for that column."

Does AS change the column in the table?​

No. It names the column in that one result. The second SELECT in the script read title by its own name.

Conclusion​

AS gave the result columns the headings book and written_by, and named the pages > 400 calculation long_read.

Additional Resources​