typestar

Computed columns in SQL

A SELECT list holds expressions, not just column names.

SELECT
    sku,
    price,
    ROUND(price * 1.2, 2) AS price_with_tax,
    name || ' (' || sku || ')' AS label,
    'catalog' AS source
FROM products;

How it works

  1. Arithmetic works on columns; ROUND takes the number of decimals.
  2. || concatenates strings — SQL's + is only ever addition.
  3. A literal column is legal and useful for tagging rows.

Keywords and builtins used here

The run, in numbers

Lines
7
Characters to type
131
Tokens
37
Three-star pace
70 tpm

At the three-star pace of 70 tokens a minute, this run takes about 32 seconds.

Type this snippet

Step 3 of 5 in Selecting rows, step 3 of 23 in Language basics.

← Previous Next →