Prompt Library › ⚙️ Technical › SQL Query Builder
GPT-4o ⚙️ Technical Basic

SQL Query Builder

Describe what data you need and get a clean, optimized SQL query with an explanation of how it works.

👁 15 views ⎘ 0 copies ♥ 0 likes

The Prompt

# SQL Query Builder

You are a database expert helping someone write an efficient SQL query.

## My Database

- Database type: [DB TYPE — e.g., MySQL, PostgreSQL, SQLite, SQL Server]
- Relevant tables and columns: [SCHEMA — e.g., users(id, name, email, created_at), orders(id, user_id, total, status, created_at)]
- Relationships: [RELATIONSHIPS — e.g., orders.user_id references users.id]

## What I Need

- Describe the data I want to retrieve: [DATA NEED — e.g., the top 10 customers by total order value in the last 90 days, with their email and order count]
- Any filters or conditions: [FILTERS — e.g., only active users, only completed orders, exclude test accounts]
- Output format: [OUTPUT — e.g., one row per customer, sorted by total descending]
- Performance concern: [SCALE — e.g., table has 2 million rows, or "small dataset"]

## What I Need

1. The complete SQL query
2. Plain-English explanation of each clause
3. Any indexes I should add for this query to be fast
4. Edge cases the query handles (or doesn't)

📝 Fill in the blanks

Replace these placeholders with your own content:

[DB TYPE — e.g., MySQL, PostgreSQL, SQLite, SQL Server]
[SCHEMA — e.g., users(id, name, email, created_at), orders(id, user_id, total, status, created_at)]
[RELATIONSHIPS — e.g., orders.user_id references users.id]
[DATA NEED — e.g., the top 10 customers by total order value in the last 90 days, with their email and order count]
[FILTERS — e.g., only active users, only completed orders, exclude test accounts]
[OUTPUT — e.g., one row per customer, sorted by total descending]
[SCALE — e.g., table has 2 million rows, or "small dataset"]

How to use this prompt

1
Hit "Run it in your AI"

We open ChatGPT, Claude, Gemini, Perplexity, Grok or Copilot with this prompt in the box, ready for you to press Enter — and copied to your clipboard as a backup. Pick your AI once and we remember it.

2
Replace the placeholders

Swap out anything in [BRACKETS] with your specific details.

3
Send it

This prompt is tuned for GPT-4o, but it works well in any of them. Prefer to do it by hand? Copy to clipboard is always there too.

Get prompts like this every Monday Free weekly digest — hand-picked prompts, new tools, AI news.