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]
| Clause | Purpose | Required |
|---|---|---|
SELECT | Choose columns or expressions | Yes |
FROM | Choose source table | Yes (for table queries) |
WHERE | Filter rows | No |
ORDER BY | Sort results | No |
LIMIT | Restrict row count | No |
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_id | name | role |
|---|---|---|
| 1 | Ada | Engineer |
| 2 | Alan | Researcher |
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;
| name | role |
|---|---|
| Ada | Engineer |
| Alan | Researcher |