P

Gemini Prompt for Generating SQL Queries

Gemini, Google's artificial intelligence model, excels at generating SQL queries thanks to its deep understanding of data structures and database syntax. Whether you work with MySQL, PostgreSQL, SQL Server, or SQLite, Gemini can transform your natural language descriptions into optimized and functional SQL queries. This capability is especially valuable for developers looking to speed up their workflow, data analysts handling complex datasets, and beginners learning SQL. By providing Gemini with a well-structured prompt, you get not only the desired query but also explanations of the logic used, optimization suggestions, and alternatives depending on your database management system. The prompt engineering approach helps guide Gemini to consider your specific constraints: performance, readability, compatibility with a particular DBMS, or adherence to your organization's naming conventions. Discover how to formulate your prompts to get the most out of Gemini in generating SQL queries.

The prompt

Gemini

You are an SQL database expert with 15 years of experience. I will describe what data I want, and you will generate the corresponding SQL query.

My database context:

  • DBMS used: [MySQL/PostgreSQL/SQL Server/SQLite]
  • Relevant tables: [list of tables with their main columns]
  • Relationships between tables: [foreign keys and joins]

What I want: [natural language description of the desired result]

Constraints:

  • Optimize the query for performance
  • Add SQL comments explaining each section
  • Use readable aliases for tables and columns
  • Propose a version with and without subqueries if relevant

For each generated query, provide:

  1. The complete and formatted SQL query
  2. A line-by-line explanation of the logic
  3. Recommended indexes to optimize execution
  4. Potential pitfalls to avoid (NULL values, duplicates, performance)

Personalize this prompt with Léa

Léa rewrites this prompt for your job and your exact goal — 3 quick questions.

Why it works

This prompt works because it assigns an expert role to Gemini, activating specialized knowledge in SQL and database optimization. The sectioned structure (context, goal, constraints, output format) eliminates ambiguity and allows Gemini to generate precise queries tailored to your technical environment. The requirement for explanations and index recommendations pushes the model to produce a complete result that goes beyond the raw query.

Expected result

Gemini generates a complete, properly formatted and commented SQL query, accompanied by detailed explanations of the logic of each clause. You also receive index recommendations to optimize performance as well as a list of common pitfalls to avoid. If relevant, an alternative version of the query is proposed to compare approaches.

Variants by level

FAQ

Can Gemini generate SQL queries for all types of databases?
Gemini supports major relational database management systems: MySQL, PostgreSQL, SQL Server, SQLite, Oracle, and MariaDB. It knows the syntactic specifics of each, such as PostgreSQL-specific window functions or SQL Server TOP statements. To get a perfectly compatible query, always specify your DBMS in the prompt. Gemini can also handle less common dialects like BigQuery or Snowflake if you specify them.
How can I ensure the SQL query generated by Gemini is secure against SQL injection?
Queries generated by Gemini are static templates, not parameterized code. To use them in production, explicitly ask in your prompt for Gemini to use prepared statements with placeholders ($1, ?, :param depending on the DBMS). Add in your constraints: 'Use bound parameters and never concatenate values directly into the query.' Gemini will then generate secure code with best practices for SQL injection protection.
Can Gemini optimize an existing SQL query that is too slow?
Yes, Gemini is particularly effective for optimizing existing queries. Paste your slow query in the prompt, specifying the DBMS, approximate data volume, and if possible, the output of the EXPLAIN or EXPLAIN ANALYZE command. Gemini will analyze potential bottlenecks (full table scans, inefficient joins, correlated subqueries) and propose an optimized version with recommended indexes. It can also suggest restructurings like replacing subqueries with CTEs or lateral joins.

Related prompts

How to use this prompt

  1. Copy the prompt with the button above.
  2. Paste it into ChatGPT, Claude or your favorite AI assistant.
  3. Replace the bracketed variables with your details, then refine the result.

About Prompt Guide

Prompt Guide is a free library of 4800+ ready-to-use prompts for ChatGPT, Claude and other AIs, with guides to learn prompting and tools to build and optimize your own prompts.

More prompts to explore

Gemini Prompt for Optimizing a Workflow

In a professional environment where every minute counts, workflow optimization has become a strategic lever. Google Gemini, with its contextual analysis and deep understanding of business processes, is a powerful ally for rethinking your workflows.

Gemini Prompt for Optimizing Ads

Gemini, Google's artificial intelligence model, is a major asset for digital marketing professionals looking to optimize their campaigns

Gemini Prompt for Optimizing SEO

Gemini, the artificial intelligence model developed by Google, is a strategic asset for optimizing your natural search ranking. Thanks to its deep understanding of language and semantics, Gemini can analyze your content, identify gaps in your SEO strategy, and generate actionable recommendations to improve your position in search results.

Gemini Prompt for Planning a Product Launch

Planning a product launch is a strategic exercise that requires coordinating dozens of variables: positioning, timing, distribution channels,

Gemini Prompt for Summarizing a Document

Summarizing a long document is one of the tasks where Gemini excels thanks to its massive context window that can handle up to 1 million tokens. Whether you have...

Gemini Prompt for Writing a Blog Post

Gemini, Google's artificial intelligence model, excels at writing long, structured content thanks to its ability to handle extended contexts and produce fluent text in French. To write an effective blog post, it's not enough to just say "write me an article": a well-constructed prompt must specify the topic, tone, expected structure, target audience, and editorial intent. By providing these elements to Gemini, you get a usable first draft that follows SEO best practices, captures attention from the introduction, and guides the reader to the conclusion. This guide offers one main optimized prompt and three variants tailored to your level of expertise, transforming Gemini into a real writing assistant. Whether you're a beginner blogger looking to publish regularly or an experienced content creator aiming for top rankings, these prompts will save you considerable time while maintaining a high level of quality.

Go further

Get new prompts every week

Join our newsletter.