> ## Documentation Index
> Fetch the complete documentation index at: https://docs.synq.io/llms.txt
> Use this file to discover all available pages before exploring further.

# SQL Tests

SQL tests let you write bespoke tests that fit your business circumstances and can be run on any table tracked in the platform through a unified workflow.

<Frame caption="Example—An SQL test runs a query to check for rows where the workspace is null. If any rows match the test, it will throw an error">
  <img src="https://mintcdn.com/synq-15/Uc7H-CPDlW17hyZt/images/monitors/sqltest1.png?fit=max&auto=format&n=Uc7H-CPDlW17hyZt&q=85&s=0ef78994e5a57fd3548466c8e341276b" width="2000" height="1202" data-path="images/monitors/sqltest1.png" />
</Frame>

### Creating a SQL test

* Head to the [Health overview](https://app.synq.io/healthz/overview)

* In the SQL tests section, click [`Add sql test`](https://app.synq.io/healthz/sql-tests/create)

* Select the connection to execute your SQL query

* Specify a SQL query. The test is considered a success if it returns zero records. If any records are returned, the test will trigger an error, and the failed records will be stored in an [audit table](#audit-table-schema) for investigation.

  <img src="https://mintcdn.com/synq-15/Uc7H-CPDlW17hyZt/images/monitors/sqltest2.png?fit=max&auto=format&n=Uc7H-CPDlW17hyZt&q=85&s=8b2a60780011eb05ab2717215d0f25f5" alt="title" width="2000" height="1081" data-path="images/monitors/sqltest2.png" />

* Choose a schedule (`hourly/daily`) and a time (`hh: mm`) when you want the test to run.

  <img src="https://mintcdn.com/synq-15/W5wQ1KC16xJvhnfC/images/monitors/sqltest3.png?fit=max&auto=format&n=W5wQ1KC16xJvhnfC&q=85&s=1ef3579c4f33921c464d2ca0bc645210" alt="title" width="2000" height="501" data-path="images/monitors/sqltest3.png" />

<Info>
  Running an excessive amount of tests or running a test too often will impact your data warehouse costs. Avoid running tests more often than needed
</Info>

* The confirmation page will show you a summary of the setup. To make it easier to locate in the UI, you can give the test a human-friendly name.

  <img src="https://mintcdn.com/synq-15/W5wQ1KC16xJvhnfC/images/monitors/sqltest4.png?fit=max&auto=format&n=W5wQ1KC16xJvhnfC&q=85&s=660360e64e00fac203c5b25bbe283784" alt="title" width="2000" height="1156" data-path="images/monitors/sqltest4.png" />

### Editing a SQL test

* Head to the [Health overview](https://app.synq.io/healthz/overview)
* Click on [SQL tests](https://app.synq.io/healthz/monitors?filters=%5B%5B%7B%22kind%22%3A%22Type%22%2C%22types%22%3A%5B1421%5D%7D%5D%5D) to see all your SQL tests
* Select the SQL test you want to edit by clicking on it
* In the popout, navigate to the `settings` tab. Click `Edit` under SQL test configurations to edit the name, run schedule, or SQL code. To delete the test, click `Delete SQL test`

  <img src="https://mintcdn.com/synq-15/W5wQ1KC16xJvhnfC/images/monitors/sqltest5.png?fit=max&auto=format&n=W5wQ1KC16xJvhnfC&q=85&s=9ba217ce15f71ac0c29c15aa407cd722" alt="title" width="2000" height="1254" data-path="images/monitors/sqltest5.png" />

### Audit table schema

When a SQL test fails, the failed records are stored in an audit table in your data warehouse for investigation and troubleshooting. The platform automatically creates this table in your data warehouse when you configure a SQL test with `saveFailures=true`.

#### Default table name

By default, the audit table is named `synq_sql_test__audit`. The table location can be configured per integration.

#### Schema by data warehouse

The audit table schema varies slightly by data warehouse. Below are the CREATE TABLE statements for each supported platform:

<Accordion title="BigQuery">
  ```sql theme={null}
  CREATE TABLE IF NOT EXISTS synq_sql_test__audit (
    integration_id STRING
      , sql_test_path STRING
      , execution_id STRING
      , rows_count INTEGER
      , result STRING
      , created_at TIMESTAMP
  );
  ```
</Accordion>

<Accordion title="Snowflake">
  ```sql theme={null}
  CREATE TABLE IF NOT EXISTS synq_sql_test__audit (
    integration_id STRING
      , sql_test_path STRING
      , execution_id STRING
      , rows_count INTEGER
      , result STRING
      , created_at TIMESTAMP
  );
  ```
</Accordion>

<Accordion title="Databricks">
  ```sql theme={null}
  CREATE TABLE IF NOT EXISTS synq_sql_test__audit (
    integration_id VARCHAR
    , sql_test_path VARCHAR
    , execution_id VARCHAR
    , rows_count INTEGER
    , result VARCHAR
    , created_at TIMESTAMP
  )
  ```
</Accordion>

<Accordion title="PostgreSQL">
  ```sql theme={null}
  CREATE TABLE IF NOT EXISTS "synq_sql_test__audit" (
    integration_id TEXT
    , sql_test_path TEXT
    , execution_id TEXT
    , rows_count INTEGER
    , result TEXT
    , created_at TIMESTAMPTZ
  );
  ```
</Accordion>

<Accordion title="Redshift">
  ```sql theme={null}
  CREATE TABLE IF NOT EXISTS "synq_sql_test__audit" (
    integration_id VARCHAR
    , sql_test_path VARCHAR
    , execution_id VARCHAR
    , rows_count INTEGER
    , result VARCHAR
    , created_at TIMESTAMP
  );
  ```
</Accordion>

<Accordion title="ClickHouse">
  ```sql theme={null}
  CREATE TABLE IF NOT EXISTS synq_sql_test__audit (
    integration_id String
      , sql_test_path String
      , execution_id String
      , rows_count Int32
      , result String
      , created_at DateTime64(8, 'UTC')
  ) Engine = MergeTree()
      ORDER BY (integration_id, sql_test_path, execution_id, created_at)
  ;
  ```
</Accordion>

#### Column descriptions

| Column           | Description                                         |
| ---------------- | --------------------------------------------------- |
| `integration_id` | Unique identifier of the data warehouse integration |
| `sql_test_path`  | Path or identifier of the SQL test                  |
| `execution_id`   | Unique identifier for each test execution           |
| `rows_count`     | Number of rows that failed the test                 |
| `result`         | The failed records in JSON format                   |
| `created_at`     | Timestamp when the audit record was created         |

<Info>
  The audit table is created automatically when you run your first SQL test. Ensure the platform has the necessary permissions to create tables in your configured schema or database.
</Info>
