Back to Blog
SQL

Using SQL with AI Tools: A Practical Guide for 2026

Vajo Lukic
June 20, 2026
7 min read
Using SQL with AI Tools: A Practical Guide for 2026

AI coding assistants have changed how most developers write SQL. Used well, they're a significant productivity multiplier. Used carelessly, they produce queries that look correct, run without errors, and return wrong results.

This guide is about using them well.

The Three Things You Must Give AI Before Asking for SQL

Most poor AI-generated SQL comes from insufficient context. Before asking any AI tool for a query, provide:

1. Your Schema

CREATE TABLE orders (
    order_id    SERIAL PRIMARY KEY,
    customer_id INTEGER NOT NULL,
    status      TEXT CHECK (status IN ('pending', 'paid', 'shipped', 'cancelled', 'refunded')),
    total       NUMERIC(10, 2),
    created_at  TIMESTAMPTZ DEFAULT NOW()
);

CREATE TABLE order_items (
    item_id    SERIAL PRIMARY KEY,
    order_id   INTEGER REFERENCES orders(order_id),
    product_id INTEGER,
    quantity   INTEGER NOT NULL,
    unit_price NUMERIC(10, 2) NOT NULL
);

Paste the actual CREATE TABLE statements. Without the schema, AI guesses column names and types — and it guesses wrong for anything non-generic.

2. Your Database and Version

State your database upfront: PostgreSQL 16, MySQL 8.0, BigQuery, Snowflake. SQL dialects differ in ways that matter. DATE_TRUNC is PostgreSQL syntax. BigQuery uses DATE_TRUNC(date, 'month') with a different argument order. MySQL didn't support CTEs before version 8.0. Without this, AI generates syntax that may not run on your database.

3. Your Business Definitions

Natural language is ambiguous. Clarify terms before AI translates them:

"Active users" = users who triggered at least one 'page_view' event in the last 30 calendar days
"Revenue" = SUM of orders.total WHERE status IN ('paid', 'shipped') — excludes pending and refunded
"Last month" = the previous complete calendar month, not rolling 30 days

Business logic lives in your head, not in AI's training data.


A Prompting Template That Works

Context:
- Database: PostgreSQL 16
- Schema: [paste CREATE TABLE statements]
- Business definitions: [define ambiguous terms]

Task:
[describe what you want in plain English]

Requirements:
- Use CTEs for readability
- Handle NULLs explicitly
- Don't use SELECT *
- Add index hints if filtering on non-primary-key columns

This structure gives AI enough context to produce a query you can actually use.


Verifying AI-Generated SQL: A Checklist

Never run AI-generated SQL on production data without checking these:

NULL handling

-- AI often writes this:
WHERE discount != 0
-- This silently excludes rows where discount IS NULL

-- Correct:
WHERE discount != 0 OR discount IS NULL
-- Or:
WHERE COALESCE(discount, 0) != 0

JOIN type is correct

-- AI defaults to INNER JOIN
-- If you want customers with no orders included, you need LEFT JOIN:

-- Wrong (loses customers with no orders):
SELECT c.name, COUNT(o.order_id)
FROM customers c
INNER JOIN orders o ON c.id = o.customer_id
GROUP BY c.name;

-- Correct:
SELECT c.name, COUNT(o.order_id)
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id
GROUP BY c.name;

No functions wrapping indexed columns

-- AI often writes this — breaks index usage:
WHERE DATE(created_at) = '2026-06-01'
WHERE LOWER(email) = 'user@example.com'
WHERE YEAR(order_date) = 2026

-- Index-safe equivalents:
WHERE created_at >= '2026-06-01' AND created_at < '2026-06-02'
WHERE email = LOWER('User@Example.com')   -- normalize the input, not the column
WHERE order_date >= '2026-01-01' AND order_date < '2027-01-01'

LIMIT is present on exploratory queries

AI doesn't add LIMIT by default. A missing LIMIT on a 100M-row table returns 100M rows to your client and potentially crashes your app.

GROUP BY includes all non-aggregated SELECT columns

In strict SQL mode (PostgreSQL, BigQuery), every column in SELECT that isn't inside an aggregate function must appear in GROUP BY. AI sometimes misses this, especially in complex CTEs.


Prompts That Consistently Work Well

"Explain this query"

Paste a complex query and ask Claude or ChatGPT to explain it step by step. AI is excellent at this — it can walk through a multi-CTE query and explain what each step computes. Useful for onboarding to an unfamiliar codebase.

"Optimize this query"

