<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gia</title>
    <description>The latest articles on DEV Community by Gia (@giaa_a5d787a32b92a).</description>
    <link>https://dev.to/giaa_a5d787a32b92a</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4073945%2F7890742c-3174-4618-89b3-574ba9fe2804.png</url>
      <title>DEV Community: Gia</title>
      <link>https://dev.to/giaa_a5d787a32b92a</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/giaa_a5d787a32b92a"/>
    <language>en</language>
    <item>
      <title>From Question to Query: Building an AI Database Assistant</title>
      <dc:creator>Gia</dc:creator>
      <pubDate>Wed, 12 Aug 2026 03:13:16 +0000</pubDate>
      <link>https://dev.to/giaa_a5d787a32b92a/from-question-to-query-building-an-ai-database-assistant-2e6e</link>
      <guid>https://dev.to/giaa_a5d787a32b92a/from-question-to-query-building-an-ai-database-assistant-2e6e</guid>
      <description>&lt;p&gt;Ever wished you could just ask your database a question — in plain English — and get a real answer back? No SQL, no waiting on the data team. That's the whole promise of &lt;strong&gt;Natural Language to SQL&lt;/strong&gt; (NL2SQL for short), and thanks to LLMs, it finally works well enough to be useful.&lt;/p&gt;

&lt;p&gt;But here's the part most people get wrong: getting an AI to write SQL is the easy bit. The magic is in everything that happens around it. In this article we'll build the whole picture together, one piece at a time. &lt;/p&gt;

&lt;h2&gt;
  
  
  What we're building
&lt;/h2&gt;

&lt;p&gt;We're going to trace the full journey of a single question. Here's what the AI has to do, in order:&lt;br&gt;
• &lt;strong&gt;Read the database&lt;/strong&gt; — what tables and columns are even in here?&lt;br&gt;
• &lt;strong&gt;Find what matters&lt;/strong&gt; — pick the few tables that relate to the question&lt;br&gt;
• &lt;strong&gt;Write the SQL&lt;/strong&gt; — turn your words into a query&lt;br&gt;
• &lt;strong&gt;Check it's safe&lt;/strong&gt; — before it ever touches the database&lt;br&gt;
• &lt;strong&gt;Run it&lt;/strong&gt; — and hand back the answer&lt;/p&gt;

&lt;p&gt;Miss any one of these and the whole thing gets flaky. So all five matter — and we'll see why as we go. Let’s walk each stop.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Get to know the database
&lt;/h2&gt;

&lt;p&gt;The AI can't answer anything until it knows what it's working with. So first it takes a look around — a process called &lt;strong&gt;schema introspection.&lt;/strong&gt;&lt;br&gt;
The good news: databases can describe themselves. Most expose their blueprint through a built-in view called &amp;nbsp;&lt;a href="https://dev.mysql.com/doc/refman/8.0/en/information-schema.html" rel="noopener noreferrer"&gt;&lt;code&gt;information_schema&lt;/code&gt;&lt;/a&gt;   so we just ask:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F02uezwfcc6d51zrkqzfu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F02uezwfcc6d51zrkqzfu.png" alt=" " width="672" height="143"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But names alone can be cryptic. What does a column called rev mean? Is status 2 good or bad? So the AI also grabs little hints — descriptions, comments, and a few sample values — to make sense of it all. Then it &lt;strong&gt;caches&lt;/strong&gt; this map, because rebuilding it on every question would be painfully slow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Zoom in on what matters
&lt;/h2&gt;

