C.W.K.
Stream
Lesson 01 of 10 · published

SELECT — Retrieving Data

~12 min · sql, select, querying

Level 0Scout
0 XP0/80 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

The shape of every read

Every query that reads from a SQLite database starts with SELECT. The minimal shape:

SELECT <columns> FROM <table> [WHERE <predicate>] [ORDER BY ...] [LIMIT N]

Things to internalize on day one:

  • SELECT * is fine for exploration but never in production code — it ties your application to whatever the schema happens to look like today.
  • Column expressions can be raw columns, expressions, function calls, or string concatenations.
  • AS creates an alias — a temporary name that's local to the query. Use it for derived columns and self-joins.
  • You can SELECT without any FROM at all: SELECT 1+1, SELECT datetime('now') — useful for one-shot checks.
Tip: Develop the muscle of explicit columns from day one. Every SELECT * in production code is a future bug — when someone adds a sensitive column or reorders the schema, your code breaks silently or leaks data.

Code

SELECT shapes·sql
-- All columns (exploration only)
SELECT * FROM users;

-- Specific columns
SELECT id, email, username FROM users;

-- Expressions and aliases
SELECT id, email,
       length(email)               AS email_length,
       upper(username)             AS uname_upper,
       email || ' (' || username || ')' AS display
FROM users;

-- No FROM at all
SELECT datetime('now') AS now_utc, 1 + 1 AS two;

External links

Exercise

Take any table you have (or create one with five rows) and write five SELECTs: (1) all columns, (2) two specific columns, (3) one expression with an alias, (4) one without a FROM, (5) one with two aliases that reference each other (e.g., compute price_with_tax then group by it). Note where SQLite lets you reuse aliases and where it doesn't.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.