Reading — step 1 of 5
Learn
Multiple Columns and Aliases
A SELECT produces a table — even when it's computing expressions — and this lesson is about controlling that table's shape: how many columns, computed how, named what. Naming, it turns out, is not cosmetic.
Multiple values per row
Separate expressions with commas; each becomes a column:
SELECT 'Ada', 1815, 2 * 18;
-- one row: | Ada | 1815 | 36 |
Order matters — columns come out in the order you asked. From a table, the same idea selects specific columns:
SELECT name, population FROM cities;
You'll also meet SELECT * — "every column." Fine for exploring; frowned on in real code (schemas change; * silently changes with them). Name what you need.
Expressions on columns
Any column can be computed from others — arithmetic, functions, concatenation:
SELECT name,
population / 1000000, -- millions (integer division! see below)
UPPER(country)
FROM cities;
The scalar-function starter pack: UPPER(s) / LOWER(s), LENGTH(s), ROUND(x, digits), ABS(x), and || for concatenation. One arithmetic honesty note: dividing two integers gives an integer in SQLite (7 / 2 → 3); write population / 1000000.0 when you want decimals — the same truncation trap as every other language in this catalog.
Aliases: AS
Computed columns get ugly automatic names (population / 1000000.0 becomes the literal expression text). AS names them:
SELECT name,
population / 1000000.0 AS millions,
'City: ' || name AS label
FROM cities;
Three reasons aliases matter beyond looks:
- Graders check column names on many exercises —
AS greetingisn't optional if the expected output saysgreeting. - Later clauses can refer to them —
ORDER BY millionsreads better than repeating the expression. - Applications read columns by name — the alias is the API you're giving whatever code runs the query.
The AS keyword itself is optional (population AS pop ≡ population pop) but write it — the explicit form survives code review everywhere. Aliases with spaces need double quotes (AS "in millions") — remember, double quotes are for identifiers.
Your exercise: Build a Greeting
Produce a greeting assembled from pieces with ||, under the exact column name the spec shows. The assembly line: string literals in single quotes, || between parts, AS to name the result. Spacing bugs live inside your quoted literals ('Hello,' || name vs 'Hello, ' || name) — when the output is one space off, the space is missing from a literal, not from SQL.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…