Skip to main content

How to Read Rows from a SQLite Table with SELECT

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

SELECT reads rows from a table. SELECT * returns every column, and a list of column names returns only those. This guide reads a table of books both ways, with real output from SQLite 3.54.1.

Syntax​

Syntax
SELECT * FROM <table>;
SELECT <column 1>, <column 2> FROM <table>;
PartWhat it means
SELECT *Every column, in the table's order
SELECT <column 1>, <column 2>Only the columns you list, in the order you list them, separated by commas
FROM <table>The table to read

SQLite's documentation on SELECT: "The SELECT statement is used to query the database. The result of a SELECT is zero or more rows of data where each row has a fixed number of columns."

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

select_rows.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 * FROM books;
SELECT title, pages FROM books;

The select_rows.sql script in a macOS-style code window: a books table with title, author and pages, three books, Emma, Persuasion and Dune, then SELECT * FROM books and SELECT title, pages FROM books.

  • The table holds three books.
  • SELECT * FROM books asks for every column.
  • SELECT title, pages FROM books asks for two of them.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
sqlite3 -table library.db ".read select_rows.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 | author | pages |
+------------+---------------+-------+
| Emma | Jane Austen | 474 |
| Persuasion | Jane Austen | 249 |
| Dune | Frank Herbert | 412 |
+------------+---------------+-------+
+------------+-------+
| title | pages |
+------------+-------+
| Emma | 474 |
| Persuasion | 249 |
| Dune | 412 |
+------------+-------+

The output in a terminal: the first result shows title, author and pages for Emma, Persuasion and Dune, and the second shows only their title and pages.

The first result has all three columns, the second only title and pages. Both hold every row: without a WHERE, a SELECT returns the whole table.

Common Mistakes​

MistakeWhat happensDo this instead
A comma after the last column, SELECT title, pages, FROM booksnear "FROM": syntax errorNo comma before FROM
A misspelled column, SELECT titel FROM booksno such column: titelCheck the column names with .schema books

Troubleshooting​

SymptomLikely causeFix
no such table: bookzThe table name is misspelled, or the table is in another database fileList the tables with .tables, which printed books here
Parse error near line 1 of select_rows.sql: table books already existsThe script already ran on library.dbDelete library.db and run the script again

FAQ​

Can SELECT work without a table?​

Yes. Without FROM, it works out its expressions once: SELECT 2 + 3; returned 5.

In what order do the rows come back?​

Without ORDER BY, there's no promised order. The documentation: "If a SELECT statement that returns more than one row does not have an ORDER BY clause, the order in which the rows are returned is undefined."

Conclusion​

SELECT * FROM books returned every column of the three books, and SELECT title, pages FROM books returned only the two that were named.

Additional Resources​