20 of the best prompts for Gemini for SQL, step by step across 4 stages. Works with ChatGPT, Claude, and Gemini.
20 of the best prompts for Gemini for SQL, step by step across 4 stages. Works with ChatGPT, Claude, and Gemini.
Published July 4, 2026
Most people try to use AI for Gemini for SQL with a single vague prompt and get generic results. This guide takes a different approach: 4 targeted stages, from Write SQL queries through BigQuery and data warehouse, each with a prompt that gives the AI exactly the context it needs. Write, debug, and optimize SQL queries faster using Gemini for database work across PostgreSQL, MySQL, and BigQuery. Works with ChatGPT, Claude, and Gemini.
These prompts help you write efficient SQL queries and work with relational data.
Write a SELECT query
Write a SQL query to retrieve [DESCRIBE WHAT YOU WANT] from [DESCRIBE TABLES]. Use [POSTGRESQL / MYSQL / BIGQUERY]. Include appropriate JOINs, WHERE filters, and ORDER BY.
Write an aggregation
Write a SQL aggregation query to [DESCRIBE GOAL] from [TABLE]. Include GROUP BY and HAVING where needed.
Write a CTE
Write a SQL query using CTEs to [DESCRIBE THE GOAL]. Break it into named steps and explain what each CTE does.
Write INSERT/UPDATE/DELETE
Write a SQL [INSERT / UPDATE / DELETE] to [DESCRIBE OPERATION] on [TABLE]. Include safety checks: appropriate WHERE clause and any transactions needed.
Write a window function
Write a SQL query with a window function to [DESCRIBE GOAL: RUNNING TOTAL, RANK, LAG/LEAD]. Explain the OVER clause including PARTITION BY and ORDER BY.
Start here to diagnose slow or incorrect queries before you attempt any fixes.
Fix a failing query
This SQL query fails or returns wrong results: [PASTE QUERY]. Expected: [DESCRIBE]. Error or actual result: [DESCRIBE]. What is wrong and how do I fix it?
Fix a JOIN
My JOIN returns [TOO MANY / TOO FEW] rows: [PASTE QUERY]. Explain the JOIN types and which I should use here.
Fix NULL handling
My query does not handle NULLs correctly: [PASTE QUERY]. Fix using IS NULL, COALESCE, or NULLIF as appropriate.
Debug a slow query
This query is too slow: [PASTE QUERY]. What indexes should I add and how should I restructure it? I use [DATABASE].
Fix a date query
My date/time query is wrong: [PASTE QUERY]. I use [DATABASE]. Fix and explain the correct date functions.
These prompts help you improve query performance through better indexing and structure.
Optimize a query
Optimize this slow query: [PASTE QUERY]. Suggest indexes, restructuring, and pattern changes. Explain the impact of each.
Design indexes
Design indexes for these queries: [PASTE QUERIES]. For each: which columns, single or composite, and what it speeds up.
Fix N+1 queries
I have an N+1 problem where a query runs in a loop: [PASTE CODE]. Rewrite to fetch all data in one query.
Read EXPLAIN
Help me read this EXPLAIN output: [PASTE RESULT]. What is the optimizer doing, where is it slow, and what should I change?
Fix slow pagination
My OFFSET pagination gets slow on large pages: [PASTE QUERY]. Rewrite using cursor-based pagination.
These prompts help you write efficient queries and manage data warehouse workloads.
Write BigQuery SQL
Write a BigQuery SQL query to [DESCRIBE GOAL] from dataset [DESCRIBE]. Use BigQuery syntax and functions. Optimize for cost by minimizing bytes scanned.
Optimize BigQuery costs
This BigQuery query scans too much data: [PASTE QUERY]. How can I reduce bytes processed? Suggest: partition filters, column selection, and query restructuring.
Write a schema
Design a database schema for [DESCRIBE APPLICATION]. Give tables, columns with types, primary and foreign keys, and constraints.
Write a migration
Write a migration to [DESCRIBE CHANGE]. Include the UP migration and DOWN rollback. Make it safe for production data.
Convert between databases
Convert this [MYSQL / POSTGRESQL / BIGQUERY] query to [TARGET DATABASE]: [PASTE QUERY]. Explain any syntax differences.
Gemini integrates natively with Google Cloud and BigQuery, making it especially useful for analytics SQL in Google's ecosystem. It can also help with standard SQL across PostgreSQL, MySQL, and SQLite when you paste queries directly.
Yes. Gemini integrates directly with BigQuery in Google Cloud Console and can generate, optimize, and explain BigQuery SQL with awareness of BigQuery-specific features like ARRAY, STRUCT, and partitioned table optimization.
Paste your CREATE TABLE statements or describe table names, column names, and types. The more schema context you provide, the more accurate the generated queries. For complex queries, share the full schema for relevant tables.
Yes. Specify which database you use (PostgreSQL, MySQL, BigQuery, SQLite, SQL Server) and Gemini will use the correct syntax, functions, and features for that database.
Missing WHERE clauses on UPDATE and DELETE statements (which modify all rows), incorrect JOIN types (getting too many or too few rows), and missing indexes on columns used in WHERE clauses. These account for the most common SQL problems.
AI Prompts for Cursor for SQL
Write, debug, and optimize SQL queries faster using Cursor to work with databases more effectively..
See promptsAI Prompts for ChatGPT for SQL
Write, debug, and optimize SQL queries faster using ChatGPT for database work across PostgreSQL, MySQL, and SQLite..
See promptsAI Prompts for Gemini for Analysis
Use Gemini to analyze information, evaluate options, and surface insights that drive better decisions..
See prompts