Skip to content

AI tool · SQL

AI SQL query generator (text to SQL)

Describe what you need in plain English and paste your tables: get one SQL query and how it works, on any of 27 models. We ran every model's queries on a sample database to see whose rows were right.

Runs on Plus and Pro, on the model you pick

Up to 1,500 characters.

Try an example:

By running it, you agree to the Terms and Privacy Policy and confirm you’re 18 or older. We keep what you send and its answer for 7 days, or in your account if you sign up.

Test runs checked .

AI SQL query generator: three examples

Three inputs and the query GPT-6 Luna gave each, made once on September 28, 2026 and shown as it came back. The example buttons in the box above show the same.

Top customers

Tables: customers(id, name, email, created_at) orders(id, customer_id, total_amount, status, created_at) Question: the 5 customers who spent the most on paid orders this year, with their total.

```sql
SELECT
  c.id,
  c.name,
  c.email,
  SUM(o.total_amount) AS total_spent
FROM customers AS c
JOIN orders AS o
  ON o.customer_id = c.id
WHERE o.status = 'paid'
  AND o.created_at >= date_trunc('year', CURRENT_DATE)
  AND o.created_at < date_trunc('year', CURRENT_DATE) + INTERVAL '1 year'
GROUP BY c.id, c.name, c.email
ORDER BY total_spent DESC, c.id
LIMIT 5;
```

This sums paid orders placed during the current calendar year for each customer.
The date range includes January 1 and excludes January 1 of next year.
Customers with no qualifying paid orders are omitted; ties are ordered by customer ID.
Users with no orders

Tables: users(id, email, signed_up_at) orders(id, user_id, placed_at) Question: users who signed up in the last 30 days and haven't placed an order yet.

```sql
SELECT u.id, u.email, u.signed_up_at
FROM users AS u
WHERE u.signed_up_at >= NOW() - INTERVAL 30 DAY
  AND u.signed_up_at <= NOW()
  AND NOT EXISTS (
    SELECT 1
    FROM orders AS o
    WHERE o.user_id = u.id
  );
```

The date range includes users who signed up from 30 days ago through now.
`NOT EXISTS` excludes anyone with an order, without duplicating users.
Monthly totals

Tables: invoices(id, client_id, amount, issued_on, paid_on) Question: for each month of 2026, the amount invoiced and how much of it has been paid.

```sql
WITH months AS (
    SELECT CAST('2026-01-01' AS date) AS month_start
    UNION ALL
    SELECT DATEADD(month, 1, month_start)
    FROM months
    WHERE month_start < CAST('2026-12-01' AS date)
)
SELECT
    m.month_start,
    COALESCE(SUM(i.amount), 0) AS amount_invoiced,
    COALESCE(SUM(CASE WHEN i.paid_on IS NOT NULL THEN i.amount ELSE 0 END), 0) AS amount_paid
FROM months AS m
LEFT JOIN invoices AS i
    ON i.issued_on >= m.month_start
   AND i.issued_on < DATEADD(month, 1, m.month_start)
GROUP BY m.month_start
ORDER BY m.month_start
OPTION (MAXRECURSION 12);
```

The query returns all 12 months, including months with no invoices.
Invoices are counted in the month they were issued; an invoice is treated as paid if `paid_on` is non-NULL.
The date range includes all issued dates in each month, regardless of time of day.

Which AI model is best at this? We tested every one

In our test runs of 5 AI SQL query generator inputs on 27 models, GLM 5.3 Flash, Claude Haiku 5.5, DeepSeek V4.1 Flash and 17 more passed the most: 5 of 5. That includes Claude Haiku 5.5 (an everyday model on every plan).

Run on November 19, 2026: 5 inputs on 27 models, each with this tool's own instructions.

