sqlcmd.net validated sql reference
beginner selecting MySQL MariaDB SQL Server PostgreSQL SQLite

Select Computed Columns With Expressions

Include arithmetic, string, or conditional expressions directly in a `SELECT` list and give them an alias with `AS`.

Docker-validated Not currently validation-green

Compute the total stock value for each inventory item

price * quantity is evaluated per row and named total_value with AS. The alias can then be used in ORDER BY to sort by the computed column. Cherries (5 × 30 = 150) rank first, then Apples (2 × 50 = 100), then Bananas (1 × 80 = 80).

Rows loaded before the example query runs.
Setup
CREATE TABLE inventory (id INT, name VARCHAR(50), price INT, quantity INT);

INSERT INTO
  inventory
VALUES
  (1, 'Apples', 2, 50),
  (2, 'Bananas', 1, 80),
  (3, 'Cherries', 5, 30);
Shared across supported engines.
SQL
SELECT
  name,
  price,
  quantity,
  price * quantity AS total_value
FROM
  inventory
ORDER BY
  total_value DESC;
Returned rows for the shared example.
namepricequantitytotal_value
Cherries530150
Apples250100
Bananas18080

Identical syntax and result across all engines.

Where this command helps.

  • computing a derived metric like total price or profit margin per row
  • formatting or transforming values at query time without changing stored data

What the command is doing.

Any valid expression can appear in a SELECT list alongside plain column references. Arithmetic operators (+, -, *, /), string functions, CASE expressions, and calls to built-in functions all produce new columns in the result without modifying the underlying table. Giving the computed column a name with AS makes the output readable and lets the alias be referenced in ORDER BY. Note that most databases do not allow a SELECT alias to appear in the same query's WHERE clause — use a subquery or CTE if you need to filter on a computed value.