Natural Language to T-Sql issue: sqlcoder-7b-2 fails on complex T-SQL joins & date logic (offline, 40GB GPU)

Project Overview

I am building a Natural Language → T-SQL system for Microsoft SQL Server (T-SQL).

Expected behavior:
If a user asks a natural-language question (e.g.,
“How many users are using smartphones last month?”),
the system should generate a valid and logically correct T-SQL query.


Constraints

  • Maximum GPU memory: 40 GB
  • Deployment: Local GPU inference only
  • No internet access after training (fully offline deployment)
  • This restricts model size and external API usage

Current Architecture

  • LLM: defog/sqlcoder-7b-2
  • Fine-tuning: ~2,500 complex SQL queries
    • Multi-table JOINs
    • Aggregations
    • Date logic
  • Schema Handling (RAG):
    • Tables and column descriptions stored separately
    • Embedded using MiniLM
    • Retrieved via cosine similarity
  • Generation Flow:
    1. User NL query
    2. Retrieve relevant schema context
    3. Inject schema into prompt
    4. Generate T-SQL

What Works

  • Simple queries
  • Single-table queries
  • WHERE / GROUP BY / HAVING
  • Basic aggregations

Issue

For complex queries involving:

  • Multiple JOINs
  • SQL Server date functions (DATEADD, DATEDIFF, CONVERT)
  • Cross-table business logic

the model often:

  • Chooses incorrect JOIN paths
  • Misses required tables
  • Hallucinates columns
  • Produces SQL Server–invalid date syntax
  • Generates logically incorrect queries

This happens despite fine-tuning and schema grounding.


Questions

  1. Is this mainly a 7B model limitation for complex for this project?
  2. Would explicitly injecting foreign-key relationships / join graphs into the prompt help?
  3. Is a query-planning stage (join planning → filters → final SQL) recommended?
  4. Any best practices for T-SQL–specific correctness?
  5. Given offline + 40 GB GPU constraints, would:
    • Larger quantized models
    • Multi-stage planners
    • Rule-based join resolution + LLM
      be more reliable?
  6. Are there any open-source or production-grade Natural Language to Sql architectures that handle complex joins reliably under similar constraints?

Goal

To generate correct, production-ready T-SQL for complex NL queries under offline and 40 GB GPU constraints.

Thanks in advance for any guidance or references!

1 Like

While the small size of the model seems to be one factor, it appears that even large models may struggle to solve the problem unless the task is broken down.

1 Like