runtime
72 / 88

lesson 72 Warehouse & reporting projects · advanced

Silver: clean, map & deduplicate

Clean values, apply mappings and remove duplicate records.

the idea

Silver applies explicit, documented rules: trim keys and text, drop rows with no usable key, keep the latest version of each customer, and map inconsistent country codes to one spelling. Write the rules so a reviewer can see exactly why each row was kept, changed or excluded.

Dataset: Retail

bronze_customers4 columns · 1,689 rows
raw_idtextnametextcountrytextupdated_atdate & time
15600Amelia SmithGB2010-04-25 09:00:00
15600Amelia Smith united kingdom 2010-07-01 09:00:00
15600 Amelia SmithUnited Kingdom2011-07-01 09:00:00
17600Amelia SmithUK2011-11-22 09:00:00
18000Amelia SmithUnited Kingdom2010-05-24 09:00:00
12420Amelia JonesCyprus2011-10-07 09:00:00
12420Amelia JonesCyprus2011-11-01 09:00:00
14420Amelia JonesUnited Kingdom2010-01-17 09:00:00
14420Amelia JonesUK2010-08-01 09:00:00
14420 AMELIA JONESGB2011-08-01 09:00:00
raw_idtext
15600
nametext
Amelia Smith
countrytext
GB
updated_atdate & time
2010-04-25 09:00:00
raw_idtext
15600
nametext
Amelia Smith
countrytext
united kingdom
updated_atdate & time
2010-07-01 09:00:00
raw_idtext
15600
nametext
Amelia Smith
countrytext
United Kingdom
updated_atdate & time
2011-07-01 09:00:00
raw_idtext
17600
nametext
Amelia Smith
countrytext
UK
updated_atdate & time
2011-11-22 09:00:00
raw_idtext
18000
nametext
Amelia Smith
countrytext
United Kingdom
updated_atdate & time
2010-05-24 09:00:00
raw_idtext
12420
nametext
Amelia Jones
countrytext
Cyprus
updated_atdate & time
2011-10-07 09:00:00
raw_idtext
12420
nametext
Amelia Jones
countrytext
Cyprus
updated_atdate & time
2011-11-01 09:00:00
raw_idtext
14420
nametext
Amelia Jones
countrytext
United Kingdom
updated_atdate & time
2010-01-17 09:00:00
raw_idtext
14420
nametext
Amelia Jones
countrytext
UK
updated_atdate & time
2010-08-01 09:00:00
raw_idtext
14420
nametext
AMELIA JONES
countrytext
GB
updated_atdate & time
2011-08-01 09:00:00

first 10 of 1,689 rows · your queries use all of them

worked example

The step marked “the pattern” is what the challenges build on. Running examples doesn’t count toward progress.

  1. See country spelling variants. Run this on Bronze. UK, GB, and full names show up as separate groups—Silver will map them to one country label.the pattern

    example.sql
    SELECT UPPER(TRIM(country)) AS raw_country, COUNT(*) AS rows
    FROM bronze_customers
    GROUP BY raw_country
    ORDER BY rows DESC;
  2. See duplicate versions of one customer. Customer 12347 appears several times with different updated_at values; Silver keeps only the latest row per trimmed key.

    step-2.sql
    SELECT raw_id, name, country, updated_at
    FROM bronze_customers
    WHERE TRIM(raw_id) = '12347'
    ORDER BY updated_at;

common mistake

A new surrogate key is not deduplication: numbering every Bronze row gives one customer several keys.

challenge 1

Create TEMP table silver_customers with customer_id (trimmed raw_id), name (trimmed) and country, keeping only the latest updated_at version of each non-empty key and mapping UK/GB/UNITED KINGDOM → United Kingdom, DE → Germany, FR → France, ES → Spain (otherwise the trimmed value).

Return customer_id, name and country from it.

The check also looks at tables your script creates or changes.

challenge-1.sql
 
Output
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.
Silver: clean, map & deduplicate — Warehouse & reporting projects · Runtime