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.

Learn more

Check the full skill on Prompt Guide to master this technique from A to Z.

View on Prompt Guide

📬 Get new prompts every week

Join our newsletter and never miss a prompt.

Similar Prompts

💻DeveloppementIntermediateAll AIs

Generate Mocks and Fixtures for Your Automated Tests

A prompt to automatically generate realistic mocks, stubs and data fixtures adapted to your test framework and use cases.

091
💻DeveloppementIntermediateAll AIs

Automatically Generate Unit Tests with AI

Automatically generate an exhaustive unit test suite covering nominal cases, edge cases, and error cases for any source code.

0223
💻DeveloppementIntermediateGemini

Create a Python Automation Script

Create a professional Python automation script with CLI configuration, structured logging, error handling, and tests.

24239
💻DeveloppementAdvancedAll AIs

Analyze and Optimize Algorithmic Complexity

Analyze the Big O complexity of your algorithms and optimize them with appropriate data structures and more efficient algorithms.

40233