runtime
82 / 88

lesson 82 Warehouse & reporting projects · advanced

Automated star-schema quality tests

Test relationships and data quality in a star schema.

the idea

A data quality test has a name, an expectation and a failure count. Zero failures means that expectation held, not that the data is perfect. Check unique line ids, valid quantities and both dimension relationships, then gate publication on the blocking tests.

Dataset: Retail

dim_customer5 columns · 848 rows
customer_keynumber · unique idcustomer_idtextcontact_nametextcountrytextregiontext
112347Arthur TurnerIcelandEurope
212353Harry TurnerBahrainRest of World
312359Theo TurnerCyprusEurope
412373Harry HillAustriaEurope
512374Emily HillAustriaEurope
612384Ava MooreSwitzerlandEurope
712389Leo MooreAustraliaRest of World
812392Lily MooreAustraliaRest of World
912404Ava SmithFinlandEurope
1012416Sophie SmithAustraliaRest of World
customer_keynumber · unique id
1
customer_idtext
12347
contact_nametext
Arthur Turner
countrytext
Iceland
regiontext
Europe
customer_keynumber · unique id
2
customer_idtext
12353
contact_nametext
Harry Turner
countrytext
Bahrain
regiontext
Rest of World
customer_keynumber · unique id
3
customer_idtext
12359
contact_nametext
Theo Turner
countrytext
Cyprus
regiontext
Europe
customer_keynumber · unique id
4
customer_idtext
12373
contact_nametext
Harry Hill
countrytext
Austria
regiontext
Europe
customer_keynumber · unique id
5
customer_idtext
12374
contact_nametext
Emily Hill
countrytext
Austria
regiontext
Europe
customer_keynumber · unique id
6
customer_idtext
12384
contact_nametext
Ava Moore
countrytext
Switzerland
regiontext
Europe
customer_keynumber · unique id
7
customer_idtext
12389
contact_nametext
Leo Moore
countrytext
Australia
regiontext
Rest of World
customer_keynumber · unique id
8
customer_idtext
12392
contact_nametext
Lily Moore
countrytext
Australia
regiontext
Rest of World
customer_keynumber · unique id
9
customer_idtext
12404
contact_nametext
Ava Smith
countrytext
Finland
regiontext
Europe
customer_keynumber · unique id
10
customer_idtext
12416
contact_nametext
Sophie Smith
countrytext
Australia
regiontext
Rest of World

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

dim_product5 columns · 4,902 rows
product_keynumber · unique idproduct_idtextdescriptiontextcategorytextpricedecimal
110002INFLATABLE POLITICAL GLOBEOther Gifts0.85
210002RROBOT PENCIL SHARPNERStationery & Gift Wrap5.17
310080GROOVY CACTUS INFLATABLEOther Gifts0.39
410109BENDY COLOUR PENCILSStationery & Gift Wrap0.42
510120DOGGY RUBBEROther Gifts0.21
610123CHEARTS WRAPPING TAPEStationery & Gift Wrap0.65
710123GARMY CAMO WRAPPING TAPEStationery & Gift Wrap0.65
810124ASPOTS ON RED BOOKCOVER TAPEStationery & Gift Wrap0.42
910124GARMY CAMO BOOKCOVER TAPEStationery & Gift Wrap0.42
1010125MINI FUNKY DESIGN TAPESStationery & Gift Wrap0.85
product_keynumber · unique id
1
product_idtext
10002
descriptiontext
INFLATABLE POLITICAL GLOBE
categorytext
Other Gifts
pricedecimal
0.85
product_keynumber · unique id
2
product_idtext
10002R
descriptiontext
ROBOT PENCIL SHARPNER
categorytext
Stationery & Gift Wrap
pricedecimal
5.17
product_keynumber · unique id
3
product_idtext
10080
descriptiontext
GROOVY CACTUS INFLATABLE
categorytext
Other Gifts
pricedecimal
0.39
product_keynumber · unique id
4
product_idtext
10109
descriptiontext
BENDY COLOUR PENCILS
categorytext
Stationery & Gift Wrap
pricedecimal
0.42
product_keynumber · unique id
5
product_idtext
10120
descriptiontext
DOGGY RUBBER
categorytext
Other Gifts
pricedecimal
0.21
product_keynumber · unique id
6
product_idtext
10123C
descriptiontext
HEARTS WRAPPING TAPE
categorytext
Stationery & Gift Wrap
pricedecimal
0.65
product_keynumber · unique id
7
product_idtext
10123G
descriptiontext
ARMY CAMO WRAPPING TAPE
categorytext
Stationery & Gift Wrap
pricedecimal
0.65
product_keynumber · unique id
8
product_idtext
10124A
descriptiontext
SPOTS ON RED BOOKCOVER TAPE
categorytext
Stationery & Gift Wrap
pricedecimal
0.42
product_keynumber · unique id
9
product_idtext
10124G
descriptiontext
ARMY CAMO BOOKCOVER TAPE
categorytext
Stationery & Gift Wrap
pricedecimal
0.42
product_keynumber · unique id
10
product_idtext
10125
descriptiontext
MINI FUNKY DESIGN TAPES
categorytext
Stationery & Gift Wrap
pricedecimal
0.85

