runtime
02 / 88

lesson 02 SQL foundations · beginner

Aliases & calculated columns

Name your results and calculate values from existing columns.

the idea

A SELECT item can be a calculation as well as a column. AS gives the result column a name. Arithmetic on a NULL value produces NULL, so a product with no list price has no calculated price either.

Dataset: Retail

The tables the challenges use, with their first 6 rows. Under each column name: the kind of value it holds, and which table it links to.

products6 columns · 5,304 rows
stock_codetext · unique iddescriptiontextcategory_idnumber · links to categorieslist_pricedecimalfirst_sold_atdate & timeis_merchandisenumber
10002INFLATABLE POLITICAL GLOBE170.852009-12-01 09:08:001
10002RROBOT PENCIL SHARPNER85.172009-12-02 14:43:001
10080GROOVY CACTUS INFLATABLE170.392009-12-02 16:02:001
10109BENDY COLOUR PENCILS80.422009-12-03 12:31:001
10120DOGGY RUBBER170.212009-12-01 16:17:001
10123CHEARTS WRAPPING TAPE80.652009-12-01 14:28:001
stock_codetext · unique id
10002
descriptiontext
INFLATABLE POLITICAL GLOBE
category_idnumber · links to categories
17
list_pricedecimal
0.85
first_sold_atdate & time
2009-12-01 09:08:00
is_merchandisenumber
1
stock_codetext · unique id
10002R
descriptiontext
ROBOT PENCIL SHARPNER
category_idnumber · links to categories
8
list_pricedecimal
5.17
first_sold_atdate & time
2009-12-02 14:43:00
is_merchandisenumber
1
stock_codetext · unique id
10080
descriptiontext
GROOVY CACTUS INFLATABLE
category_idnumber · links to categories
17
list_pricedecimal
0.39
first_sold_atdate & time
2009-12-02 16:02:00
is_merchandisenumber
1
stock_codetext · unique id
10109
descriptiontext
BENDY COLOUR PENCILS
category_idnumber · links to categories
8
list_pricedecimal
0.42
first_sold_atdate & time
2009-12-03 12:31:00
is_merchandisenumber
1
stock_codetext · unique id
10120
descriptiontext
DOGGY RUBBER
category_idnumber · links to categories
17
list_pricedecimal
0.21
first_sold_atdate & time
2009-12-01 16:17:00
is_merchandisenumber
1
stock_codetext · unique id
10123C
descriptiontext
HEARTS WRAPPING TAPE
category_idnumber · links to categories
8
list_pricedecimal
0.65
first_sold_atdate & time
2009-12-01 14:28:00
is_merchandisenumber
1

first 6 of 5,304 rows · your queries use all of them

customers4 columns · 848 rows
customer_idnumber · unique idcountrytextregiontextfirst_order_atdate & time
12347IcelandEurope2010-10-31 14:20:00
12353BahrainRest of World2010-10-27 12:44:00
12359CyprusEurope2009-12-05 13:32:00
12373AustriaEurope2010-02-04 12:08:00
12374AustriaEurope2010-04-30 14:01:00
12384SwitzerlandEurope2011-08-10 11:23:00
customer_idnumber · unique id
12347
countrytext
Iceland
regiontext
Europe
first_order_atdate & time
2010-10-31 14:20:00
customer_idnumber · unique id
12353
countrytext
Bahrain
regiontext
Rest of World
first_order_atdate & time
2010-10-27 12:44:00
customer_idnumber · unique id
12359
countrytext
Cyprus
regiontext
Europe
first_order_atdate & time
2009-12-05 13:32:00
customer_idnumber · unique id
12373
countrytext
Austria
regiontext
Europe
first_order_atdate & time
2010-02-04 12:08:00
customer_idnumber · unique id
12374
countrytext
Austria
regiontext
Europe
first_order_atdate & time
2010-04-30 14:01:00
customer_idnumber · unique id
12384
countrytext
Switzerland
regiontext
Europe
first_order_atdate & time
2011-08-10 11:23:00

first 6 of 848 rows · your queries use all of them

worked example

Run these steps in order to see the idea before you write your own query. The step marked “the pattern” is what the challenges ask for. Running the example does not count toward progress.

  1. See products that have a price. list_price is the unit selling price. Some products have no price recorded (NULL); this step skips those so you see real numbers first.

    step-1.sql
    SELECT stock_code, description, list_price
    FROM products
    WHERE list_price IS NOT NULL
    LIMIT 10;
  2. Calculate and name a column. Multiply the price, then use AS to name the new column. The challenges ask for every product—including missing prices, where the result stays NULL. That is normal, not a bug.the pattern

    example.sql
    SELECT stock_code, list_price, list_price * 10 AS price_for_ten
    FROM products
    WHERE list_price IS NOT NULL
    LIMIT 12;

common mistake

Without AS, the result column is named after the expression text, which does not match the requested name.

challenge 1

Return stock_code, list_price and price_with_vat for every product, where price_with_vat is list_price × 1.2 rounded to 2 decimals.

what to return

Columns
stock_codelist_priceprice_with_vat in this order
Rows
any order
challenge-1.sql
 
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.
Aliases & calculated columns — SQL foundations · Runtime