Skip to content
Lesson 2 of 10

Step 1 of 5 · Reading · ~2 min

Learn

SELECT and Tables

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 / 23); 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:

  1. Applications and reporting tools read columns by name — one honest note about this course, though: the SQLite runner prints values only, one row per line with | between columns and no header row, so an alias never appears in graded output here. Alias for the reasons below, not to satisfy the grader.
  2. Later clauses can refer to themORDER BY millions reads better than repeating the expression.
  3. 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 poppopulation 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 ||. 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. And reach for ||, never +: SQLite reads + as arithmetic, so 'Hello, ' + 'SQL!' quietly evaluates both strings as numbers and prints 0.

Up nextCreating and Querying TablesSELECT and Tables

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…