SQL
4 min read
BEGINNER
Computed columns
Quick AnswerA computed result column evaluates an expression for each row. Use it for calculations such as a two-unit price, without changing the original price.
Core idea
A computed result column evaluates an expression for each row. Use it for calculations such as a two-unit price, without changing the original price.
Minimal example
sql
SELECT name, price_cents * 2 AS pair_price_cents FROM products ORDER BY id;Expected rows (in order):
text
Notebook | 700Pen | 300Bag | 4800Pencil | 200How it works
AS labels the result of an expression. Keep units explicit in the alias. Integer cents avoid fractional currency in these examples; division, rounding and overflow need separate treatment. A computed result column is not a stored generated column.
Practical example
sql
SELECT name, price_cents + 50 AS delivery_total_cents FROM products ORDER BY id;Expected rows (in order):
text
Notebook | 400Pen | 200Bag | 2450Pencil | 150Common mistake and practice
Do not interpret the alias as an update to the original column. Calculate the price of three units and give the result a descriptive alias.