# Can I test the uniqueness of two columns?

Yes, there's a few different options for testing the uniqueness of two columns.

Consider an orders table that contains records from multiple countries, and the combination of ID and country code is unique:

| order\_id | country\_code |
| --------- | ------------- |
| 1         | AU            |
| 2         | AU            |
| ...       | ...           |
| 1         | US            |
| 2         | US            |
| ...       | ...           |

Here are some approaches:

#### 1. Create a unique key in the model and test that

models/orders.sql

```sql

select
  country_code || '-' || order_id as surrogate_key,
  ...
```

models/orders.yml

```yml
models:
  - name: orders
    columns:
      - name: surrogate_key
        data_tests:
          - unique
```

#### 2. Test an expression

models/orders.yml

```yml
models:
  - name: orders
    data_tests:
      - unique:
          arguments: # available in v1.10.5 and higher. Older versions can set the <argument_name> as the top-level property.
            column_name: "(country_code || '-' || order_id)"
```

#### 3. Use the `dbt_utils.unique_combination_of_columns` test

This is especially useful for large datasets since it is more performant. Check out the docs on [packages](../../docs/build/packages.md) for more information.

models/orders.yml

```yml
models:
  - name: orders
    data_tests:
      - dbt_utils.unique_combination_of_columns:
          arguments: # available in v1.10.5 and higher. Older versions can set the <argument_name> as the top-level property.
            combination_of_columns:
              - country_code
              - order_id
```

## Was this page helpful?

YesNo

[Privacy policy](https://www.getdbt.com/cloud/privacy-policy)[Create a GitHub issue](https://github.com/dbt-labs/docs.getdbt.com/issues)

This site is protected by reCAPTCHA and the Google [Privacy Policy](https://policies.google.com/privacy) and [Terms of Service](https://policies.google.com/terms) apply.
