DEV Community

Cover image for dbt Tutorial: Clean Shop Orders Data With SQL Models
The Practical Developer
The Practical Developer

Posted on Originally published at practicaldevblog.blogspot.com

dbt Tutorial: Clean Shop Orders Data With SQL Models

Quick answer

dbt turns a folder of SQL files into a tested data pipeline: you write SELECT statements as models, dbt runs them in order, and tests check the results. With the DuckDB adapter, the whole thing runs on one local file, no server and no cloud account. This post cleans raw shop orders with dbt, totals paid sales by city, and catches a duplicate order before the bad numbers reach anyone.

An online shop in Karachi exports its orders to a CSV file nightly. The totals column is a mess: some rows say "Rs 1,250", some say 980.50, some say 1150 with no currency. One row is a cancelled order, and one order id appears twice because the export ran twice. Summing that file in a spreadsheet gives a number, but the number is wrong, and nobody can say which row broke it. The usual fix is a one-off script that only its author understands. dbt offers a different shape: the cleaning lives in versioned SQL models, the rules are visible, and tests run every time the pipeline runs.

What I would check before using it

My first check is the shape of the mess. dbt earns its place when raw data needs the same cleaning every time: strip symbols, fix types, drop bad rows, then aggregate. If the data is already clean and the question is a one-off query, plain SQL is faster, and dbt is ceremony for nothing. The SQL Joins post covers the query side of that choice.

My second check is who reruns the pipeline. A dbt project is a folder of SQL files, so anyone on the team can read the cleaning rules. That shared reading is also the risk: models depend on each other, so a careless edit to a staging model can break every model downstream. I treat staging models as a contract and change them deliberately.

My third check is where the data ends up. dbt is a transformation tool, not a database: it needs a warehouse or a local engine to run against. DuckDB covers a laptop-sized project, and the PostgreSQL post covers the step up when the data outgrows one file. If the shop needs live dashboards or streaming data, dbt alone is the wrong tool, because it runs in batches.

My verdict, as judgement: adopt dbt when the same cleaning runs again and again and others read the results. The recommendation flips for a single report nobody will rerun, or if the team will not run the tests, because an untested pipeline is a spreadsheet with extra steps.

Cleaning shop orders with dbt and DuckDB, from seed to green tests

The demo is a small Karachi shop. The raw data is a CSV seed of nine rows, one a duplicate, with totals in mixed formats: "Rs 1,250", plain 980.50, and bare 1150. Everything runs locally against DuckDB.

Install it and check the version:

$ pip install dbt-duckdb
$ dbt --version
Core:
  - installed: 1.12.7
  - latest:    1.12.7 - Up to date!

Plugins:
  - duckdb: 1.11.1 - Up to date!
Enter fullscreen mode Exit fullscreen mode

The seed, seeds/raw_orders.csv, holds the raw export. The last two rows repeat ORD-1007.

order_id,customer,city,total_rs,status,order_date
ORD-1001,Ayesha Khan,Karachi,"Rs 1,250",paid,2026-11-20
ORD-1002,Bilal Ahmed,Lahore,980.50,paid,2026-11-21
ORD-1003,Sana Malik,Islamabad,"Rs 2,400",paid,2026-11-21
ORD-1004,Usman Raza,Karachi,1150,paid,2026-11-22
ORD-1005,Hira Shah,Lahore,"Rs 750",cancelled,2026-11-22
ORD-1006,Ahmed Noor,Karachi,3200,paid,2026-11-23
ORD-1007,Fatima Zahra,Islamabad,"Rs 1,875",paid,2026-11-23
ORD-1007,Fatima Zahra,Islamabad,"Rs 1,875",paid,2026-11-23
ORD-1008,Zain Ali,Karachi,"Rs 2,100",paid,2026-11-24
Enter fullscreen mode Exit fullscreen mode

The staging model, models/staging/stg_orders.sql, cleans in one SELECT: it strips everything but digits and the decimal point from the total, casts it to a number, and lowercases the status.

select
    order_id,
    trim(customer) as customer,
    trim(city) as city,
    cast(
        regexp_replace(cast(total_rs as varchar), '[^0-9.]', '', 'g')
        as double
    ) as total_pkr,
    lower(trim(status)) as status,
    cast(order_date as date) as order_date
from {{ ref('raw_orders') }}
Enter fullscreen mode Exit fullscreen mode

The {{ ref('raw_orders') }} call links the models: dbt resolves the name and records the dependency. The marts model, models/marts/paid_totals_by_city.sql, aggregates only paid orders by city:

select
    city,
    count(*) as order_count,
    round(sum(total_pkr), 2) as total_pkr
from {{ ref('stg_orders') }}
where status = 'paid'
group by city
order by total_pkr desc
Enter fullscreen mode Exit fullscreen mode

The tests in models/staging/schema.yml say the order id must be unique and never null, and the cleaned total never null.

version: 2

models:
  - name: stg_orders
    columns:
      - name: order_id
        tests:
          - unique
          - not_null
      - name: total_pkr
        tests:
          - not_null
Enter fullscreen mode Exit fullscreen mode

Seed and run first:

