Start learning free

LESSON 41 OF 107 · DATA ENGINEERING: DATA QUALITY

Accepted Values and Ranges

Practice

A value can sit in the right column and still be wrong. A status 'PAID' is not 'paid', so a report that keeps status = 'paid' silently drops that order. A quantity of -1 or "two" breaks every total it touches. Two kinds of check catch them: accepted values (the column holds only values from a known list) and ranges (a number lies between two limits).

NOT IN (…) is true when a value is none of the listed values. The match is exact, so 'Yes' is not 'yes'. For example, the newsletter column of customers.csv should only say yes or no:

SELECT COUNT(*)
FROM 'customers.csv'
WHERE newsletter NOT IN ('yes', 'no')

Every column of raw_orders is text, so a number rule first turns the text into a number. TRY_CAST(x AS INT) gives the whole number, or NULL when the text isn't one, like "two"; a plain CAST would stop the whole query with an error. NOT BETWEEN 1 AND 20 is true for numbers below 1 or above 20 (BETWEEN includes both ends). A comparison with NULL gives NULL, which WHERE drops, so the NULLs need their own IS NULL test, joined with OR.

A hidden step adds orders/2026-05-08.csv with two rows:

5076,57,Mug,2,12,PAID,2026-05-08 09:05:00
5077,212,Pen,-1,2,paid,2026-05-08 10:40:00

Your program prints the first two lines below: the status check finds 'PAID', and the quantity check finds "two" (order 5030, in 05-03's file) and -1. The check then sets one quantity to 21 and one to 20 and runs your quantity query again: 21 fails, 20 passes.

status is known: FAIL (1)
quantity is 1 to 20: FAIL (2)
With a quantity of 21 and one of 20:
quantity is 1 to 20: FAIL (3)

Your task: make the first query count the rows whose status is neither 'paid' nor 'cancelled', and report it as "status is known". Make the second query count the rows whose quantity isn't a whole number from 1 to 20, and report it as "quantity is 1 to 20".


Assignment
Follow the Coding Tutorial to write an accepted-values check on status and a range check on quantity.


Hint
Use TRY_CAST(quantity AS INT) in both parts of the quantity rule: compare the number, not the text. Each check's count is con.execute(query).fetchone()[0].


STEP 1 OF 5
    Ctrl + Enter to check