This query takes 8 seconds on PostgreSQL 16 with 50M orders.
Indexes: idx_orders_customer_id, idx_orders_created_at.
EXPLAIN ANALYZE output: [paste it]
Suggest specific optimizations.

Give AI the EXPLAIN output and it can identify obvious issues like sequential scans, missing indexes, or inefficient join order.

"Rewrite for [dialect]"

Rewrite this PostgreSQL query for BigQuery.
Note: we use partitioned tables on the order_date column.

Dialect translation is where AI saves the most time. It handles syntax differences well when you specify the target dialect and version.

"Add error handling to this dbt model"

AI understands dbt syntax and can add tests, documentation blocks, and NULL handling to existing models.


When NOT to Use AI for SQL

Multi-step data migrations

-- Don't let AI write this unsupervised:
UPDATE orders SET status = 'archived'
WHERE created_at < '2020-01-01';

A wrong date or missing WHERE clause silently modifies millions of rows. Write migrations yourself, run with BEGIN / ROLLBACK first to verify the affected row count, then COMMIT.

Complex business logic with edge cases

If the correct answer depends on a business rule not obvious from the schema (e.g., "an order is 'complete' only if all items have shipped AND no refund exists AND payment cleared"), write the logic yourself. AI will produce something that looks right but misses the edge cases.

Anything involving PII or sensitive data

Don't paste real customer data, real emails, or real financial records into a public AI tool. Use schema-only context for your prompts.


Tool Comparison

Tool Best for
Claude Explanation, reasoning about schema design, complex multi-step queries
ChatGPT Query generation, dialect translation, quick lookups
GitHub Copilot In-editor SQL completion, inline suggestions as you type
Cursor Full-file SQL editing with codebase context
DBeaver AI SQL completion directly inside a database client

All of these produce better output when you understand SQL well enough to verify and correct them.


Frequently Asked Questions

Is it safe to paste my database schema into ChatGPT?

Schema definitions (CREATE TABLE statements) don't contain actual data, so the risk is lower than pasting rows. However, the schema itself can reveal sensitive business information — table names, relationships, and column names may be proprietary. For sensitive systems, use a local AI model or ensure your prompts don't include data that would concern your security team.

Can AI write SQL tests (dbt tests, data quality checks)?

Yes, and this is one of the better use cases. AI can generate dbt test definitions, SQL assertions, and freshness checks. Example: "write a dbt test that verifies no order total is less than the sum of its line items." Review them before running, but the scaffolding is reliable.

Which AI tool writes the best SQL?

As of 2026, Claude and GPT-4o are roughly equivalent for most SQL tasks. Claude tends to produce more readable, CTE-structured queries with explicit NULL handling. Copilot is more convenient for in-context completion while writing code. The difference between tools is smaller than the difference between prompting with and without schema context.


Want to build the SQL foundation that makes AI tools actually useful? See Is SQL Still Relevant in the Age of LLMs? for the full picture, or ChatGPT Can't Replace Your SQL Skills for a deeper look at what AI still gets wrong. For structured hands-on practice, the SQL Crash Course covers 350+ examples from basics to expert level.

Have a prompting technique that works well for SQL? Get in touch.

#sql#ai#chatgpt#copilot#claude#productivity#llms

Enjoyed this article? Share it!

About the Author

VL

Vajo Lukic

Vajo Lukic is a technology leader with 20+ years of experience in software development and system administration. Author of The Practical Linux Handbook, he shares practical, field-tested knowledge to help developers and IT professionals master Linux fundamentals.

Read more about Vajo

Related Articles

Is SQL Still Relevant in 2026? SQL in the Age of LLMs

Is SQL Still Relevant in 2026? SQL in the Age of LLMs

SQL is more valuable than ever in the LLM era. Here's why data professionals still need SQL even with AI tools like ChatGPT and Copilot.

Read more →
ChatGPT Can't Replace Your SQL Skills — Here's Why

ChatGPT Can't Replace Your SQL Skills — Here's Why

ChatGPT generates SQL, but it doesn't know your schema, can't optimize for your indexes, and gets NULL handling wrong. Here's what AI still can't do with SQL.

Read more →
SQL for AI Engineers: Querying Vector Databases and LLM Outputs

SQL for AI Engineers: Querying Vector Databases and LLM Outputs

How AI engineers use SQL to query vector databases, extract LLM outputs, and build ML feature pipelines. With pgvector, BigQuery, and dbt examples.

Read more →

Ready to Transform Your Life?

Get the complete guide to personal transformation and start your journey today.

Get the Book