Skip to main content

How to Give a Table a Short Name in SQLite with AS

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

In a join, every column is often written with its table's name, as in books.title and authors.name, and the statement gets long. A table alias gives a table a short name for one statement. This guide joins books to their authors as b and a, with real output from SQLite 3.54.1.

Syntax​

Syntax
SELECT <alias>.<column>
FROM <table> AS <alias>;
PartWhat it means
<table> AS <alias>Gives the table a second, usually shorter name for this one statement
<alias>.<column>A column of that table, named through its alias

In SQLite's SELECT syntax, the alias follows the table's name in the FROM clause, as table-name AS table-alias.

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 table_alias.sql:

table_alias.sql
CREATE TABLE authors (
author_id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);

CREATE TABLE books (
title TEXT NOT NULL,
author_id INTEGER NOT NULL
);

INSERT INTO authors (author_id, name) VALUES (1, 'Jane Austen');
INSERT INTO authors (author_id, name) VALUES (2, 'Frank Herbert');
INSERT INTO books (title, author_id) VALUES ('Emma', 1);
INSERT INTO books (title, author_id) VALUES ('Persuasion', 1);
INSERT INTO books (title, author_id) VALUES ('Dune', 2);

SELECT b.title, a.name
FROM books AS b
JOIN authors AS a ON a.author_id = b.author_id;

The table_alias.sql script in a macOS-style code window: an authors table and a books table with two authors and three books, then a SELECT of b.title and a.name FROM books AS b JOIN authors AS a ON a.author_id = b.author_id.

  • books AS b and authors AS a give the two tables one-letter names.
  • b.title, a.name and the ON condition use those names.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
sqlite3 -table library.db ".read table_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
+------------+---------------+
| title | name |
+------------+---------------+
| Emma | Jane Austen |
| Persuasion | Jane Austen |
| Dune | Frank Herbert |
+------------+---------------+

The output in a terminal: Emma and Persuasion next to Jane Austen, and Dune next to Frank Herbert.

Each book came back with its author, the same result as with the full table names. Only the statement got shorter, and the headings are still the column names, title and name.

Common Mistakes​

MistakeWhat happensDo this instead
The table's own name after giving it an alias: books.title with FROM books AS bno such column: books.titleOnce a table has an alias, use the alias everywhere in that statement
An alias from an earlier statement, as in SELECT b.title FROM books;no such column: b.title: an alias lasts for one statement onlyGive the table its alias again in each statement that uses it

Troubleshooting​

SymptomLikely causeFix
ambiguous column name: author_idBoth tables have author_id, and the column has no alias in frontWrite b.author_id or a.author_id
Parse error near line 1 of table_alias.sql: table authors already existsThe script already ran on library.dbDelete library.db and run the script again

FAQ​

Is the word AS required?​

No. FROM books b JOIN authors a ON a.author_id = b.author_id returned the same three rows.

Does an alias rename the table?​

No. It's a name for one statement only, and the table keeps its own name in the database, as the second mistake above shows.

Conclusion​

FROM books AS b JOIN authors AS a gave the two tables short names, and the join returned the same books and authors with less typing.

Additional Resources​