GitHub Copilot Prompt for Generating SQL Queries
GitHub Copilot is radically transforming how developers write SQL queries on a daily basis. Integrated directly into your code editor, this AI assistant understands your project context — table schemas, entity relationships, naming conventions — to generate precise and optimized SQL queries. Whether you work with PostgreSQL, MySQL, SQL Server, or SQLite, Copilot adapts to the specific syntax of your database engine. The challenge is not just to generate functional SQL, but to obtain performant, readable, and maintainable queries. A well-structured prompt enables Copilot to produce complex joins, correlated subqueries, window functions, and aggregation queries that follow best practices. By providing your table schema, expected output, and performance constraints, you get production-ready queries rather than drafts that need reworking. This guide offers an optimized main prompt as well as variants suited to your skill level, to fully leverage Copilot in generating reliable and performant SQL queries.
Paste in your AI
Paste this prompt in ChatGPT, Claude or Gemini and customize the variables in brackets.
-- Database schema:
-- Table: users (id INT PK, email VARCHAR, created_at TIMESTAMP, plan VARCHAR)
-- Table: orders (id INT PK, user_id INT FK->users.id, amount DECIMAL, status VARCHAR, created_at TIMESTAMP)
-- Table: order_items (id INT PK, order_id INT FK->orders.id, product_id INT, quantity INT, unit_price DECIMAL)
-- Table: products (id INT PK, name VARCHAR, category VARCHAR, price DECIMAL)
-- Engine: PostgreSQL 15
-- Goal: Generate a query that returns monthly revenue by product category for the last 12 months, with the percentage change from the previous month, excluding cancelled orders.
-- Constraints: use CTEs for readability, window functions for variance calculation, and index filtered columns.
-- Output format: month, category, revenue, variance_pct
Personalize this prompt with Léa
Léa rewrites this prompt for your job and your exact goal — 3 quick questions.
Why this prompt works
This prompt works because it provides Copilot with the three essential elements: the complete table schema with types and relationships, the target SQL engine to adapt syntax, and a precise description of the expected result with output columns. By specifying technical constraints (CTEs, window functions), you guide Copilot towards an implementation that follows best practices rather than a naive solution with nested subqueries. The SQL comment format allows natural integration into the editor, where Copilot excels at completing the subsequent code.
Use Cases
Variants
Expected Output
Copilot generates a complete SQL query using CTEs to structure the calculation in clear steps: first monthly revenue by category, then applying LAG() to calculate the variance. The query includes filters on order status and time period, with readable aliases and consistent ORDER BY. You obtain a query ready to execute, readable by the entire team, and easily modifiable.
Frequently Asked Questions
How can I get GitHub Copilot to understand my database schema?
The most effective way is to place your schema definitions (CREATE TABLE) in an SQL file open in an adjacent tab or within the same file. Copilot analyzes open files as context. You can also write the schema in SQL comments right before your query, as in the main prompt. For projects using an ORM (Prisma, Drizzle, SQLAlchemy), Copilot can also infer the schema from your model files if they are open in the editor.
Can Copilot optimize an existing SQL query that is too slow?
Yes, paste your slow query and add a comment like "-- Optimize this query: it takes 12s on a table with 5M rows. Available indexes: idx_orders_created_at, idx_orders_user_id". Copilot can rewrite the query by replacing correlated subqueries with joins, using materialized CTEs, or restructuring WHERE conditions to better leverage indexes. However, for a real performance diagnosis, combine Copilot with EXPLAIN ANALYZE on your SQL engine.
Does GitHub Copilot handle syntax differences between PostgreSQL, MySQL, and SQL Server?
Copilot adapts to the SQL engine if you specify it in the prompt. Without indication, it tends to generate standard SQL or PostgreSQL. To get specific syntax, indicate the engine and its version in a comment (e.g., "-- MySQL 8.0" or "-- SQL Server 2022"). Common differences it handles well include: LIMIT vs TOP, date functions (DATE_TRUNC vs DATEPART), JSONB types (PostgreSQL) vs JSON_VALUE (SQL Server), and recursive CTE syntax which varies by engine.
Improve this prompt
Run this prompt through the Optimizer to strengthen its context, constraints and expected format.
Improve this prompt with the OptimizerComments
- LéaAI
Pour améliorer la précision, précise le type de jointure entre `orders` et `order_items` (INNER JOIN si seules les commandes avec articles t'intéressent) et utilise `date_trunc('month', o.created_at)` pour l'agrégation mensuelle. Si tu souhaites inclure les catégories sans vente, remplace par LEFT JOIN sur `products`.
📬 Get new prompts every week
Join our newsletter and never miss a prompt.
Go further
Similar Prompts
Write an ORM query
Write efficient ORM queries
Stable Diffusion Prompt to Generate JavaScript Code
Stable Diffusion is an AI image generation model, not a proper code generation tool. However, it can be used creatively in the context of JavaScript development. Stable Diffusion excels at creating code-related visuals: stylized screenshots of JavaScript snippets, illustrations of programming concepts, software architecture diagrams, or educational images for tutorials and technical documentation. By combining well-crafted prompts, you can generate visual representations of JavaScript code on screen, infographics explaining algorithms, or illustrations for your technical blog posts. This approach is particularly useful for tech content creators, web development instructors, and marketing teams who need professional visuals around JavaScript programming. In this guide, we offer optimized prompts for achieving high-quality visual renderings featuring JavaScript code in various visual contexts.
Implement property-based testing
Add property-based tests
Implement rate limiting
Protect APIs from abuse