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 quality test has a named expectation and a failure count. Zero means that expectation passed, not that all data is perfect. Check unique line IDs, valid measures, and both dimension relationships. Gate promotion on declared blocking rules; do not silently repair financial data.

tables in this lesson

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

dq_sales5 columns · 5 rows
line_idintegercustomer_keyintegerproduct_keyintegerquantityintegersalesreal
111224
29991112
32999180
42300
111224

all 5 rows

dim_customer4 columns · 5 rows
customer_keyinteger · keycustomer_idtextnametextcountrytext
1C001Alice NguyenUnited States
2C002BenUnited Kingdom
3C003CaraUnited Kingdom
4C004DevIndia
5C005ElenaCanada

all 5 rows

dim_product5 columns · 4 rows
product_keyinteger · keyproduct_idtextnametextcategorytextpricereal
1P01NotebookStationery12
2P02Desk lampHome45
3P03KeyboardTech80
4P04HeadphonesTech120

all 4 rows

example

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

example.sql
SELECT 'duplicate_line_ids' AS test,COUNT(*) AS failures
FROM (
  SELECT line_id
  FROM dq_sales
  GROUP BY line_id
  HAVING COUNT(*)>1
)
UNION ALL
SELECT 'orphan_customers',COUNT(*)
FROM dq_sales s
LEFT JOIN dim_customer c ON c.customer_key=s.customer_key
WHERE c.customer_key IS NULL
UNION ALL
SELECT 'orphan_products',COUNT(*)
FROM dq_sales s
LEFT JOIN dim_product p ON p.product_key=s.product_key
WHERE p.product_key IS NULL
UNION ALL
SELECT 'invalid_quantity',COUNT(*)
FROM dq_sales
WHERE quantity IS NULL OR quantity<=0

Watch for: Failure counts may overlap on one row; summed failures are rule violations, not distinct rejected rows.

build it step by step

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

  1. Understand the source contract. Fictional extracts, load counts, invalid dates, versioned dimensions, refresh staging and a data catalog. The foundation project is latest-state; separate history and incremental labs demonstrate extensions.

    step-1.sql
    SELECT *
    FROM dq_sales;
  2. Execute the transformation. A quality test has a named expectation and a failure count. Zero means that expectation passed, not that all data is perfect. Check unique line IDs, valid measures, and both dimension relationships. Gate promotion on declared blocking rules; do not silently repair financial data.

    step-2.sql
    SELECT 'duplicate_line_ids' AS test,COUNT(*) AS failures
    FROM (
      SELECT line_id
      FROM dq_sales
      GROUP BY line_id
      HAVING COUNT(*)>1
    )
    UNION ALL
    SELECT 'orphan_customers',COUNT(*)
    FROM dq_sales s
    LEFT JOIN dim_customer c ON c.customer_key=s.customer_key
    WHERE c.customer_key IS NULL
    UNION ALL
    SELECT 'orphan_products',COUNT(*)
    FROM dq_sales s
    LEFT JOIN dim_product p ON p.product_key=s.product_key
    WHERE p.product_key IS NULL
    UNION ALL
    SELECT 'invalid_quantity',COUNT(*)
    FROM dq_sales
    WHERE quantity IS NULL OR quantity<=0
  3. Test another question. Failure counts may overlap on one row; summed failures are rule violations, not distinct rejected rows.

    step-3.sql
    WITH checks AS(
      SELECT 'duplicate_line_ids' AS test,COUNT(*) AS failures
      FROM (
        SELECT line_id
        FROM dq_sales
        GROUP BY line_id
        HAVING COUNT(*)>1
      )
      UNION ALL
      SELECT 'orphan_customers',COUNT(*)
      FROM dq_sales s
      LEFT JOIN dim_customer c ON c.customer_key=s.customer_key
      WHERE c.customer_key IS NULL
      UNION ALL
      SELECT 'orphan_products',COUNT(*)
      FROM dq_sales s
      LEFT JOIN dim_product p ON p.product_key=s.product_key
      WHERE p.product_key IS NULL
      UNION ALL
      SELECT 'invalid_quantity',COUNT(*)
      FROM dq_sales
      WHERE quantity IS NULL OR quantity<=0
    )
    SELECT SUM(failures) AS blocking_failures,SUM(failures)=0 AS can_publish
    FROM checks

challenge 1

Return test,failures for duplicate_line_ids (number of duplicated id groups), orphan_customers (rows), orphan_products (rows), and invalid_quantity (NULL or <=0 rows) in dq_sales.

Return testfailuresany row order

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