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.

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 Grading Papers

Grading papers is one of the most time-consuming tasks for teachers, trainers, and professors. Between essays, exams, and homework, hours are spent evaluating, annotating, and writing personalized feedback.

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 Organizing a Project

Organizing a project can quickly become a headache, especially when juggling multiple tasks, deadlines, and stakeholders. Gemini, the intelligence...

Gemini Prompt for Planning a Meeting

Planning an effective meeting requires much more than simply choosing a time slot. You need to define clear objectives, prepare a structured agenda, identify essential participants, and anticipate logistical needs. Gemini, Google's AI, proves to be a formidable assistant for orchestrating every step of this preparation. Thanks to its ability to synthesize complex information and generate structured documents, Gemini helps you transform a simple invitation into a productive meeting where every minute counts. Whether you are organizing a weekly team check-in, a quarterly project review, or a brainstorming workshop, a well-crafted prompt enables you to obtain a complete meeting plan including the agenda, time allocation, preparatory documents, and even key questions to address. No more meetings that drag on without a conclusion: with Gemini, you structure your exchanges in advance to maximize the impact of every professional encounter.

Go further

Get new prompts every week

Join our newsletter.