Menu
CoddyTech

Calculate and Name Columns

Part of the First Queries section of Coddy's PostgreSQL journey. Lesson 11 of 20.

A SELECT expression computes a value for each row. Multiplication uses *. AS gives a result column a readable name; it does not rename the source column.

Input table products:

idnamecategorypricestockactivenote
1Maptravel125TrueNULL
2Notebookbooks80TrueRecycled
3Atlasbooks243FalseNULL
4Mughome128TrueBlue
5lamphome302FalseClearance
6Mini maptravel610TrueNULL
SELECT id, price * 2 AS pair_price FROM products ORDER BY id;

For Map, two units cost 24, so pair_price is 24. The expression is calculated independently for each product. A column alias describes the result without changing the stored price.

Calculate in the SELECT list and name expressions with AS.

challenge icon

Challenge

Easy

Return id and inventory_value, defined as price times stock, for every product. Order by id.

The input tables are shown in the test panel. Each run starts from fresh data. Return the named columns in the stated order; do not hard-code the displayed rows.

Try it yourself

-- Write your SQL here.
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in First Queries

Practice on your own: PostgreSQL playground