What you will be able to do
- Route valid and invalid rows into separate tables with complementary WHERE filters
- Use CASE WHEN to replace placeholder values such as -1 with NULL
- Fix or remove bad rows in place with DELETE FROM and UPDATE, and know when MERGE INTO is required
- Add NOT NULL and CHECK constraints so that invalid data cannot be written again
1.Splitting good and bad rows with WHERE
Invalid data is any row that breaks a business rule. Examples include a negative quantity, an event timestamp in the future, or a code that does not match the expected format. When you clean a table in SQL you have two choices. You can stop invalid rows from reaching the cleaned table, or you can fix the table after they have landed. Databricks documents the first approach as a quarantine pattern built from ordinary WHERE clauses.
DECLARE current_time = now()
INSERT INTO silver_table
SELECT * FROM bronze_table
WHERE event_timestamp <= current_time AND quantity >= 0;
INSERT INTO quarantine_table
SELECT * FROM bronze_table
WHERE event_timestamp > current_time OR quantity < 0;The two predicates are logical opposites. The silver filter ANDs the rules together, and the quarantine filter ORs their negations. Bad records don't reach downstream tables, but they aren't thrown away either: you can inspect them, fix them and replay them. current_time is captured once with DECLARE so both inserts compare against the same moment. Databricks recommends always processing filtered data as a separate write operation, as shown here with two INSERT statements. One caution: a row whose quantity is NULL makes both predicates NULL, so it lands in neither table. If missing values are possible, handle them explicitly with IS NULL.
Checkpoint 1 of 6· Check yourself
Using the quarantine pattern above, where does a bronze row with quantity = -3 and a past event_timestamp end up?
quantity >= 0 is false, so the silver AND condition fails. quantity < 0 is true, so the quarantine OR condition succeeds.
“You can use filters and WHERE clauses to define custom logic that quarantines bad records and prevents them from propagating to downstream tables.”Source: docs.databricks.com
Sources1
2.Turning placeholder values into real NULLs
Some invalid data is not wrong so much as disguised. An upstream system that cannot encode NULL may write a placeholder such as -1 for a missing weight. If you leave it in, every average and every range filter downstream has to remember to ignore -1. A better fix is to apply the conversion once, while the data is being transformed, with a CASE WHEN expression.
Checkpoint 2 of 6· Fill the gap
Complete the transformation so that the placeholder -1 is stored as a missing value.
INSERT INTO silver_table
SELECT
* EXCEPT weight,
CASE
WHEN weight = -1 THEN ?
ELSE weight
END AS weight
FROM bronze_table;The goal is to represent missing data as a real NULL, which aggregates skip and IS NULL can find. Writing 0 would replace one fake value with another.
Source: docs.databricks.com* EXCEPT weight selects every column except the raw weight, and the CASE expression adds a cleaned weight column back in its place. The ELSE branch passes valid values through unchanged. The same structure handles other conditional business rules: list each predictable violation as a WHEN branch and say what it should become. For a single sentinel, nullif(weight, -1) is a shorter equivalent, because nullif returns NULL when its two arguments are equal and the first argument otherwise.
Every consumer would have to know and repeat that rule, and any query that forgot it would treat -1 as a real weight. Replacing the placeholder once, as a transformation, fixes it for everyone downstream.
3.Fixing tables in place with DELETE and UPDATE
When bad rows are already in a table, you can remove or correct them in place. DELETE FROM table_name [WHERE predicate] removes the rows that match the predicate. Leave out the WHERE clause and every row is deleted, so always write the predicate first. UPDATE table_name SET col = expr [WHERE clause] rewrites column values on the matching rows. Both statements are supported only on Delta Lake tables, and neither can target a foreign table.
UPDATE events SET eventType = 'click' WHERE eventType = 'clk'Both WHERE clauses accept subqueries such as IN, EXISTS and NOT EXISTS, with two exceptions: nested subqueries, and a NOT IN subquery inside an OR. Databricks recommends rewriting NOT IN as NOT EXISTS where possible, because NOT IN subqueries can be slow. In UPDATE, SET col = DEFAULT resets a value to the column's DEFAULT expression, or to NULL if the column has none. Some SQL dialects allow UPDATE ... FROM ... JOIN to correct values from a lookup table. Databricks SQL does not, so use MERGE INTO instead.
MERGE INTO t1
USING t2 ON t1.c2 = t2.c2
WHEN MATCHED THEN UPDATE SET t1.c1 = t2.c1;Checkpoint 3 of 6· Check yourself
You need to overwrite c1 in table t1 with corrected values from a reference table t2, matching on c2. Which statement works in Databricks SQL?
UPDATE ... FROM ... JOIN is not supported in Databricks SQL, and the documentation names MERGE INTO as the replacement. Resetting to DEFAULT would not copy the corrected values.
“To update a table based on a join with another table or subquery, use MERGE INTO instead.”Source: docs.databricks.com
Checkpoint 4 of 6· Exam question
An analyst runs `SELECT COALESCE(region, cost_center, 'UNKNOWN') AS region FROM catalog.dim_store` against a Unity Catalog dimension table where `region` is missing for some rows and `cost_center` is missing for a different subset of rows. What determines the value returned for each row?
Correct answer: A — COALESCE evaluates arguments left to right and returns the first non-NULL one, so `cost_center` only applies when `region` is NULL and 'UNKNOWN' only when both are NULL
- A. This matches documented COALESCE behavior: it short-circuits on the first non-NULL argument in left-to-right order, so cost_center is only reached when region is NULL, and the literal fallback only fires when both columns are NULL.
- B. COALESCE short-circuits rather than evaluating every argument, and it never simply returns the last item in the list; if it did, the literal fallback would overwrite valid region and cost_center values, which is not how the function behaves.
- C. COALESCE has no equality requirement between its arguments; it only checks each one for NULL in order, so this description invents a constraint the function does not enforce.
- D. COALESCE returns a single scalar value per row matching the first non-NULL argument's type, not a collection, so there is no array to explode.
4.Stopping invalid data from coming back
Cleaning a table once does not keep it clean. Constraints check rows as they are written. All constraints on Databricks require Delta Lake, and two kinds are enforced. A NOT NULL constraint means the column cannot hold NULL. A CHECK constraint names a boolean expression that must be true for every row. If a write violates either one, the transaction fails with an error, so the bad row is never stored.
ALTER TABLE main.default.people_demo ALTER COLUMN middleName DROP NOT NULL;
ALTER TABLE main.default.people_demo ALTER COLUMN ssn SET NOT NULL;This sets the order of work: clean first, then constrain. Use UPDATE with coalesce, or DELETE FROM, to remove the existing NULLs, and only then add NOT NULL. CHECK constraints work the same way. ALTER TABLE ADD CONSTRAINT checks all existing rows before adding the constraint. You can write value ranges such as CHECK (birthDate > '1900-01-01') and regex patterns with REGEXP or RLIKE. A CHECK expression cannot use user-defined, aggregate or window functions. To list a table's CHECK constraints, run DESCRIBE DETAIL or SHOW TBLPROPERTIES.
| Tool | Acts on | Effect on invalid rows |
|---|---|---|
| WHERE filter in INSERT ... SELECT | Rows being copied into a new table | Excluded from the target; can be routed to a quarantine table |
| DELETE FROM ... WHERE | Rows already in a Delta table | Removed |
| UPDATE ... SET ... WHERE | Rows already in a Delta table | Values corrected in place |
| NOT NULL / CHECK constraint | Future writes, after existing rows pass | The violating transaction fails with an error |
Checkpoint 5 of 6· Check yourself
A CHECK constraint validIds CHECK (id > 1 and id < 99999999) is on a table, and an INSERT includes one row with id = 0. What happens?
Enforced constraints don't drop or repair individual rows. A violation fails the whole transaction.
“When a constraint is violated, the transaction fails with an error.”Source: docs.databricks.com
Checkpoint 6 of 6· Exam question
A Unity Catalog managed table `hr.employees` has a small number of rows with clearly invalid `hire_date` values (e.g. dates far in the future) that were introduced by a faulty upstream feed. The team wants these specific rows permanently removed from the table itself, not just filtered out of query results. Which statement accomplishes this?
Correct answer: A — DELETE FROM hr.employees WHERE hire_date > current_date() to remove the future-dated rows
- A. DELETE FROM with a WHERE predicate permanently removes the matching rows from the Delta table's data files, which is the only listed operation that changes the stored table rather than just changing what a query returns.
- B. A plain SELECT with a WHERE clause only filters the rows returned to the client; it does not modify the underlying table, so the invalid rows remain in `hr.employees` afterward.
- C. UPDATE would overwrite the invalid hire_date with NULL but keep the row in the table, which fixes the value rather than removing the row as the requirement specifies.
- D. Creating a view layers a filtered read on top of the same underlying data without deleting anything, so the invalid rows still physically exist in `hr.employees`.
Sources5
Exam traps
Each one states something that sounds right. Open it to see what is actually true.
1.Databricks SQL supports
UPDATE ... FROM ... JOINfor correcting values from another table.Why is that wrong?That syntax is not supported. To update a table from a join with another table or subquery, use MERGE INTO.
2.Adding a NOT NULL or CHECK constraint to an existing table only affects new rows, so you can add it before cleaning.Why is that wrong?
Databricks checks every existing row before it adds the constraint. Existing violations must be cleaned first or the ALTER TABLE fails.
Covered in Stopping invalid data from coming back
Sources
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“Databricks recommends always processing filtered data as a separate write operation, especially when using Structured Streaming.”
↩︎ Splitting good and bad rows with WHERE“you could use a case when statement to dynamically replace these records as a transformation.”
↩︎ Turning placeholder values into real NULLs“You can use filters and WHERE clauses to define custom logic that quarantines bad records and prevents them from propagating to downstream tables.”
↩︎ Checkpoint“It can only be enabled on an existing table if no existing records in the column are null”
↩︎ Prediction - 2.
“Returns NULL if expr1 equals expr2, or expr1 otherwise.”
↩︎ Turning placeholder values into real NULLs - 3.
“Deletes the rows that match a predicate. When no predicate is provided, deletes all rows.”
↩︎ Fixing tables in place with DELETE and UPDATE“This statement is only supported for Delta Lake tables.”
↩︎ Fixing tables in place with DELETE and UPDATE - 4.
“The DEFAULT expression for the column if one is defined, NULL otherwise.”
↩︎ Fixing tables in place with DELETE and UPDATE“To update a table based on a join with another table or subquery, use MERGE INTO instead.”
↩︎ Exam trap 1“To update a table based on a join with another table or subquery, use MERGE INTO instead.”
↩︎ Checkpoint - 5.
“Use the DESCRIBE DETAIL and SHOW TBLPROPERTIES commands to see a table's CHECK constraints.”
↩︎ Stopping invalid data from coming back“All constraints on Databricks require Delta Lake.”
↩︎ Stopping invalid data from coming back“ALTER TABLE ADD CONSTRAINT verifies that all existing rows satisfy the constraint before adding the constraint to the table.”
↩︎ Exam trap 2“When a constraint is violated, the transaction fails with an error.”
↩︎ Checkpoint