Module 1 · Selecting data

Use aliases and create calculated columns

By the end you will be able to calculate a column and give it a clear name.

About 12 min

0 of 52 lessons

Before you start, you need

Choose the order of the columns

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.

Minimal syntax
SELECT expression AS result_name
FROM table_name;

See it in action

What is the stock of each product worth at its current price?

SQL
SELECT name,
       price * stock AS inventory_value
FROM products;
Example result: 8 of 9 rows
nameinventory_value
Mouse448.2
Keyboard346.5
Monitor 24716
Pro Laptop0
Headphones768.9
Monitor 27498
SSD Drive801
Cable USB-C360
  1. For each product, its price is multiplied by the units available. Mouse costs 24.90 and has 18 units: 24.90 × 18 = 448.20.
  2. AS inventory_value gives the calculated figure a name. That heading explains what the result means.
  3. 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

Not started

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.

Calculate double the stock

Guided practice

Calculate double the stock

Not started

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.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Next, calculate a price with VAT

Not started

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.

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