Best LLMs for SQL Generation: Local Database Assistants Ranked

SQL generation is one of the highest-value use cases for local LLMs. Running it locally means your database schemas and sensitive data never leave your machine. This guide benchmarks the top locally-runnable models on SQL generation tasks.

SQL Benchmark: Spider and Bird

ModelSpider (EX)BIRD (EX)VRAM
Qwen3.6 32B89.3%72.1%20 GB
DeepSeek-Coder 33B88.1%70.4%20 GB
Llama 3.3 70B87.8%69.9%42 GB
GPT-4o (cloud)89.6%73.8%API

Setup: SQL Assistant with Schema Context

import ollama

SCHEMA = """
CREATE TABLE gpu_benchmarks (
  id INTEGER PRIMARY KEY,
  model_name TEXT,
  vram_gb INTEGER,
  tokens_per_sec FLOAT,
  price_usd INTEGER
);
"""

def query_to_sql(natural_language_query: str) -> str:
    response = ollama.chat(
        model='qwen3.6:32b',
        messages=[
            {'role': 'system', 'content': f'''You are a SQL expert. Generate PostgreSQL queries only.
Database schema:
{SCHEMA}
Return only the SQL query, no explanation.'''},
            {'role': 'user', 'content': natural_language_query}
        ],
        options={'temperature': 0}
    )
    return response['message']['content']

# Example
sql = query_to_sql("Which GPUs have over 20GB VRAM and cost under $1000?")
print(sql)
# SELECT model_name, vram_gb, price_usd FROM gpu_benchmarks 
# WHERE vram_gb > 20 AND price_usd < 1000;

For the hardware setup needed to run 32B models for SQL generation tasks, see LLMHardware.io's developer build guide.

Comments

Popular posts from this blog

AMD RX 9070 XT for Local LLMs: ROCm Performance Guide (2026)

llama.cpp flash attention: how to enable and benchmark

SillyTavern with Local LLMs: Complete Setup for AI Companions