Home › Learn › SQL › Shaping results

Expressions and aliases

Lesson 10 of 26. Calculate new columns and name them.

The select list can hold expressions, not just column names: price * stock computes a value per row. AS inventory_value names the new column; without an alias the column is called by the expression text, which is ugly and awkward to refer to. Aliases work for plain columns too, name AS product.

Text joins with ||: first || ' ' || last. Functions like ROUND(x, 2), UPPER(text), and LENGTH(text) can be used anywhere an expression can.

SELECT name, salary * 12 AS yearly FROM employees;

Your task

Show each product's id, name, and the value of its stock, price * stock, as a column called inventory_value.

Part of the final project. product_values is the view of each product with the value of its remaining stock. It is saved in this browser and loaded into Project: the order report when you get there.

Hint 1

price * stock AS inventory_value

Show the solution
SELECT id, name, price * stock AS inventory_value FROM products;

    Runs SQLite compiled to WebAssembly in this tab; the database is rebuilt before every run.