# Your LLM writes good SQL. That's not the same as safe SQL

DevFeed: [Your LLM writes good SQL. That's not the same as safe SQL](<https://devfeed.tech/articles/your-llm-writes-good-sql-that-s-not-the-same-as-safe-sql-118718.md>)

Original publisher: [Read original article](<https://www.infoworld.com/article/4244721/your-llm-writes-good-sql-thats-not-the-same-as-safe-sql.html>)

Author: Venkat Navneeth Burla

Published: 2026-11-05T09:00:00Z

Content type: opinion

Language: en

Sources: [InfoWorld](<https://devfeed.tech/sources/infoworld.md>)

Topics: [LLM security](<https://devfeed.tech/topics/llm-security.md>), [text2sql](<https://devfeed.tech/topics/text2sql.md>), [AI for SQL query generation](<https://devfeed.tech/topics/ai-for-sql-query-generation.md>), [least privilege](<https://devfeed.tech/topics/least-privilege.md>), [Large Language Model](<https://devfeed.tech/topics/llm.md>), [LLM observability](<https://devfeed.tech/topics/llm-observability.md>), [Benchmark](<https://devfeed.tech/topics/benchmark.md>), [JOIN](<https://devfeed.tech/topics/join.md>)

Tags: [ai](<https://devfeed.tech/tags/ai.md>), [analytics](<https://devfeed.tech/tags/analytics.md>), [analytics-artificial-intelligence-data-management-generative-ai-sql](<https://devfeed.tech/tags/analytics-artificial-intelligence-data-management-generative-ai-sql.md>), [artificial-intelligence](<https://devfeed.tech/tags/artificial-intelligence.md>), [benchmark](<https://devfeed.tech/tags/benchmark.md>), [cortex](<https://devfeed.tech/tags/cortex.md>), [customer](<https://devfeed.tech/tags/customer.md>), [data](<https://devfeed.tech/tags/data.md>), [data-management](<https://devfeed.tech/tags/data-management.md>), [engagement](<https://devfeed.tech/tags/engagement.md>), [english](<https://devfeed.tech/tags/english.md>), [field](<https://devfeed.tech/tags/field.md>), [fields](<https://devfeed.tech/tags/fields.md>), [filter](<https://devfeed.tech/tags/filter.md>), [finance](<https://devfeed.tech/tags/finance.md>), [generative-ai](<https://devfeed.tech/tags/generative-ai.md>), [healthcare](<https://devfeed.tech/tags/healthcare.md>), [hospitals](<https://devfeed.tech/tags/hospitals.md>), [hr](<https://devfeed.tech/tags/hr.md>), [ids](<https://devfeed.tech/tags/ids.md>), [ignore](<https://devfeed.tech/tags/ignore.md>), [it](<https://devfeed.tech/tags/it.md>), [language-en](<https://devfeed.tech/tags/language-en.md>), [llm](<https://devfeed.tech/tags/llm.md>), [sql](<https://devfeed.tech/tags/sql.md>), [storytype-feature](<https://devfeed.tech/tags/storytype-feature.md>)

## AI overview

The article argues that accurate text-to-SQL can still expose data to the wrong user, and recommends enforcing access rules in code before queries run. It proposes resolving each user's role, business unit, territory, and assigned accounts first, limiting the model's available tables and columns, and tagging sensitive fields in the metadata catalog. A policy layer should parse and rewrite generated SQL; validation should reject SELECT *, scope-crossing queries, oversized results, and risky joins, with execution through a narrow database account and an audit record of context, policy, SQL, and results. In a synthetic, de-identified 900-query benchmark, 40% ran unchanged; others needed row filters, masking, or join rewrites, while some were sent for review or blocked. The article reports up to about 80 unauthorized rows from plain text-to-SQL, leaks under static role-based access control, and none under its policy-aware approach, with about 6.5 ms added per query. These findings are limited to the stated benchmark.

## Source excerpt

A few years into working with data, I've learned that the hardest part of an AI system is rarely the model. Usually it's the question of who gets to ask what. That question came up for me while building an LLM analytics tool on Snowflake Cortex for healthcare engagement and field-performance data. The tool let people ask questions in plain English, and it cut recurring analytics turnaround from days to minutes. It also made me realize that a fast, accurate answer can still be the wrong answer. A correct query can still be the wrong one Most of us test text-to-SQL the same way. We ask a question, check that the query runs, and compare the output to what we expected. That works right up until the answer is correct for the wrong person. Picture a sales rep who types "quarterly sales for my hospitals" and gets every hospital in the region back. The SQL is clean. The result is still a problem, and the same thing can happen in a multi-tenant app, an HR system, or a finance tool. Start with who is asking The first change I'd make is to what the model is allowed to see. Before anything is generated, look up who is asking: their role, business unit, territory, and assigned accounts. Then give the model only the tables and columns that person can use. Sensitive fields such as customer IDs, contract terms, or salaries should be tagged in your metadata catalog so they are never offered in the first place. It sounds obvious, but it is easy to skip when a demo is working. A prompt is not a permission system It is tempting to add "only show my territory" to the system prompt and move on. I understand the appeal. But a prompt is a suggestion, and the model can ignore it, forget it, or misread it on a hard question. A better setup parses the generated SQL and rewrites it before anything runs, so the rules live in code that the model cannot talk its way around. In the benchmark behind this work, the most common fix across 900 test queries was adding a row filter to the WHERE clause (