A skill for generating optimized SQL queries with Copilot from natural language, including joins, aggregations, and window functions.
## Copilot SQL Query Skill
### Overview
Use Copilot to generate SQL queries from natural language descriptions, optimize existing queries, and build complex reports.
### Method 1: Comment-to-SQL
Write a comment describing the query and let Copilot generate SQL:
```sql
-- Get the top 10 customers by total order value in the last 30 days
-- Include customer name, email, order count, and total spent
SELECT
c.name,
c.email,
COUNT(o.id) AS order_count,
SUM(o.total_amount) AS total_spent
FROM customers c
JOIN orders o ON c.id = o.customer_id
WHERE o.created_at >= NOW() - INTERVAL '30 days'
GROUP BY c.id, c.name, c.email
ORDER BY total_spent DESC
LIMIT 10;
```
### Method 2: Schema-Aware Generation
Provide schema context for accurate queries:
```sql
-- Tables:
-- users (id, name, email, created_at, is_active)
-- orders (id, user_id, status, total, created_at)
-- order_items (id, order_id, product_id, quantity, price)
-- products (id, name, category_id, price)
-- categories (id, name, parent_id)
-- Query: Monthly revenue breakdown by product category
-- for the last 12 months, with month-over-month growth percentage
```
### Method 3: Query Optimization
Paste a slow query and ask Copilot to optimize:
```
Optimize this query. It takes 15 seconds on a table with 10M rows:
SELECT * FROM orders
WHERE customer_id IN (SELECT id FROM customers WHERE region = 'US')
AND created_at > '2024-01-01'
ORDER BY created_at DESC;
Suggest: index recommendations, query rewrites, and explain
why the optimized version is faster.
```
### Common Query Patterns
**Window Functions:**
```sql
-- Rank customers by spending within each region
SELECT name, region, total_spent,
RANK() OVER (PARTITION BY region ORDER BY total_spent DESC) as rank
FROM customer_summary;
```
**CTEs for Readability:**
```sql
-- Break complex query into readable steps
WITH monthly_revenue AS (
SELECT DATE_TRUNC('month', created_at) AS month,
SUM(total) AS revenue
FROM orders
GROUP BY 1
),
revenue_with_growth AS (
SELECT month, revenue,
LAG(revenue) OVER (ORDER BY month) AS prev_revenue,
ROUND((revenue - LAG(revenue) OVER (ORDER BY month)) /
LAG(revenue) OVER (ORDER BY month) * 100, 2) AS growth_pct
FROM monthly_revenue
)
SELECT * FROM revenue_with_growth ORDER BY month;
```
**Cursor-Based Pagination:**
```sql
-- Efficient pagination without OFFSET
SELECT id, name, created_at
FROM products
WHERE created_at < $1 -- cursor: last item's created_at
ORDER BY created_at DESC
LIMIT 20;
```
### Tips
- Always provide table schemas for accurate query generation
- Ask Copilot to add EXPLAIN ANALYZE before running on production
- Use CTEs to break complex queries into readable steps
- Request index recommendations alongside query generation
- Specify the database engine (PostgreSQL, MySQL, SQL Server) for correct syntaxFree to copy and use. Compatible with GitHub Copilot, GPT-5.
Write SQL comments describing the query you need, and Copilot generates the SQL. Include table schema comments at the top of the file for best results.
Initial release
Sign in and download this prompt to leave a review.