Each model's results on the tool's test inputs
ModelPassedHard onesMessages used on ProCost per runTime per run
GLM 5.3 FlashZ.ai5 of 52 of 21 each, of 60 a day on Pro$0.00021.8 s
Claude Haiku 5.5Anthropic5 of 52 of 21 each, of 60 a day on Pro$0.00054.1 s
DeepSeek V4.1 FlashDeepSeek5 of 52 of 21 each, of 60 a day on Pro$0.00061.6 s
GPT-6.1 TerraOpenAI5 of 52 of 21 each, of 250 a month on Pro$0.00095.6 s
Claude Haiku 4.5Anthropic5 of 52 of 21 each, of 250 a month on Pro$0.00152.0 s
Mistral Large 4Mistral5 of 52 of 21 each, of 250 a month on Pro$0.003625.9 s
DeepSeek V4 ProDeepSeek5 of 52 of 21 each, of 250 a month on Pro$0.00447.0 s
Grok 4.7xAI5 of 52 of 21 each, of 250 a month on Pro$0.008616.5 s
GPT-6.1 SolOpenAI5 of 52 of 21 each, of 125 a month on Pro$0.00187.7 s
GPT-6 SolOpenAI5 of 52 of 21 each, of 125 a month on Pro$0.00193.6 s
GPT-6.2 SolOpenAI5 of 52 of 21 each, of 125 a month on Pro$0.00293.3 s
Kimi K3Moonshot5 of 52 of 21 each, of 125 a month on Pro$0.00462.9 s
Gemini 4 HydrogenGoogle5 of 52 of 21 each, of 125 a month on Pro$0.00486.7 s
Gemini 3.1 Pro (preview)Google5 of 52 of 21 each, of 125 a month on Pro$0.00658.6 s
Gemini 4 ArgonGoogle5 of 52 of 21 each, of 62 a month on Pro$0.00186.9 s
Gemini 4 CarbonGoogle5 of 52 of 21 each, of 62 a month on Pro$0.006215.4 s
Claude Opus 5.5Anthropic5 of 52 of 21 each, of 62 a month on Pro$0.02007.7 s
GPT-6 AstraOpenAI5 of 52 of 21 each, of 31 a month on Pro$0.00814.4 s
GPT-6.2 AstraOpenAI5 of 52 of 21 each, of 31 a month on Pro$0.01033.6 s
Claude Fable 5.2Anthropic5 of 52 of 21 each, of 31 a month on Pro$0.03467.1 s
GPT-6 LunaOpenAI4 of 52 of 21 each, of 60 a day on Pro$0.000113.4 s
Gemini 3.8 FlashGoogle4 of 52 of 21 each, of 250 a month on Pro$0.00063.0 s
GLM 5.3Z.ai4 of 52 of 21 each, of 250 a month on Pro$0.00102.1 s
Kimi K3.1 ProMoonshot4 of 52 of 21 each, of 125 a month on Pro$0.00418.1 s
Claude Fable 5.1Anthropic4 of 52 of 21 each, of 31 a month on Pro$0.03075.9 s
Claude Sonnet 5Anthropic3 of 52 of 21 each, of 125 a month on Pro$0.00513.9 s
Claude Sonnet 5.5Anthropic3 of 52 of 21 each, of 125 a month on Pro$0.00734.4 s
Passed: of the 5 test inputs, how many answers passed their check (an answer the provider failed to give isn't counted). Messages used: what each run counted as on Pro, and Pro's count on the model. Cost: what OpenRouter charged us per run, on average; in llmwise you pay per message, not per token. Time: from sending to the whole answer.

Side by side: a second order within 60 days

The same input on GLM 5.3 Flash and Claude Haiku 5.5, as each answered it, unedited.

Write one SQL query for this, in a ```sql code block, then a few lines on how it works. Use only these tables and columns. Write it for SQLite.

---
Tables (SQLite):
CREATE TABLE customers (id INTEGER PRIMARY KEY, name TEXT NOT NULL, country TEXT NOT NULL, signed_up TEXT NOT NULL);
CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, category TEXT NOT NULL, price_cents INTEGER NOT NULL);
CREATE TABLE orders (id INTEGER PRIMARY KEY, customer_id INTEGER NOT NULL REFERENCES customers(id), ordered_on TEXT NOT NULL, status TEXT NOT NULL);
CREATE TABLE order_items (order_id INTEGER NOT NULL REFERENCES orders(id), product_id INTEGER NOT NULL REFERENCES products(id), quantity INTEGER NOT NULL);

Question: the customers whose second order came within 60 days of their first, ignoring cancelled orders and ordering each customer's orders by date. Return only their names, sorted by name.
GLM 5.3 Flash
```sql
WITH ranked AS (
  SELECT
    o.customer_id,
    o.ordered_on,
    ROW_NUMBER() OVER (
      PARTITION BY o.customer_id
      ORDER BY o.ordered_on, o.id
    ) AS rn
  FROM orders o
  WHERE o.status <> 'cancelled'
),
firsts AS (
  SELECT customer_id, ordered_on AS first_on FROM ranked WHERE rn = 1
),
seconds AS (
  SELECT customer_id, ordered_on AS second_on FROM ranked WHERE rn = 2
)
SELECT c.name
FROM seconds s
JOIN firsts f ON f.customer_id = s.customer_id
JOIN customers c ON c.id = s.customer_id
WHERE julianday(s.second_on) - julianday(f.first_on) <= 60
ORDER BY c.name;
```

The CTE ranks each customer's non-cancelled orders by date (id as a tie-breaker so rows with the same timestamp get distinct numbers). It then pulls each customer's 1st and 2nd order separately, so customers with only one order are naturally excluded. `julianday` converts the TEXT dates to days so the gap is a plain subtraction; SQLite string dates like 'YYYY-MM-DD' convert correctly. If your dates include times or another format, check that `julianday` still parses them.
Claude Haiku 5.5
```sql
WITH ranked AS (
    SELECT customer_id,
           ordered_on,
           ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY ordered_on, id) AS rn
    FROM orders
    WHERE status <> 'cancelled'
),
firsts AS (
    SELECT customer_id,
           MAX(CASE WHEN rn = 1 THEN julianday(ordered_on) END) AS first_day,
           MAX(CASE WHEN rn = 2 THEN julianday(ordered_on) END) AS second_day
    FROM ranked
    WHERE rn <= 2
    GROUP BY customer_id
)
SELECT c.name
FROM firsts f
JOIN customers c ON c.id = f.customer_id
WHERE f.second_day IS NOT NULL
  AND f.second_day - f.first_day <= 60
