3. Clean layers: bronze, silver and gold
3.4 Check your layers with SQL
Highlights & notes
Select lesson text to highlight it, or tap Add note.
Bookmarks
Bookmark a lesson to see it here.
How do you know that silver is really clean? You ask it. Three small queries catch most problems, and you can run them after every load.
- Duplicates: group by the key and look for a count above 1
- Missing values: count the rows where an important column is NULL
- Reconcile: compare counts between layers. If bronze has 3 different orders, silver should have 3 rows.
A check that returns zero rows when all is well is easy to automate. Dataform assertions are built on exactly this idea.
Find duplicate keys
silver.orders should have one row per order.
SQL
SELECT order_id, COUNT(*) AS copiesFROM silver.ordersGROUP BY order_idHAVING COUNT(*) > 1;
Rows at this step
order_id | copies ---------+------- 102 | 2
Quick check
The duplicate check returns zero rows. What does that mean?
Do the layers agree?
Two counts side by side.
SQL
SELECT (SELECT COUNT(DISTINCT order_id) FROM bronze.orders_raw) AS bronze_orders, (SELECT COUNT(*) FROM silver.orders) AS silver_rows;
Result
bronze_orders: 3 silver_rows: 3
Quick check
Bronze has 500 different orders and silver has 480 rows. What should you do?
Try it yourself
1. Run the duplicate check on your sales.orders table. It should return no rows.
2. Write a query that counts the rows of sales.orders where amount is NULL.
Question 1 · Easy
How do you find duplicate keys?
Question 2 · Medium
What does "reconcile" mean here?
Question 3 · Hard
Why design a check so that it returns zero rows on success?
Key takeaways
Check each layer with small queries: duplicates by key, NULLs in important columns, and counts that agree between layers. Zero rows returned means all is well.
Save your progress
Start free and your progress, code and notes stay saved. Free for 1 month, no card needed.
Start free Log in