runtime
16 / 88

lesson 16 Calculations & data quality · intermediate

CAST, text types & NULL arithmetic

Convert sensor values and calculate deviations with explicit units.

the idea

CAST converts a value to another type. Casting text to INTEGER reads the leading digits and stops at the first non-digit, so '02134-1207' becomes 2134 and leading zeros vanish. Any arithmetic or concatenation with NULL produces NULL; COALESCE supplies a fallback.

Dataset: Logistics

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.

shipments12 columns · 8,200 rows
shipment_idnumber · unique idorder_referencetextshipper_nametextorigin_warehouse_idnumber · links to warehousesdestination_citytextdestination_statetextdestination_postal_codetextservice_level_idnumber · links to service_levelscarrier_idnumber · links to carrierscreated_atdate & timepromised_delivery_datedatedeclared_valuedecimal
1ORD-643450Delta Robotics4OrlandoFL33457332025-01-19 21:43:152025-01-2244.17
2ORD-102115Crest Outfitters3RaleighNC27481712025-04-09 00:16:312025-04-15114.7
3ORD-926480Arbor Systems3CharlotteNC27558772025-03-06 19:19:352025-03-1350.37
4ORD-651018Timber Robotics6MiamiFL331221122025-04-05 00:31:022025-04-111181.62
5ORD-901762APEX ANALYTICS4San DiegoCA97949232025-01-20 04:30:442025-01-2991.96
6ORD-832022Nimbus Outfitters 3New YorkNY10050652025-04-03 15:09:382025-04-11166.67
shipment_idnumber · unique id
1
order_referencetext
ORD-643450
shipper_nametext
Delta Robotics
origin_warehouse_idnumber · links to warehouses
4
destination_citytext
Orlando
destination_statetext
FL
destination_postal_codetext
33457
service_level_idnumber · links to service_levels
3
carrier_idnumber · links to carriers
3
created_atdate & time
2025-01-19 21:43:15
promised_delivery_datedate
2025-01-22
declared_valuedecimal
44.17
shipment_idnumber · unique id
2
order_referencetext
ORD-102115
shipper_nametext
Crest Outfitters
origin_warehouse_idnumber · links to warehouses
3
destination_citytext
Raleigh
destination_statetext
NC
destination_postal_codetext
27481
service_level_idnumber · links to service_levels
7
carrier_idnumber · links to carriers
1
created_atdate & time
2025-04-09 00:16:31
promised_delivery_datedate
2025-04-15
declared_valuedecimal
114.7
shipment_idnumber · unique id
3
order_referencetext
ORD-926480
shipper_nametext
Arbor Systems
origin_warehouse_idnumber · links to warehouses
3
destination_citytext
Charlotte
destination_statetext
NC
destination_postal_codetext
27558
service_level_idnumber · links to service_levels
7
carrier_idnumber · links to carriers
7
created_atdate & time
2025-03-06 19:19:35
promised_delivery_datedate
2025-03-13
declared_valuedecimal
50.37
shipment_idnumber · unique id
4
order_referencetext
ORD-651018
shipper_nametext
Timber Robotics
origin_warehouse_idnumber · links to warehouses
6
destination_citytext
Miami
destination_statetext
FL
destination_postal_codetext
33122
service_level_idnumber · links to service_levels
1
carrier_idnumber · links to carriers
12
created_atdate & time
2025-04-05 00:31:02
promised_delivery_datedate
2025-04-11
declared_valuedecimal
1181.62
shipment_idnumber · unique id
5
order_referencetext
ORD-901762
shipper_nametext
APEX ANALYTICS
origin_warehouse_idnumber · links to warehouses
4
destination_citytext
San Diego
destination_statetext
CA
destination_postal_codetext
97949
service_level_idnumber · links to service_levels
2
carrier_idnumber · links to carriers
3
created_atdate & time
2025-01-20 04:30:44
promised_delivery_datedate
2025-01-29
declared_valuedecimal
91.96
shipment_idnumber · unique id
6
order_referencetext
ORD-832022
shipper_nametext
Nimbus Outfitters
origin_warehouse_idnumber · links to warehouses
3
destination_citytext
New York
destination_statetext
NY
destination_postal_codetext
10050
service_level_idnumber · links to service_levels
6
carrier_idnumber · links to carriers
5
created_atdate & time
2025-04-03 15:09:38
promised_delivery_datedate
2025-04-11
declared_valuedecimal
166.67

first 6 of 8,200 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 messy postal codes as stored. Run this. Some codes lack a leading zero, some include ZIP+4, and some are missing.

    step-1.sql
    SELECT shipment_id, destination_postal_code
    FROM shipments
    WHERE destination_state = 'MA'
    LIMIT 15;
  2. Try CAST and string concatenation. Run this. CAST changes type; concatenating with NULL yields NULL—COALESCE is what fixes display later.the pattern

    example.sql
    SELECT
      CAST('02134' AS INTEGER) AS as_number,
      CAST(42 AS TEXT) || '!' AS as_text,
      'ZIP ' || NULL AS with_null;

common mistake

A ZIP code is an identifier, not a number. CAST(zip AS INTEGER) loses leading zeros and the ZIP+4 suffix.

challenge 1

For shipments to Massachusetts (destination_state = 'MA'), return shipment_id, destination_postal_code and zip_number, the postal code cast to INTEGER.

what to return

Columns
shipment_iddestination_postal_codezip_number 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.
CAST, text types & NULL arithmetic — Calculations & data quality · Runtime