&lt;p&gt;Here's a trap: real databases can have hundreds of tables. It's tempting to just dump them all into the prompt and let the AI figure it out. Don't. LLMs have a limited &lt;strong&gt;context window&lt;/strong&gt; (how much text they can read at once), and stuffing it with junk actually makes the AI worse — the useful tables get lost in the noise. It's expensive, too.&lt;br&gt;
Instead, we hand the AI only the tables it needs, using &lt;strong&gt;semantic search&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Ahead of time, turn every table description into an **embedding **and store it in a &amp;nbsp;&lt;a href="https://www.pinecone.io/learn/vector-database/" rel="noopener noreferrer"&gt;vector store&lt;/a&gt;&amp;nbsp;  .&lt;/li&gt;
&lt;li&gt; When a question arrives, embed it too and search for the closest-matching tables.&lt;/li&gt;
&lt;li&gt; Pull in any &lt;strong&gt;connected tables&lt;/strong&gt; (via foreign keys) so joins still work.&lt;/li&gt;
&lt;li&gt; Add &lt;strong&gt;business definitions&lt;/strong&gt; — like what "active user" or "revenue" means in your company.
This step is the unsung hero. It's what keeps the SQL accurate and the bill small, even when the database is huge.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 3: Let the AI write the SQL
&lt;/h2&gt;

&lt;p&gt;Now the fun part. We build a *&lt;em&gt;prompt *&lt;/em&gt;— the instructions we send the model — with everything it needs: a few ground rules (which SQL flavor, keep it read-only, no SELECT *), the tables we picked, a couple of &lt;a href="https://learnprompting.org/docs/basics/few_shot" rel="noopener noreferrer"&gt;worked examples&lt;/a&gt;  , and finally the question.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fis86u3jxno1yy6ljd2p1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fis86u3jxno1yy6ljd2p1.png" alt=" " width="640" height="272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That trick of showing a couple of example question-and-SQL pairs has a name: &lt;strong&gt;few-shot prompting&lt;/strong&gt;. And it works surprisingly well — a few good examples teach the model how your database likes to be queried far better than a wall of instructions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Never trust, always check
&lt;/h2&gt;

&lt;p&gt;Rule number one: &lt;strong&gt;don't run whatever the AI spits out&lt;/strong&gt;. LLMs sometimes &lt;strong&gt;hallucinate&lt;/strong&gt; — they invent a table or column that doesn't exist, or write something risky. So every query goes through a safety gate first. We &lt;strong&gt;parse it&lt;/strong&gt; (read its structure with a tool like &amp;nbsp;&lt;a href="https://sqlglot.com/" rel="noopener noreferrer"&gt;&lt;code&gt;sqlglot&lt;/code&gt;&lt;/a&gt;  ) to confirm it's valid and a single statement, confirm it's &lt;strong&gt;read-only&lt;/strong&gt;, and check that the tables and columns are real.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fktz5rfkkfitcmy0h0yh2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fktz5rfkkfitcmy0h0yh2.png" alt=" " width="648" height="160"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;See that last line? EXPLAIN asks the database to plan the query without actually running it — a cheap way to catch mistakes before a single row is read. Only once everything passes do we run it for real, on a &lt;strong&gt;read-only connection&lt;/strong&gt; with a row cap and a timeout, so nothing can go rogue. Whatever comes back is the answer your user sees. &lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;Here’s the thing I hope sticks with you: the AI writing the SQL isn’t the clever part. That’s just the last little step. The real work — the stuff that makes it actually trustworthy — is everything happening quietly in the background.&lt;br&gt;
Think of the AI like a really good translator at a busy restaurant. It only does its job well because it already knows the menu (your schema), it listens for what you actually want instead of everything on the wall (the right context), and it double-checks the order before it hits the kitchen (validation). Skip any of those and you get the wrong dish — fast, confidently, and completely wrong.&lt;br&gt;
But get them right, and something kind of magical happens. The person asking doesn’t need to know SQL. They don’t need to file a ticket. They don’t need to wait on anyone. They just… ask — and a real answer comes back. That’s the whole dream, and it’s a lot closer than it used to be. &lt;/p&gt;

&lt;p&gt;*&lt;em&gt;So next time someone says “the AI just writes the query,” you’ll know the truth: the query is easy. Everything around it is where the magic lives. &lt;br&gt;
*&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>sql</category>
      <category>llm</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
