0Pricing
SQL Academy · Lesson

Testing SQL: dbt tests, Great Expectations

Write data quality tests with dbt (unique, not_null, accepted_values) and richer assertions with Great Expectations.

Why Test SQL?

Bad data is invisible until it causes a bad decision. Tests catch:

  • Schema drift (column dropped, type changed)
  • Data quality (NULLs where forbidden, duplicates, out-of-range)
  • Referential issues (orphan rows)
  • Business invariants (revenue ≥ 0, totals add up)

dbt Tests: Built-In

Declare per-column in YAML:

# models/orders.yml
version: 2
models:
  - name: orders
    columns:
      - name: id
        tests:
          - unique
          - not_null
      - name: user_id
        tests:
          - not_null
          - relationships:
              to: ref('users')
              field: id
      - name: status
        tests:
          - accepted_values:
              values: ['pending', 'paid', 'cancelled']

All lessons in this course

  1. DBT (Data Build Tool) Fundamentals
  2. SQLFluff and Linting
  3. Testing SQL: dbt tests, Great Expectations
  4. CI for Schema Changes (GitHub Actions)
← Back to SQL Academy