$ dbt seed
1 of 1 OK loaded seed file main.raw_orders ... [INSERT 9 in 0.14s]
Done. PASS=1 WARN=0 ERROR=0 SKIP=0 NO-OP=0 REUSED=0 TOTAL=1

$ dbt run
1 of 2 OK created sql view model main.stg_orders ... [OK in 0.13s]
2 of 2 OK created sql view model main.paid_totals_by_city ... [OK in 0.04s]
Done. PASS=2 WARN=0 ERROR=0 SKIP=0 NO-OP=0 REUSED=0 TOTAL=2
Enter fullscreen mode Exit fullscreen mode

Then the tests:

$ dbt test
1 of 3 PASS not_null_stg_orders_order_id ... [PASS in 0.08s]
2 of 3 PASS not_null_stg_orders_total_pkr ... [PASS in 0.02s]
3 of 3 FAIL 1 unique_stg_orders_order_id ... [FAIL 1 in 0.04s]

Done. PASS=2 WARN=0 ERROR=1 SKIP=0 NO-OP=0 REUSED=0 TOTAL=3
Enter fullscreen mode Exit fullscreen mode

The unique test fails because ORD-1007 appears twice. Before the fix, Islamabad reports 3 orders totalling 6,150.0. The fix is in the source data: delete the duplicate row, reload with dbt seed --full-refresh, and retest.

$ dbt seed --full-refresh
1 of 1 OK loaded seed file main.raw_orders ... [CREATE 8 in 0.16s]
Done. PASS=1 WARN=0 ERROR=0 SKIP=0 NO-OP=0 REUSED=0 TOTAL=1

$ dbt test
1 of 3 PASS not_null_stg_orders_order_id ... [PASS in 0.08s]
2 of 3 PASS not_null_stg_orders_total_pkr ... [PASS in 0.02s]
3 of 3 PASS unique_stg_orders_order_id ... [PASS in 0.03s]

Done. PASS=3 WARN=0 ERROR=0 SKIP=0 NO-OP=0 REUSED=0 TOTAL=3
Enter fullscreen mode Exit fullscreen mode

All three tests pass. The final result:

| city      | order_count | total_pkr |
| --------- | ----------- | --------- |
| Karachi   |           4 |   7,700.0 |
| Islamabad |           2 |   4,275.0 |
| Lahore    |           1 |     980.5 |
Enter fullscreen mode Exit fullscreen mode

Karachi leads with four paid orders at Rs 7,700, Islamabad has two at Rs 4,275, and Lahore one at Rs 980.50.

Terminal output showing a dbt test failing on a duplicate order id, then all three tests passing after the fix, with paid totals by city for Karachi, Islamabad, and Lahore.

Common mistakes and how to fix them

First, summing the raw totals column. The symptom is a cast error or a silent wrong total, because "Rs 1,250" is text, not a number. The fix is to clean the value in the staging model, stripping symbols and commas before casting.

Second, trusting the source file to have no duplicates. The symptom is a total that looks plausible but is inflated, like Islamabad at 6,150 instead of 4,275. The fix is a unique test on the order id, so the pipeline fails loudly.

Third, editing the seed CSV and rerunning without a full refresh. The symptom is the old rows still showing up, because dbt seed skips reinserting a seed it already loaded. The fix is dbt seed --full-refresh after any change to a seed file.

Fourth, writing a table name instead of ref() in a model. The symptom is dbt running models in the wrong order, or a model pointing at the wrong schema. The fix is to reference upstream models with {{ ref('model_name') }}, so dbt builds the dependency graph.

Frequently Asked Questions

Do I need a database server to learn dbt? No. The DuckDB adapter runs the whole project on one local file. A server only matters when the data outgrows a laptop.

What is the difference between a model and a seed? A seed is a CSV file dbt loads as a table, like the raw orders here. A model is a SELECT statement dbt turns into a view or table, like the staging and marts models above.

Should I use dbt or Pandas for this? Both clean data, but they suit different work. The Pandas post covers row-by-row cleaning in Python for one-off analysis, while dbt fits cleaning that runs on a schedule, in SQL the team can read. The Polars post covers a faster Python option for large frames.

Can dbt tests catch bad data before it ships? Yes, that is their main job. A unique test fails the run when an id repeats, and a not_null test fails it when a value goes missing.

What to remember

  • A dbt model is a SELECT statement, and ref() links models so they run in order.
  • Staging models clean raw data once, so every model downstream trusts the same numbers.
  • Seeds load CSV files as tables; use dbt seed --full-refresh after editing one.
  • Tests like unique and not_null turn silent bad data into a failed run.
  • DuckDB runs a whole dbt project on one local file, with no server.

Where this leaves you

The shop orders started as a CSV nobody could sum safely, and ended as two models and three tests that run the same way every time. The duplicate order id was the lesson: without the unique test, Islamabad would have reported Rs 6,150 instead of Rs 4,275, and nobody would have known. That is the whole case for dbt in one small project. Take a messy export of your own, load it as a seed, write one staging model that cleans it, and add a unique test on its id.

Further reading

Waqas Shahid is a software engineer from Karachi with more than 12 years of experience, including 6+ years in engineering leadership. He writes The Practical Developer.

Originally published at https://practicaldevblog.blogspot.com/2026/11/dbt-shop-orders-data-models.html

Top comments (0)