Skip to main content

SELECT Statement

The SELECT statement is the foundation of querying.

You will use it constantly to inspect data, validate updates, and power application features.

Core Concepts

flowchart LR
A[SELECT columns] --> B[FROM table]
B --> C[Optional filters/sort/limit]
C --> D[Result rows]
ClausePurposeRequired
SELECTChoose columns or expressionsYes
FROMChoose source tableYes (for table queries)
WHEREFilter rowsNo
ORDER BYSort resultsNo
LIMITRestrict row countNo

Code Examples

-- Setup sample table.
CREATE TABLE employees (
employee_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
role TEXT NOT NULL
);

INSERT INTO employees (name, role)
VALUES
('Ada', 'Engineer'),
('Alan', 'Researcher');
Expected output
Table created and two rows inserted.
-- Basic SELECT query.
SELECT employee_id, name, role
FROM employees;
employee_idnamerole
1AdaEngineer
2AlanResearcher

SQLite-Specific Nuances

SQLite Nuance

A SELECT query in SQLite can also run without a table (for constants and expressions), for example SELECT 2 + 2;.

Common Pitfalls / Best Practices

Pitfall

Using SELECT * by default in long-term code.

Schema changes can alter output shape unexpectedly.

Best Practice

Explicitly list columns in SELECT for predictable, readable queries.

Quick Challenge

Write a query that returns only name and role from employees.

View Solution
SELECT name, role
FROM employees;
namerole
AdaEngineer
AlanResearcher