How to Give a Table a Short Name in SQLite with AS
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
SELECT <alias>.<column>
FROM <table> AS <alias>;
| Part | What 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 --versionto 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:
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;

books AS bandauthors AS agive the two tables one-letter names.b.title,a.nameand theONcondition use those names.
Step 2: Run It
In the folder that holds the script, run:
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.
+------------+---------------+
| title | name |
+------------+---------------+
| Emma | Jane Austen |
| Persuasion | Jane Austen |
| Dune | 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
| Mistake | What happens | Do this instead |
|---|---|---|
The table's own name after giving it an alias: books.title with FROM books AS b | no such column: books.title | Once 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 only | Give the table its alias again in each statement that uses it |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
ambiguous column name: author_id | Both tables have author_id, and the column has no alias in front | Write b.author_id or a.author_id |
Parse error near line 1 of table_alias.sql: table authors already exists | The script already ran on library.db | Delete 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
- SELECT — where a table alias goes in the
FROMclause. - FROM clause processing — how SQLite joins the tables you name.
