Selecting Specific Columns in SQL

A select list defines which values each result row exposes, often with aliases describing computed or renamed fields.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace a catalogue projection
  2. Keep result names stable
  3. Name the round-trip output fields precisely
  4. Confirm contents, aliases and grain

Trace a catalogue projection

A table can hold ID, title, private notes, and publication date. A public list can select only the identifying and display fields. Minimal data boundaries explain why this can be a deliberate contract.

Keep result names stable

A query can rename a computed value for the consumer. Name its unit when relevant so later code knows how to display it. See database units.[1]

Name the round-trip output fields precisely

A table of catalogue items can contain ID, title and an internal note. A public listing selects ID and title explicitly. A client decoding two named columns has a more stable interface when someone adds an unrelated database column later. See rows and columns.

WITH items(id, title, note) AS (VALUES (7, 'Bridge', 'draft source'))
SELECT id, title AS display_title FROM items;
-- Expected columns: id, display_title. Row: 7, Bridge.

Confirm contents, aliases and grain

Selecting fewer fields does not change which source rows qualify. Renaming a result alias can still affect a consumer. Test an extra source column and use the returned field names in the check.

References

  1. PostgreSQL Global Development Group, Queries ↩