---
title: "SQL files with too many CTEs | Metacenta"
description: "For SQL projects without dbt: which files hold more CTEs than our threshold, how we count them, and when a long file is still the right shape."
url: "https://metacenta.com/rules/sql-high-cte-count"
updated: "2026-10-14"
tokens: 476
---

Maintainability · rule sql-high-cte-count

# SQL files with too many CTEs

A Metacenta review checks this under the rule **SQL files stay readable**. Everything below applies whether or not you ever commission one.

## What this rule checks

For SQL projects without dbt, this rule flags files that write a relation and hold more than eight CTEs; eight passes. We count CTE definitions across every statement in the file. Assertion files are not counted.

## Why it matters

Logic this dense is hard to test in pieces, and a wrong step is hard to find. A reviewer has to hold every CTE in mind to know what the output means.

## How to fix it

**Split the densest SQL files.** Split the intermediate steps into their own relations, each written by its own file. Start with the steps other files could read too.

## When it is fine to leave

A file of several short statements, each with a few CTEs, is counted as one total and may exceed the threshold while staying readable. So may a file of many one-line import CTEs. Leave either as it is.

## What we need to check it

The `.sql` files, read from your repository with read-only access or uploaded. We tokenise the SQL rather than parse it, so the finding is medium confidence. Files over 500,000 characters are skipped.

[Ask about a review](https://metacenta.com/enquire?src=rule-sql-high-cte-count&via=md)

## Related pages

- [Duplicate logic across dbt models](https://metacenta.com/rules/duplicate-model-logic)
- [select * in dbt models](https://metacenta.com/rules/select-star)
- [dbt models with too many CTEs](https://metacenta.com/rules/high-cte-count)
- [dbt layer naming: inconsistent model prefixes](https://metacenta.com/rules/layer-naming-mismatch)
- [A sample review, as a client reads it](https://metacenta.com/sample)
- [All published rules](https://metacenta.com/rules)
- [How a review is assessed](https://metacenta.com/standard)
