# SQL linting for database apps

A query can parse, run and still be wrong: a join that counts each order once per item, a `DELETE` that lost its `WHERE`, a `NULL` written to a `NOT NULL` column, a tenant table read without the tenant filter. The database accepts all of them. `sqlty check` reads your schema and reports them where the query is written, in the editor, a pre-commit hook or CI. It runs no query and needs no database.

Types are one part of it. sqlty has [over 80 rules](<https://sqlty.kuncar.dev/reference/rules/index.md>), and these catch mistakes that no type checker sees:

| Mistake | Rules | Default |
| --- | --- | --- |
| Totals a join inflates | [`fanout-aggregate`](<https://sqlty.kuncar.dev/reference/rules/fanout-aggregate/index.md>), [`chasm-join`](<https://sqlty.kuncar.dev/reference/rules/chasm-join/index.md>), [`unproven-join`](<https://sqlty.kuncar.dev/reference/rules/unproven-join/index.md>) | warn, ignore |
| Joins on the wrong key | [`foreign-key-mismatch`](<https://sqlty.kuncar.dev/reference/rules/foreign-key-mismatch/index.md>), [`entity-mismatch`](<https://sqlty.kuncar.dev/reference/rules/entity-mismatch/index.md>) | warn, ignore |
| Grouping the database rejects | [`ungrouped-column`](<https://sqlty.kuncar.dev/reference/rules/ungrouped-column/index.md>), [`position-out-of-range`](<https://sqlty.kuncar.dev/reference/rules/position-out-of-range/index.md>) | error |
| Writes that touch every row | [`missing-where`](<https://sqlty.kuncar.dev/reference/rules/missing-where/index.md>) | warn |
| Writes the schema forbids | [`null-write`](<https://sqlty.kuncar.dev/reference/rules/null-write/index.md>), [`nullable-write`](<https://sqlty.kuncar.dev/reference/rules/nullable-write/index.md>), [`insert-missing-column`](<https://sqlty.kuncar.dev/reference/rules/insert-missing-column/index.md>), [`literal-write`](<https://sqlty.kuncar.dev/reference/rules/literal-write/index.md>) | error, warn |
| Filters that never match | [`unmatched-literal`](<https://sqlty.kuncar.dev/reference/rules/unmatched-literal/index.md>) | warn |
| Tenant data read across tenants | [`tenant-scope`](<https://sqlty.kuncar.dev/reference/rules/tenant-scope/index.md>), [`tenant-policy`](<https://sqlty.kuncar.dev/reference/rules/tenant-policy/index.md>) | error, warn |
| Schema drift | [`unknown-column`](<https://sqlty.kuncar.dev/reference/rules/unknown-column/index.md>), [`unknown-table`](<https://sqlty.kuncar.dev/reference/rules/unknown-table/index.md>), [`deprecated-column`](<https://sqlty.kuncar.dev/reference/rules/deprecated-column/index.md>) | error, warn |
| Queries that break when the schema grows | [`select-star`](<https://sqlty.kuncar.dev/reference/rules/select-star/index.md>), [`ambiguous-column`](<https://sqlty.kuncar.dev/reference/rules/ambiguous-column/index.md>) | warn, error |
| Parameters that don't fit | [`param-count-mismatch`](<https://sqlty.kuncar.dev/reference/rules/param-count-mismatch/index.md>), [`param-type-mismatch`](<https://sqlty.kuncar.dev/reference/rules/param-type-mismatch/index.md>), [`extra-param`](<https://sqlty.kuncar.dev/reference/rules/extra-param/index.md>) | error, warn |
| Migrations that lock or fail | [17 migration rules](<https://sqlty.kuncar.dev/migration-safety/index.md>) | error, warn |

## An example

```toml
[tool.sqlty]
schema = "schema.sql"
```

`schema.sql`:

```sql
CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT NOT NULL, status TEXT NOT NULL CHECK (status IN ('active', 'banned')));
CREATE TABLE orders (id INTEGER PRIMARY KEY, user_id INTEGER NOT NULL REFERENCES users, total NUMERIC NOT NULL);
CREATE TABLE order_items (id INTEGER PRIMARY KEY, order_id INTEGER NOT NULL REFERENCES orders, amount NUMERIC NOT NULL);
```

```python
import decimal

from sqlty import SQL

# error: [fanout-aggregate] "SUM(o.total) aggregates orders, whose rows repeat once per order_items row"
revenue = SQL[tuple[str, decimal.Decimal]]("""--sql
SELECT u.email, SUM(o.total)
FROM users u
JOIN orders o ON o.user_id = u.id
JOIN order_items i ON i.order_id = o.id
GROUP BY u.email""")

# error: [unmatched-literal] "`users.status` is never 'blocked', so this WHERE matches no row"
blocked = SQL[tuple[int]]("--sql\nSELECT id FROM users WHERE status = 'blocked'")

# error: [missing-where] "`UPDATE` without `WHERE` changes every row of `orders`: add a `WHERE`, or `WHERE true` to mean it"
reset = SQL[tuple[()]]("--sql\nUPDATE orders SET total = 0")

# error: [null-write] "`users.email` is NOT NULL, but NULL is written to it"
signup = SQL[tuple[()]]("--sql\nINSERT INTO users (id, email, status) VALUES (1, NULL, 'active')")
```

The fixes: aggregate the items per order first, compare `status` with a value its `CHECK` allows, add the `WHERE`, write an email. A rule's page explains why it matters and when to turn it off, and `sqlty rule NAME` prints it.

## Where it runs

- **Editor:** the [VS Code extension](<https://sqlty.kuncar.dev/recipes/vscode/index.md>) and the language server underline each finding as you type, with quick fixes.
- **CI and pre-commit:** `sqlty check .` exits non-zero on errors ([CI and pre-commit](<https://sqlty.kuncar.dev/recipes/ci/index.md>)).
- **Coding agents:** the check needs no database, so an agent can run it in its sandbox and read `--output-format json` ([agents](<https://sqlty.kuncar.dev/recipes/agents/index.md>)).
- **TypeScript and Go:** the same rules through the [ESLint plugin](<https://sqlty.kuncar.dev/recipes/eslint/index.md>) and [`go vet`](<https://sqlty.kuncar.dev/recipes/go/index.md>).

Severities are set per rule in `[tool.sqlty.rules]`; see [configuration](<https://sqlty.kuncar.dev/reference/configuration/index.md>). For layout and style, the [`sqruff`](<https://sqlty.kuncar.dev/reference/rules/sqruff/index.md>) rule adds sqruff's lint rules and [`sqlty format`](<https://sqlty.kuncar.dev/guide/formatting/index.md>) formats each query.
