Your First SQLite Queries
Run your first SQLite statements, compute simple values, and read rows from a tiny table.
Welcome to your first hands-on SQLite page. You do not need to install anything yet: every runnable block on this page runs in your browser.
One important habit: each block is independent. If one block creates a table, the next block does not remember it. When a block needs a table that is already prepared for you, the hidden setup lives in initSql; the query you see is the part you practice.
SQL can give back a value
The keyword SELECT means: compute something and show me the result. You can use it even before you have any tables.
The result is a tiny table: one row and one column. The value is text, so it is wrapped in single quotes.
SQLite can do simple arithmetic
SQLite can also act like a calculator. Arithmetic expressions go after SELECT.
Read that statement as: select four computed values, and label the output columns sum, difference, product, and quotient.
In SELECT 2 + 2 AS sum;, what does AS sum do?
It changes the arithmetic answer.
It gives the result column a friendly name.
It creates a table named sum.
How to read a simple statement
A SQL statement is one complete instruction. We end statements with a semicolon ; so SQLite knows the instruction is finished.
For now, the most common shape is:
SELECT value_or_column AS output_name;Later, when you read stored data, you add FROM table_name to say where the rows come from.
Create a tiny table and read it back
Tables are where databases become useful. A table is like a labeled grid: columns describe what each piece of data means, and rows hold individual records.
This block creates a small table, inserts two rows, and reads the rows back.
Read the final query like a sentence: select the name and species columns from the pets table.
Reading from a prepared table
Sometimes the setup is hidden so you can focus on the query. The block below starts with a fresh snacks table each time you run it.
Try replacing name, calories with *. In SQL, * means "all columns."
Which part of SELECT name, calories FROM snacks; tells SQLite which table to read?
SELECT
FROM snacks
name, calories
The semicolon
Check your understanding
What does SELECT do in these beginner queries?
It permanently saves new rows.
It asks SQLite to return values, calculations, or columns.
It deletes a table.
It turns text into numbers automatically.
Why do we usually put a semicolon at the end of a SQL statement?
It makes the result table sort itself.
It changes text into a column name.
It marks the end of one complete instruction.
It is only used in one database system.
In this course, why can one runnable block not rely on a table created in a previous block?
Each block runs independently with its own fresh setup.
SQLite does not support tables.
SELECT removes tables after it runs.
Text values prevent tables from being reused.