ORDER BY c.name;
```

The first CTE drops cancelled orders and numbers each customer's remaining orders by date, using id to break ties between orders on the same day. The second CTE pulls each customer's first and second order dates into one row, and customers with fewer than two valid orders get a NULL second date and are excluded. The final filter keeps customers whose second order falls within 60 days of the first, using julianday so the day difference is exact. This assumes the status value is spelled 'cancelled' and that ordered_on is in ISO format (YYYY-MM-DD). If two different customers share a name, that name will appear once per customer.

How the inputs are scored

  • Query result: the query runs on a small sample shop database (SQLite) and must return the same rows as our reference query.

The inputs: a filter and a sort, including customers with no orders, units sold, with ties, a second order within 60 days, and each category's share of revenue. Every model gets the tool's own instructions and up to Pro's reply limit. The method is the same as our other test runs.

How to use the AI SQL query generator

  1. Paste your tables as “table(column, column, …)” or as CREATE TABLE statements, then your question in plain English. Up to 1,500 characters.

  2. Pick your database: PostgreSQL, MySQL, SQLite or SQL Server.

  3. Press “Write the query”. It runs on your plan (Plus or Pro) on the model you pick, and the whole answer streams in.

  4. Run it on a copy or with a LIMIT first, and check a few rows by hand.

Tips for a better answer

  • Paste the real columns

    Paste the CREATE TABLE statements, or at least every column you might need. The query can only be as right as the tables it knows.

  • Say what one row means

    Say what a row of the answer is: “one row per customer”, “one row per month”. It decides the GROUP BY for you.

  • Mention the empty cases

    “Customers who never ordered”, “including months with no sales”: say whether empty cases count, or the query will quietly drop them.

  • Room for a big schema

    Paste a large schema, up to 20,000 characters, and keep refining the query in the chat.

What a good query looks like

  • The right rows, not just no error

    Run it and look at the rows, not just whether it runs. A query that joins orders to their items can count an order twice; totals that look high usually mean a join multiplied them.

  • Only your columns

    Every table and column in the query should be one you gave. An invented column (a “total” your table doesn't have) is the most common way text-to-SQL goes wrong.

  • NULLs and missing rows

    Customers with no orders, products never sold, a NULL in the middle of a sum: ask yourself whether they should be in the answer, and check the query agrees.

  • Your database's dialect

    Dates, string concatenation and “top N” differ between PostgreSQL, MySQL, SQLite and SQL Server. The query should use your database's way.

What you need to run it, and what a run gives

  • Running it takes a plan, Plus or Pro. There's no free plan or trial: a plan is paid at checkout and renews every month until you cancel. Cancel in two clicks in Settings → Billing and you keep your plan to the end of the month you paid for. llmwise accounts are for people 18 and older.

  • Paste up to 20,000 characters in a run. It runs on the model you pick, shows the whole answer, and you can carry on in the chat with any model your plan has, or ask another model the same thing.

  • A run counts on your plan like any other message: the table above shows what one counted as on Pro, model by model.

More AI tools are on the tools page, and what each plan gives is on pricing.

Questions

Is the SQL query generator free?

No: there's no free plan or trial. It runs on Plus and Pro, on any model your plan has; Claude Fable 5.2, GPT-6.2 Astra, and their earlier versions need Pro. Paid monthly from the day you start. Cancel in two clicks in Settings → Billing.

Does it connect to my database?

No. It only sees what you paste: table and column names are enough, and it never needs your data. Don't paste passwords, keys or customers' personal data.

Which SQL dialects does it support?

PostgreSQL, MySQL, SQLite and SQL Server. Pick yours: dates, limits and string functions are written the way your database expects.

Can it write UPDATE or DELETE queries?

It writes read-only SELECT queries by default. If you ask for an UPDATE or DELETE it writes one with a warning to run it inside a transaction and check the row count first.

Which AI writes the most accurate SQL?

Our test runs below ran every model's query on the same sample shop database and compared its rows with the right answer's, from a simple filter to a date gap between orders.

Which AI is best for this, in your tests?

In our test runs of 5 AI SQL query generator inputs on 27 models, GLM 5.3 Flash, Claude Haiku 5.5, DeepSeek V4.1 Flash and 17 more passed the most: 5 of 5. That includes Claude Haiku 5.5 (an everyday model on every plan). The table on this page has every model's result, what a run counts as on Pro, and the time it took.

Claude, GPT, Gemini, DeepSeek, Grok, Kimi, GLM, and Mistral, in one chat.

See what a message costs before you send it. Every plan is month to month: cancel in two clicks. Sign in with an email link, no password.