Before you beginSet up the Maple Market governance lab
Define six checks that match the business
A data quality rule says what acceptable records look like. Maple Market reports sales in one currency in this lab. Finance wants one positive amount per order, a real customer on each sale and the expected number and total from its approved source control. Detect bad input where it lands. Serve the last good result until the replacement is ready.
| Dimension | Question | Example check or evidence |
|---|---|---|
| Accuracy | Is the value correct in the real source? | Compare sale 704 to the approved receipt of 90 |
| Completeness | Is a required value or row missing? | AMOUNT IS NULL or fewer rows than the source control |
| Uniqueness | Did the same business key arrive twice? | COUNT(*) per SALE_ID greater than one |
| Validity | Is the value allowed? | Amount greater than zero and region in the agreed list |
| Consistency | Do related values agree? | Exactly one customer for every order and matching source and report totals |
| Freshness | How recent is the latest load? | Compare LOADED_AT with the 30-minute delivery target |
Load a damaged delivery into a private table
Run the setup as M18_OWNER with the approved warehouse after creating the core objects. DELIVERY_ID is a unique source-row number within this single-batch fixture. SALE_ID is the business key. The source control is a separate expected order count and revenue from an approved source, not a total derived from this broken file.
The incoming table is deliberately allowed to store bad amounts so the team can diagnose them. Consumers cannot select this schema. CORE.SALES already holds the last good result of three sales and revenue 450. The release statement later writes only to this existing protected table.
| Delivery | Sale | Input issue | Corrected source |
|---|---|---|---|
| 4 | 704 | NULL amount | 90 |
| 5 and 6 | 705 | 60 delivered twice | Keep one delivery for 60 |
| 7 | 706 | No customer 999 | Customer 103 |
| 8 | 707 | Amount -10 | Amount 10 |
USE ROLE M18_OWNER;
CREATE TABLE LS_M18_GOV_LAB.QUALITY.INCOMING_SALES (
DELIVERY_ID NUMBER NOT NULL, SALE_ID NUMBER NOT NULL,
CUSTOMER_ID NUMBER NOT NULL, REGION VARCHAR, AMOUNT NUMBER(10,2),
LOADED_AT TIMESTAMP_LTZ NOT NULL
);
INSERT INTO LS_M18_GOV_LAB.QUALITY.INCOMING_SALES
SELECT COLUMN1,COLUMN2,COLUMN3,COLUMN4,COLUMN5,CURRENT_TIMESTAMP()
FROM VALUES
(1,701,101,'IN',100),(2,702,102,'EU',200),(3,703,103,'US',150),
(4,704,104,'IN',NULL),(5,705,105,'EU',60),(6,705,105,'EU',60),
(7,706,999,'US',40),(8,707,101,'IN',-10);
CREATE TABLE LS_M18_GOV_LAB.QUALITY.BATCH_CONTROL (
EXPECTED_ORDERS NUMBER NOT NULL, EXPECTED_REVENUE NUMBER(10,2) NOT NULL
);
INSERT INTO LS_M18_GOV_LAB.QUALITY.BATCH_CONTROL VALUES (7,650);
CREATE TABLE LS_M18_GOV_LAB.QUALITY.SALES_REJECTS (
ATTEMPT_ID NUMBER, DELIVERY_ID NUMBER, SALE_ID NUMBER,
CUSTOMER_ID NUMBER, REGION VARCHAR, AMOUNT NUMBER(10,2), REASON VARCHAR
);Find invalid rows and log them without publishing
COUNT OVER finds duplicates without hiding either delivery. The customer summary counts references once per customer, so the LEFT JOIN keeps orphan rows visible and catches ambiguous reference data. The CASE reports the first problem found on each row. Fixing it may expose another problem on the next run. Duplicate sale 705 is held on both rows until the source confirms which delivery to keep.
Run the preview first. Then write the five rejected deliveries to SALES_REJECTS. MERGE matches the attempt and delivery so repeating the same logging step does not add duplicates. Treat the rejects as private because they contain operational values and identifiers. In production save the original file or immutable delivery version with an incident reference.
| Delivery | Sale | Reason |
|---|---|---|
| 4 | 704 | MISSING_AMOUNT |
| 5 | 705 | DUPLICATE_SALE |
| 6 | 705 | DUPLICATE_SALE |
| 7 | 706 | UNKNOWN_CUSTOMER |
| 8 | 707 | INVALID_AMOUNT |
CREATE VIEW LS_M18_GOV_LAB.QUALITY.SALES_VALIDATION AS
WITH deliveries AS (
SELECT I.*, COUNT(*) OVER (PARTITION BY SALE_ID) AS COPIES
FROM LS_M18_GOV_LAB.QUALITY.INCOMING_SALES I
), known_customers AS (
SELECT CUSTOMER_ID, COUNT(*) AS MATCHES
FROM LS_M18_GOV_LAB.CORE.CUSTOMERS
GROUP BY CUSTOMER_ID
)
SELECT D.DELIVERY_ID, D.SALE_ID, D.CUSTOMER_ID, D.REGION, D.AMOUNT,
CASE
WHEN D.AMOUNT IS NULL THEN 'MISSING_AMOUNT'
WHEN D.COPIES > 1 THEN 'DUPLICATE_SALE'
WHEN C.MATCHES IS NULL THEN 'UNKNOWN_CUSTOMER'
WHEN C.MATCHES <> 1 THEN 'AMBIGUOUS_CUSTOMER'
WHEN D.REGION IS NULL OR D.REGION NOT IN ('IN','EU','US') THEN 'INVALID_REGION'
WHEN D.AMOUNT <= 0 THEN 'INVALID_AMOUNT'
ELSE NULL
END AS REASON
FROM deliveries D
LEFT JOIN known_customers C ON C.CUSTOMER_ID = D.CUSTOMER_ID;
SELECT DELIVERY_ID, SALE_ID, REASON
FROM LS_M18_GOV_LAB.QUALITY.SALES_VALIDATION
WHERE REASON IS NOT NULL ORDER BY DELIVERY_ID;Save the first attempt for investigation
The output has five rows for attempt 1. A later repair should not erase these records. A different source revision should get a different attempt identifier in a production journal.
-- Use attempt 1 for the first review. Preserve its evidence during repair.
MERGE INTO LS_M18_GOV_LAB.QUALITY.SALES_REJECTS R
USING (
SELECT 1 AS ATTEMPT_ID, DELIVERY_ID, SALE_ID, CUSTOMER_ID, REGION, AMOUNT, REASON
FROM LS_M18_GOV_LAB.QUALITY.SALES_VALIDATION WHERE REASON IS NOT NULL
) S ON R.ATTEMPT_ID = S.ATTEMPT_ID AND R.DELIVERY_ID = S.DELIVERY_ID
WHEN NOT MATCHED THEN INSERT
(ATTEMPT_ID,DELIVERY_ID,SALE_ID,CUSTOMER_ID,REGION,AMOUNT,REASON)
VALUES (S.ATTEMPT_ID,S.DELIVERY_ID,S.SALE_ID,S.CUSTOMER_ID,S.REGION,S.AMOUNT,S.REASON);
SELECT ATTEMPT_ID, DELIVERY_ID, SALE_ID, REASON
FROM LS_M18_GOV_LAB.QUALITY.SALES_REJECTS ORDER BY ATTEMPT_ID, DELIVERY_ID;Use a release condition that blocks the write
This pipeline requires a complete delivery, so even valid rows from a failing batch wait. Check the one approved manifest, zero rejects and both independent totals. The CROSS JOIN adds one release decision to each candidate row. A FALSE or NULL decision lets no rows into MERGE. Monitoring can discover a problem after loading. This condition prevents it from changing the report.
Keep one writer and a stable approved snapshot during the exercise. In production record a batch ID, separate each source revision and coordinate writes and checkpoints so a retry uses the same data. Validate that the target’s business key is unique before publishing. Standard table primary and foreign keys are usually not enforced in Snowflake. This SQL checks the relationships explicitly.
| State | Deliveries | Valid | Rejected | Valid revenue | Release? | Served rows / revenue |
|---|---|---|---|---|---|---|
| Broken input | 8 | 3 | 5 | 450 | FALSE | 3 / 450 |
| Verified correction | 7 | 7 | 0 | 650 | TRUE | 7 / 650 after MERGE |
CREATE VIEW LS_M18_GOV_LAB.QUALITY.SALES_RELEASE_CHECK AS
WITH measured AS (
SELECT COUNT(*) AS DELIVERIES,
COUNT(CASE WHEN REASON IS NULL THEN 1 END) AS VALID_ORDERS,
COUNT(CASE WHEN REASON IS NOT NULL THEN 1 END) AS REJECTED,
COALESCE(SUM(CASE WHEN REASON IS NULL THEN AMOUNT END),0) AS VALID_REVENUE
FROM LS_M18_GOV_LAB.QUALITY.SALES_VALIDATION
), manifest AS (
SELECT COUNT(*) AS MANIFEST_ROWS,
MAX(EXPECTED_ORDERS) AS EXPECTED_ORDERS,
MAX(EXPECTED_REVENUE) AS EXPECTED_REVENUE
FROM LS_M18_GOV_LAB.QUALITY.BATCH_CONTROL
)
SELECT M.*, C.EXPECTED_ORDERS, C.EXPECTED_REVENUE,
C.MANIFEST_ROWS = 1 AND M.REJECTED = 0
AND M.VALID_ORDERS = C.EXPECTED_ORDERS
AND M.VALID_REVENUE = C.EXPECTED_REVENUE AS RELEASE_OK
FROM measured M CROSS JOIN manifest C;
SELECT * FROM LS_M18_GOV_LAB.QUALITY.SALES_RELEASE_CHECK;Try the publish statement against the broken batch
Use this same publish statement before and after repair. With the current input, it updates and inserts zero rows. The served table still shows 3 and 450. A clean batch goes through this gate without giving consumers access to the input.
MERGE INTO LS_M18_GOV_LAB.CORE.SALES T
USING (
SELECT V.SALE_ID, V.CUSTOMER_ID, V.REGION, V.AMOUNT, C.EMAIL AS BUYER_EMAIL
FROM LS_M18_GOV_LAB.QUALITY.SALES_VALIDATION V
JOIN LS_M18_GOV_LAB.CORE.CUSTOMERS C ON C.CUSTOMER_ID = V.CUSTOMER_ID
CROSS JOIN LS_M18_GOV_LAB.QUALITY.SALES_RELEASE_CHECK R
WHERE V.REASON IS NULL AND R.RELEASE_OK
) S ON T.SALE_ID = S.SALE_ID
WHEN MATCHED AND (
T.CUSTOMER_ID IS DISTINCT FROM S.CUSTOMER_ID OR T.REGION IS DISTINCT FROM S.REGION
OR T.AMOUNT IS DISTINCT FROM S.AMOUNT OR T.BUYER_EMAIL IS DISTINCT FROM S.BUYER_EMAIL
) THEN UPDATE SET CUSTOMER_ID = S.CUSTOMER_ID, REGION = S.REGION,
AMOUNT = S.AMOUNT, BUYER_EMAIL = S.BUYER_EMAIL
WHEN NOT MATCHED THEN INSERT (SALE_ID,CUSTOMER_ID,REGION,AMOUNT,BUYER_EMAIL)
VALUES (S.SALE_ID,S.CUSTOMER_ID,S.REGION,S.AMOUNT,S.BUYER_EMAIL);
SELECT COUNT(*) AS ORDERS, SUM(AMOUNT) AS REVENUE
FROM LS_M18_GOV_LAB.CORE.SALES;Apply the source-approved repair and reconcile both sides
In the exercise the transaction models an approved source repair. It fixes the missing amount and bad reference, removes the extra delivery and corrects the negative value. The raw values held by the first attempt remain available in the reject log. Do not invent amounts or silently delete real deliveries. Confirm corrections with the source owner and mark the approved version.
The next preview should return no invalid input rows. The release gate now reads 7 valid orders, 650 and TRUE. Rerun the prior publish block. It inserts four rows and leaves the earlier three unchanged. Then rerun the publish block once more. Since the values match, it inserts or updates zero. Reconcile keys and values in both directions. The query below reports differences and should return zero rows.
- Private incoming dataCheck the snapshot to Validation and release
- Validation and releaseHold a failed batch to Private quarantine
- Private quarantineRetry after approved repair to Validation and release
- Validation and releasePublish a passing batch to Protected sales
- Private incoming data
- Eight deliveries and an approved source control.
- Validation and release
- Check records, keys and control totals.
- Private quarantine
- Hold failures and record evidence.
- Protected sales
- Expose only the released dataset.
-- Model an approved source correction in the disposable lab only.
BEGIN TRANSACTION;
UPDATE LS_M18_GOV_LAB.QUALITY.INCOMING_SALES SET AMOUNT = 90
WHERE DELIVERY_ID = 4 AND SALE_ID = 704 AND AMOUNT IS NULL;
DELETE FROM LS_M18_GOV_LAB.QUALITY.INCOMING_SALES
WHERE DELIVERY_ID = 6 AND SALE_ID = 705;
UPDATE LS_M18_GOV_LAB.QUALITY.INCOMING_SALES SET CUSTOMER_ID = 103
WHERE DELIVERY_ID = 7 AND SALE_ID = 706 AND CUSTOMER_ID = 999;
UPDATE LS_M18_GOV_LAB.QUALITY.INCOMING_SALES SET AMOUNT = 10
WHERE DELIVERY_ID = 8 AND SALE_ID = 707 AND AMOUNT = -10;
COMMIT;
SELECT * FROM LS_M18_GOV_LAB.QUALITY.SALES_RELEASE_CHECK;Verify keys and values after publication
Compare missing, extra, duplicate or changed keys as well as the total. Two errors can cancel out in a sum. This comparison covers the complete seven-order target in the lab. In a production table with prior batches, reconcile the target only for the intended batch or business range.
WITH target AS (
SELECT SALE_ID, COUNT(*) AS COPIES, MIN(CUSTOMER_ID) AS CUSTOMER_ID,
MIN(REGION) AS REGION, MIN(AMOUNT) AS AMOUNT
FROM LS_M18_GOV_LAB.CORE.SALES GROUP BY SALE_ID
)
SELECT COALESCE(T.SALE_ID, S.SALE_ID) AS SALE_ID,
S.AMOUNT AS SOURCE_AMOUNT, T.AMOUNT AS TARGET_AMOUNT, T.COPIES
FROM LS_M18_GOV_LAB.QUALITY.SALES_VALIDATION S
FULL OUTER JOIN target T ON T.SALE_ID = S.SALE_ID
WHERE T.SALE_ID IS NULL OR S.SALE_ID IS NULL OR S.REASON IS NOT NULL
OR T.COPIES <> 1 OR T.CUSTOMER_ID IS DISTINCT FROM S.CUSTOMER_ID
OR T.REGION IS DISTINCT FROM S.REGION OR T.AMOUNT IS DISTINCT FROM S.AMOUNT
ORDER BY SALE_ID;Check how recent the data is and assign follow-up
The load timestamp says when the batch reached this table. It does not prove that the source events themselves are recent. The example should read FRESH soon after seeding and STALE after 30 minutes. An empty table yields MISSING. A future timestamp raises a clock problem. Production monitoring often tracks both a source business timestamp and the arrival clock, along with expected batches.
When the report is stale, tell the report owner which delivery is missing or held and keep the last good timestamp visible. Use scheduled monitoring for alerts, then locate bad records with SQL before retrying.
SELECT MAX(LOADED_AT) AS LATEST_LOAD,
DATEDIFF('minute', MAX(LOADED_AT), CURRENT_TIMESTAMP()) AS AGE_MINUTES,
CASE
WHEN MAX(LOADED_AT) IS NULL THEN 'MISSING'
WHEN MAX(LOADED_AT) > CURRENT_TIMESTAMP() THEN 'CLOCK_ERROR'
WHEN MAX(LOADED_AT) < DATEADD('minute',-30,CURRENT_TIMESTAMP()) THEN 'STALE'
ELSE 'FRESH'
END AS FRESHNESS_STATUS
FROM LS_M18_GOV_LAB.QUALITY.INCOMING_SALES;Interview practice
Explain the decision and the evidence you would use to test it.
What is the difference between detecting and preventing bad data?
The invalid-row query detects the problems and records them. The release condition controls whether that batch can change the report.
How would you find missing customer references?
I would left join orders to one summary row per customer ID and check for zero or multiple matches. An inner join alone would hide the missing orders.
Why compare keys if the revenue matches?
Two mistakes can cancel out or a duplicate can replace a valid order of the same amount. I compare both directions, counts per key, fields that matter and the total before I close the incident.
Words used in this lesson
- Quarantine
- Private storage for records held for investigation.
- Reconciliation
- Compare counts, keys and totals against independent source evidence.
- Quality gate
- A rule that prevents publication until the checks pass.
See how it works
Choose a step to explore the idea at your own pace.
Open the interactive explanation
Snowflake data quality checks, quarantine and safe release
Validate, quarantine and replay a sales delivery
The last good report stays available when the release gate is closed.
Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.
Read the complete diagram explanation
- Validate, quarantine and replay a sales delivery
The last good report stays available when the release gate is closed.
Private incoming data → Validation and release → Private quarantine → Protected sales
Try the SQL or command
Read → predict → run in your learning environment → compare. You can also learn by reading without an account.
New to SQL? Where to run it and how to read it
A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.
- No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
- For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
- Read the entire example, including comments beginning with
--. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor. - Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
- SELECT / FROM
- Choose the values to return / identify their input.
- VALUES / COLUMN1
- VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
- AS / WITH
- AS gives a result a readable name. WITH names a temporary query step within one statement.
- NULL / ::
- NULL means missing or unknown. The :: notation converts a value to a named data type.
- Session
- One connection and its settings. Keep related transaction steps in the same session.
- Database.schema.table
- A full address for stored data. It is separate from the warehouse that processes queries.
Need the L2_OBJECTS_LAB setup? Open the first database-object lesson →
Help with common practice errors
- No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
- Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
- Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
- Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.
Before running this lesson
All records are fictional. Outputs are expected or simulated. Live practice needs an authorized scratch account, test roles, an approved warehouse and the editions listed in the lesson.
Check your understanding
Choose an answer, then check your reasoning. This is practice, not a scored test.
Try it yourself
Name one test that would catch a missing order and one that would catch a stale source.
Compare your reasoning
Match expected order IDs against the target in both directions. Compare the latest load timestamp to its delivery target and independently check missing batches.
Connect this lesson to the learning path
Official sources and curriculum
Module 18 · See module scope
By Rohit Ray · Independent educational lesson · Explore this visual lab