P
💻DeveloppementIntermediateAll AIs

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

Answer 3 questions and Léa tailors the prompt to your situation.

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

Generating SQL Queries

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 Optimizer

Comments

Be the first to comment on this prompt.

📬 Get new prompts every week

Join our newsletter and never miss a prompt.

Go further

Similar Prompts

💻DeveloppementIntermediateAll AIs

Stable Diffusion Prompt for Documenting Code

Stable Diffusion, though primarily known for generating artistic images, can become a powerful tool for creating visuals to accompany code documentation. By generating conceptual diagrams, data flow illustrations, visual representations of software architectures, or thematic icons, Stable Diffusion helps enrich technical documentation that is often dry and hard to grasp. Developers and technical writers can thus produce explanatory schematics, process infographics, or illustrations of abstract concepts like design patterns, data structures, or execution flows. The major advantage lies in production speed: where a graphic designer would take hours to create custom visuals, Stable Diffusion generates illustrations in seconds, allowing rapid iteration until the desired result is achieved. This approach is particularly useful for teams documenting APIs, open-source libraries, or distributed systems, where visual clarity makes all the difference between ignored documentation and documentation developers actually consult.

0111
💻DeveloppementIntermediateAll AIs

Namespace and Quota Management

Manage a multi-tenant cluster

0108
💻DeveloppementBeginnerAll AIs

Artifact Retention Policy

Optimize artifact storage costs

097
💻DeveloppementAdvancedAll AIs

Build a virtualized list

Handle massive data lists

0142