Before you start, you need
Create a calculated column with a clear name
Sometimes you need a figure that is not stored in the table. If a product costs 10 and there are 3 units left, you can work out that the stock is worth 30. In SQL, price * stock does that multiplication for each product.
You can write calculations with +, -, * and / in SELECT. The result appears as a new column. Before calculating, decide what the figure represents: the value of the stock is not the same as the amount of past sales.
An alias is a name for a result column. Write AS inventory_value after the calculation to give it that heading. It also works for renaming a visible column: name AS product.
The calculation and the alias affect only the answer. The original values are kept: the price, the stock and the column name in the table do not change.
SELECT expression AS result_name
FROM table_name;See it in action
What is the stock of each product worth at its current price?
SELECT name,
price * stock AS inventory_value
FROM products;| name | inventory_value |
|---|---|
| Mouse | 448.2 |
| Keyboard | 346.5 |
| Monitor 24 | 716 |
| Pro Laptop | 0 |
| Headphones | 768.9 |
| Monitor 27 | 498 |
| SSD Drive | 801 |
| Cable USB-C | 360 |
- For each product, its price is multiplied by the units available. Mouse costs 24.90 and has 18 units: 24.90 × 18 = 448.20.
AS inventory_valuegives the calculated figure a name. That heading explains what the result means.- The stored price and stock stay the same. This query calculates an answer; it does not update the inventory.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
First, give it a name
Show name with the alias product, and price without renaming it.
Expected result: 9 rows with the columns product, price, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
Change only the heading for name, keeping the values.
Use AS on the column that needs a different name.
Write name AS product and select price as well.
Solution explained
SELECT name AS product, price FROM products;The alias changes a heading, without transforming the name or the price.
Calculate double the stock
Guided practice
Calculate double the stock
Write a query that shows each product's name and double its stock in a column called double_stock.
Expected result: 9 rows with the columns name, double_stock, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
Calculate double the stock for each product and give the result a name.
Add AS after the expression.
The required alias is double_stock.
Solution explained
SELECT name, stock * 2 AS double_stock FROM products;Each row keeps its product and adds a value calculated from the stock.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Next, calculate a price with VAT
Show name, price and price multiplied by 1.21 with the alias price_with_vat.
Expected result: 9 rows with columns name, price, price_with_vat, in that order. The order of the rows does not matter.
View tables and data
Open this panel to see the full data.
A 21% VAT is the same as multiplying the price by 1.21. In SQL, decimals use a point: 1,21 would be read as two columns.
Write the multiplication as an expression inside SELECT, after price.
Add AS price_with_vat to the expression price * 1.21.
Solution explained
SELECT name, price, price * 1.21 AS price_with_vat FROM products;Each product gets its own calculation: Mouse goes from 24.90 to 30.129. The price column still shows the stored value, without VAT.
Key takeaways
- An expression calculates one value per row.
- AS names a column of the result.
- Calculating in SELECT leaves the original data unchanged.
Next: Filter rows with WHERE