first 10 of 4,902 rows · your queries use all of them

dq_sales5 columns · 2,555 rows
line_idnumbercustomer_keynumberproduct_keynumberquantitynumbersalesdecimal
12302746724762439.6
1230284671829317.85
123029467428415
1230304672725617.34
1230314673247415.8
1230324672573219.9
1230334671672617.7
12303446735941230.6
1230354672317416.6
12303646717842420.4
line_idnumber
123027
customer_keynumber
467
product_keynumber
2476
quantitynumber
24
salesdecimal
39.6
line_idnumber
123028
customer_keynumber
467
product_keynumber
1829
quantitynumber
3
salesdecimal
17.85
line_idnumber
123029
customer_keynumber
467
product_keynumber
428
quantitynumber
4
salesdecimal
15
line_idnumber
123030
customer_keynumber
467
product_keynumber
2725
quantitynumber
6
salesdecimal
17.34
line_idnumber
123031
customer_keynumber
467
product_keynumber
3247
quantitynumber
4
salesdecimal
15.8
line_idnumber
123032
customer_keynumber
467
product_keynumber
2573
quantitynumber
2
salesdecimal
19.9
line_idnumber
123033
customer_keynumber
467
product_keynumber
1672
quantitynumber
6
salesdecimal
17.7
line_idnumber
123034
customer_keynumber
467
product_keynumber
3594
quantitynumber
12
salesdecimal
30.6
line_idnumber
123035
customer_keynumber
467
product_keynumber
2317
quantitynumber
4
salesdecimal
16.6
line_idnumber
123036
customer_keynumber
467
product_keynumber
1784
quantitynumber
24
salesdecimal
20.4

first 10 of 2,555 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. Find duplicated line ids. Run this. line_id should be unique; groups with COUNT(*) > 1 mean the same sale line arrived twice in the batch.the pattern

    example.sql
    SELECT line_id, COUNT(*) AS copies
    FROM dq_sales
    GROUP BY line_id
    HAVING COUNT(*) > 1
    LIMIT 10;
  2. Find facts missing a customer dimension. customer_key values with no dim_customer row are orphans—those lines fail the relationship test before publish.

    step-2.sql
    SELECT s.line_id, s.customer_key
    FROM dq_sales s
    LEFT JOIN dim_customer c ON c.customer_key = s.customer_key
    WHERE c.customer_key IS NULL
    LIMIT 10;
  3. Shape one quality test query. Each named test returns one row: test name and failure count. Zero failures means that rule passed, not that the batch is perfect.

    step-3.sql
    SELECT 'duplicate_line_ids' AS test, COUNT(*) AS failures
    FROM (
      SELECT line_id
      FROM dq_sales
      GROUP BY line_id
      HAVING COUNT(*) > 1
    );

common mistake

Failure counts can overlap on one row; their sum counts rule violations, not distinct bad rows.

challenge 1

Find duplicate_line_ids (line ids appearing more than once), orphan_customers (rows whose customer_key is not in dim_customer), orphan_products (rows whose product_key is not in dim_product) and invalid_quantity (NULL or not positive).

Return test and failures.

challenge-1.sql
 
Output
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.
Automated star-schema quality tests — Warehouse & reporting projects · Runtime