runtime
02 / 88

lesson 02 SQL foundations · beginner

Aliases & calculated columns

Name your results and calculate values from existing columns.

the idea

AS gives an output column a readable name. You can calculate values for each row with arithmetic. The original table stays unchanged.

tables in this lesson

The challenges use this data. Each column shows its type underneath; → marks a link to another table.

products4 columns · 6 rows
idinteger · keynametextcategorytextpricereal
1NotebookStationery12
2Desk lampHome45
3KeyboardTech80
4Pen setStationery8
5HeadphonesTech120

showing 5 of 6 rows

orders6 columns · 12 rows
idinteger · keycustomer_idintegerproduct_idintegerquantityintegerstatustextordered_attext
101112paid2025-01-01
102231paid2025-01-02
103121paid2025-01-05
104352pending2025-01-08
105445paid2025-02-01

showing 5 of 12 rows

example

The idea in one query. Press Run to see what it returns; running examples never affects your progress.

example.sql
SELECT name, price * 2 AS double_price
FROM products;

Watch for: AS names the result. It does not update the stored column. A quoted number is text, so use a numeric expression for arithmetic.

build it step by step

Each step changes one thing. Run them in order and watch the result change.

  1. Name a calculated column. Read the stored price first. The next step multiplies it and names the result without changing the table.

    step-1.sql
    SELECT name, price
    FROM products;
  2. Run the reference. AS gives an output column a readable name. You can calculate values for each row with arithmetic. The original table stays unchanged.

    step-2.sql
    SELECT name, price * 2 AS double_price
    FROM products;

challenge 1

Return name and price * 2 as double_price for every product.

Return namedouble_priceany row order

challenge-1.sql
 
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.