# dbt analyses

***

In dbt, **Analyses** are SQL files that live in your `analyses/` directory. They are the "exploratory sandbox" of your project—allowing you to write and version-control SQL that you don't necessarily want to turn into a permanent table or view in your database.

***

### The Core Purpose

The primary goal of an Analysis is to provide a space for **ad-hoc queries** that still benefit from dbt’s core features (like `ref()` and `source()`) without cluttering your production data warehouse with "one-off" tables.

#### Key Characteristics:

* **Compiled, Not Run**: When you run `dbt compile`, dbt will build the raw SQL for your analyses. However, `dbt run` **will not** execute them against your database.
* **No Materialization**: Analyses do not create tables or views. They exist only as code in your repository and compiled SQL in your `target/` folder.
* **DAG Integration**: You can use `{{ ref('...') }}` to refer to your models, but since an analysis isn't a model itself, no other model can `ref()` an analysis.

***

### When to use Analyses

Analyses are perfect for queries that you need to run frequently but don't need to persist as a data asset.

* **Auditing & Validation**: "Show me all records where the total fare is negative" (to investigate a data quality issue).
* **One-off Requests:** A stakeholder asks for a quick count of active users last Tuesday.
* **Testing Logic:** Before you commit a complex join into a permanent **Model**, you might write it as an Analysis first to see if the results look correct.
* **Dashboard Queries:** If you have a BI tool that requires a specific, messy SQL block, you can store that query in the `analyses/` folder so it’s version-controlled and easy for teammates to find.

***

### Comparison: Analysis vs. Model vs. Seed

| **Feature**                | **Analysis**      | **Model**       | **Seed**                |
| -------------------------- | ----------------- | --------------- | ----------------------- |
| **Location**               | `analyses/`       | `models/`       | `seeds/`                |
| **Executes on `dbt run`?** | No                | Yes             | No (`dbt seed` instead) |
| **Creates a Table/View?**  | No                | Yes             | Yes                     |
| **Can be `ref()`-ed?**     | No                | Yes             | Yes                     |
| **Use Case**               | Ad-hoc / One-offs | Permanent Logic | Static Reference Data   |

***

### How to use them

1. **Create the file:** Save a file like `analyses/check_recent_rides.sql`.
2. **Write your SQL**:&#x20;

   ```sql
   -- Use ref() just like in a model!
   SELECT * FROM {{ ref('stg_taxi_rides') }}
   WHERE pickup_datetime >= current_date - 1
   ```
3. **Compile**: Run `dbt compile`.
4. **Retrieve:** Go to `target/compiled/taxi_rides_ny/analyses/check_recent_rides.sql`, copy the pure SQL, and run it in your SQL runner (like DuckDB or DBeaver).

***
