Query advice
Español: es/ADVICE.md
Experimental: not covered by the 1.x stability guarantee (see STABILITY.md); it may change in a minor release.
A query can be allowed and still be a bad idea. SELECT * FROM orders passes a permissive policy, gets LIMIT 1000 and runs, although it reads every column of every row. Query advice names those problems and gives the query a score from 1 to 100. It is off by default and advisory: it never changes the decision unless you set min_score.
# policy.yaml
dialect: postgres
tables: [orders, countries]
advice:
small_tables: [countries] # no-filter does not apply to these
warn_below: 60 # flags the result when the score is lower
min_score: null # set a number to reject below it (see below)
weights: {select-star: 20} # override a default penalty
$ sqlguardllm check --policy policy.yaml --advice "SELECT * FROM orders"
SELECT * FROM orders LIMIT 1000
-- query score: 50/100
-- no-filter: orders (-25)
-- select-star: * (-15)
-- no-row-bound: no LIMIT, TOP or FETCH (-10)
In text mode the advice is printed as SQL comments, so the rewritten SQL on stdout stays valid SQL. In Python it is Guard.explain(sql, context).advice; with --format json and in explain_query of the MCP server it is the advice field. --advice works even when the policy has no advice section.
What it reports
The score is max(1, 100 − the sum of the penalties). A finding that repeats counts at most twice. The findings are the contract; the number is a summary for people and dashboards.
| Code | Fires on | Default penalty |
|---|---|---|
select-star |
* or t.* in the outermost projection |
15 |
no-filter |
A SELECT that reads a table with no WHERE, unless the table is in small_tables |
25 |
no-row-bound |
The outermost query has no LIMIT, TOP or FETCH. An aggregate with no GROUP BY returns one row and is exempt |
10 |
leading-wildcard |
LIKE '%text' |
10 |
function-on-filtered-column |
A function or cast around a column in WHERE or JOIN ... ON (UPPER(email) = ..., YEAR(created_at) = ...) |
10 |
or-chain |
More than 3 OR branches over different columns |
8 |
unlinked-join |
A cartesian product (CROSS JOIN, or tables not linked by a join condition) |
25 |
deep-nesting |
More than 2 levels of subqueries, derived tables and CTEs | 8 |
order-without-limit |
ORDER BY in the outermost query with no row bound |
5 |
It also lists the columns by clause, in two views: columns as written in the query (o.total or total), and tables, resolved to the physical table the way Guard.explain() does (aliases, correlated subqueries, and unqualified columns through the policy's schema): {"orders": {"projected": ["id"], "filtered": ["total"]}}. The clauses are projected, filtered, joined, grouped, ordered and in_functions. A column that can't be tied to one table is left out of tables, and a column of a derived table or CTE is counted at the table it comes from.
Rejecting low scores
With min_score: 80, check() rejects a query whose score is lower, with the experimental code low_query_score and a hint to name the columns, filter the rows and add a limit. Use it with care: it turns advice into a rule, so test the policy with sqlguardllm test first.
What it does not do
- It scores the model's SQL before the rewrite. The row filters, masks and limit the guard adds are not credited, and no context value ever appears in the advice.
- It knows nothing about table sizes, indexes or plans. For those see the cost guard in MCP.md.
- It is a heuristic over the syntax tree. A query can score 100 and still be slow, and
no-filterfires on a deliberate full-table report.