SQL is the universal language of data work. AI can write almost any analytical query you can describe — but the quality of the output depends entirely on whether the AI knows your schema, your dialect, and your performance constraints. This topic shows you how to brief AI for SQL that runs the first time, on the warehouse you actually use.
Most data scientists write SQL every day — for ad-hoc questions, dashboard queries, ETL transforms, and ML feature pipelines. AI is one of the highest-leverage assistants for SQL because the syntax is dense, the dialect quirks are many, and a single missing GROUP BY clause can silently change a result. A well-briefed prompt produces correct, performant, dialect-aware SQL in seconds. A vague prompt produces ANSI-flavoured guesses that won't run in BigQuery, Snowflake, or Postgres without rewriting. This tutorial gives you the patterns to get the good outcome every time.
SQL prompts have one extra ingredient compared to Pandas prompts: the dialect. PostgreSQL, MySQL, BigQuery, Snowflake, Redshift, Databricks SQL, and DuckDB all share roughly 80% of their syntax — and the remaining 20% is where SQL errors hide. Date functions, window frames, array handling, JSON access, and pivot syntax differ across all of them. State your dialect at the very top of the prompt and the AI will produce code that runs.
The other key element is the schema brief: table names, column names, data types, primary keys, and foreign-key relationships. SQL prompts without schemas produce queries with fabricated joins — usually almost-right joins that introduce duplicates you only notice three days later.
Use this structure at the top of every SQL prompt:
Reusable SQL brief
Dialect: BigQuery (Standard SQL).
Schema: analytics_warehouse
Tables:
customers (
customer_id INT64 PK,
signup_ts TIMESTAMP,
plan_type STRING, -- basic | pro | enterprise
country STRING
)
orders (
order_id INT64 PK,
customer_id INT64 FK -> customers,
order_ts TIMESTAMP,
status STRING, -- placed | shipped | returned
gross_value_gbp NUMERIC,
discount_pct NUMERIC -- 0..0.45, nullable
)
events (
event_id INT64 PK,
customer_id INT64 FK -> customers,
event_ts TIMESTAMP,
event_type STRING
)
Notes:
- orders has ~28M rows, partitioned by DATE(order_ts).
- events has ~520M rows, clustered by customer_id.
- Returns count as negative revenue per business rule.
Weak prompt
Write a SQL query to find my top customers by revenue.
No dialect, no schema, no time window, no definition of "top" or "revenue". The AI will hallucinate tables like sales and customers, choose a generic SELECT with ANSI date functions, and produce a query that fails on the first run.
Stronger prompt
Act as a senior analytics engineer writing BigQuery SQL.
Schema (paste reusable brief from above).
Task: identify the top 50 customers by net revenue
in the last 90 days, where net revenue per order =
gross_value_gbp * (1 - COALESCE(discount_pct, 0))
and only status = 'shipped' contributes.
Requirements:
- Use a CTE per logical step (orders_filtered,
orders_with_net, revenue_per_customer).
- Use partition pruning: filter on order_ts
before joining.
- Return columns: customer_id, customer_country,
plan_type, total_net_revenue_gbp, order_count.
- Sort by total_net_revenue_gbp DESC, limit 50.
- Add a top-of-query comment block explaining
the business definition of "net revenue".
You get clean, idiomatic BigQuery SQL: three CTEs, a partition-pruned filter on order_ts, a COALESCE on the discount, and a final aggregate that joins back to customers on the customer key. No hallucinated tables, no dialect mistakes.
The pattern is: dialect → schema brief → task → business rule → performance notes → output structure. Dialect is non-negotiable. Schema brief eliminates fabrication. Business rule (here: "returns count as negative revenue") removes ambiguity. Performance notes (partitioning, clustering) make sure the query runs in reasonable time on real-world volumes.
For complex multi-step analyses, ask the AI to use CTEs rather than nested subqueries. The phrase "Use one CTE per logical step and name them clearly" produces queries that read top-to-bottom like a paragraph — much easier to review and modify later.
LIMIT 1000 version of the query before running on the full table — much cheaper iteration.Tip: Ask AI to write the query and a brief plain-English explanation of each CTE. The explanation doubles as documentation when you commit the query to your dbt or analytics repo.
Pick a warehouse you use. Build a reusable schema brief for your three most-used tables — names, columns, types, keys. Save it as a snippet. Use it as the first paragraph of every SQL prompt this week.
Write a prompt asking for a window-function query that calculates each customer's 30-day rolling spend, ordered by order_ts. Specify your dialect. Compare what AI produces for BigQuery vs Snowflake vs Postgres — notice the subtle differences in window-frame syntax.
Take an existing slow query and ask AI:
Rewrite this query for better performance on a partitioned, clustered warehouse. Explain each optimisation.
Compare execution plans before and after.
Sign in to join the discussion and post comments.
Sign inPrompt Engineering for Content & Copywriting
Write blogs, ads, emails, and social media content ten times faster with AI. 13 practical tutorials on prompt engineering for content creators and copywriters.
Foundations of Prompt Engineering
The must-know basics of prompt engineering. Learn what prompts are, how AI models read them, and how to write clear instructions that get great results.
Advanced Prompt Engineering Techniques
Master the powerful techniques AI experts use every day. Chain-of-thought, RAG, agents, function calling, prompt evaluation, and much more — 20 deep-dive tutorials.
Prompt Engineering Projects & Real-World Applications
Twelve hands-on projects that turn prompt engineering theory into a portfolio. Build chatbots, content generators, RAG systems, and more.
Prompt Engineering for Education & Learning
Use AI as your personal tutor. Learn how to study faster, create lesson plans, generate practice questions, master languages, and prepare for competitive exams with smart prompts.
Prompt Engineering for Business & Productivity
Use AI to work smarter — automate tasks, make better decisions, and communicate professionally. 12 practical business prompt tutorials for professionals.