Dataslope logoDataslope

Inserting Data in SQLite

Add one row or many rows to SQLite tables with INSERT INTO and then SELECT the results.

A table's structure is only the beginning. To make a table useful, you add rows.

The SQL statement for adding rows is INSERT INTO.

INSERT adds rows

An INSERT statement names the table, names the columns you are filling, and supplies matching values.

Here is a complete example: create a table, insert one row, then read it back.

SQL
SQLite 3.53

SQLite does not have a separate BOOLEAN type. In beginner examples, we often use INTEGER values 0 and 1 for yes/no data: 0 means false or no, and 1 means true or yes.

Omit the auto id column

When a table has id INTEGER PRIMARY KEY, you usually leave id out of the insert. SQLite assigns it for you.

SQL
SQLite 3.53

The insert only gave title. SQLite supplied id, and DEFAULT 0 supplied done.

QuestionSelect one

Why can the insert omit id in a table with id INTEGER PRIMARY KEY?

SQLite does not store ids.

SQLite can automatically assign the id value.

The id column becomes text instead.

The row is inserted without any columns.

Insert several rows at once

You can add multiple rows with one INSERT by separating value groups with commas.

SQL
SQLite 3.53

Each parenthesized group becomes one new row.

Column order matters

The column list and values match by position. The first value goes into the first named column, the second value goes into the second named column, and so on.

SQL
SQLite 3.53
QuestionSelect one

In INSERT INTO contacts (name, email, age) VALUES ('Maya', 'maya@example.com', 31);, where does 31 go?

Into name

Into email

Into age

Into id

Insert with explicit columns

You do not have to fill every column every time. Name the columns you are providing, and let defaults or empty values handle the rest.

SQL
SQLite 3.53

The first row has no page count, so pages uses its default 0. The second row has no author, so that value is empty in the result.

Practice challenge

SQL Challenge
SQLite 3.53
Insert two pantry items

The pantry table is already created. Insert two rows:

  • 'Rice' with quantity 3
  • 'Beans' with quantity 5

Then select name and quantity from pantry ordered by id.

Check your understanding

QuestionSelect one

What does INSERT INTO do?

It reads rows from a table.

It adds new rows to a table.

It deletes a table's structure.

It renames every column.

QuestionSelect one

How do you insert multiple rows in one INSERT statement?

Write several table names after INSERT INTO.

Put every value in one long quoted string.

Add several parenthesized value groups separated by commas.

Use SELECT * before VALUES.

QuestionSelect one

In this course's SQLite examples, what do 0 and 1 often represent in yes/no columns?

Table names and column names.

False/no and true/yes values.

Text and dates.

Primary keys only.

On this page