SQL Query Prompt Templates
AI prompt templates for writing SQL queries. Create SELECT, JOIN, aggregate, and complex queries.
Overview
Writing SQL queries from scratch can be tedious, especially when you're dealing with complex joins or unfamiliar database schemas. These prompts help you describe what data you need in plain English and get working SQL code back. They're useful whether you're a beginner learning SQL or an experienced analyst who wants to save time.
Best Practices
Always specify the database type (PostgreSQL, MySQL, SQL Server) since syntax varies
Include table names and column names when you know them
Describe the expected output format, like 'one row per customer' or 'grouped by month'
Mention any performance concerns if you're working with large tables
Ask for comments in the code if you want to understand the logic
Prompt Templates
1. Basic Query Builder
Write a [DATABASE_TYPE] query that retrieves [DATA_DESCRIPTION] from the [TABLE_NAME] table. Filter by [CONDITIONS]. Sort by [SORT_FIELD] in [ASC/DESC] order.
Write a PostgreSQL query that retrieves customer name, email, and total orders from the customers table. Filter by customers who signed up in 2024. Sort by total_orders in DESC order.
SELECT customer_name, email, total_orders FROM customers WHERE signup_date >= '2024-01-01' AND signup_date < '2025-01-01' ORDER BY total_orders DESC;
Simple queries with filtering and sorting on a single table
- •Include sample data if the AI needs context about your column values
- •Specify if you want LIMIT added for testing
2. Multi-Table JOIN Query
Write a [DATABASE_TYPE] query that joins [TABLE_1] and [TABLE_2] on [JOIN_CONDITION]. I need [COLUMNS_NEEDED]. Additional filters: [FILTERS]. The relationship is [ONE-TO-ONE/ONE-TO-MANY/MANY-TO-MANY].
Write a MySQL query that joins orders and customers on customer_id. I need customer name, order date, and order total. Additional filters: orders from the last 30 days. The relationship is one-to-many (one customer, many orders).
SELECT c.customer_name, o.order_date, o.order_total FROM orders o INNER JOIN customers c ON o.customer_id = c.customer_id WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY);
When you need data from multiple related tables
- •Specify if you need LEFT JOIN to include records without matches
- •Mention if there are any NULL values you need to handle
3. Aggregate Query with Grouping
Write a [DATABASE_TYPE] query that calculates [AGGREGATIONS] from [TABLE_NAME], grouped by [GROUP_BY_FIELDS]. Include only groups where [HAVING_CONDITION]. Time period: [DATE_RANGE].
Write a PostgreSQL query that calculates total revenue and order count from sales, grouped by product_category and month. Include only groups where total revenue exceeds $10,000. Time period: 2024.
SELECT product_category, DATE_TRUNC('month', sale_date) as month, SUM(revenue) as total_revenue, COUNT(*) as order_count FROM sales WHERE sale_date >= '2024-01-01' AND sale_date < '2025-01-01' GROUP BY product_category, DATE_TRUNC('month', sale_date) HAVING SUM(revenue) > 10000 ORDER BY month, total_revenue DESC;Building reports that summarize data by categories or time periods
- •Be explicit about how you want dates grouped (day, week, month, quarter)
- •Ask for running totals if you need cumulative calculations
4. Subquery and CTE Builder
Write a [DATABASE_TYPE] query using [SUBQUERY/CTE] to solve this problem: [PROBLEM_DESCRIPTION]. The main tables involved are [TABLES]. I need to first [STEP_1], then [STEP_2].
Write a PostgreSQL query using CTE to find customers whose total spending is above the average. The main tables are customers and orders. I need to first calculate each customer's total spending, then compare it to the overall average.
WITH customer_totals AS (SELECT customer_id, SUM(order_total) as total_spent FROM orders GROUP BY customer_id), avg_spending AS (SELECT AVG(total_spent) as avg_total FROM customer_totals) SELECT c.customer_name, ct.total_spent FROM customers c JOIN customer_totals ct ON c.customer_id = ct.customer_id CROSS JOIN avg_spending a WHERE ct.total_spent > a.avg_total ORDER BY ct.total_spent DESC;
Complex queries that require intermediate calculations or multiple steps
- •CTEs make queries more readable, so prefer them over nested subqueries
- •Ask for the query to be broken into named CTEs if you need to understand each step
Common Mistakes to Avoid
Not specifying the database type, which leads to syntax errors when the query uses features specific to another database
Forgetting to mention table relationships, which results in incorrect JOINs or Cartesian products
Being vague about date ranges like 'recent orders' instead of specifying exact dates or relative periods
Frequently Asked Questions
Writing SQL queries from scratch can be tedious, especially when you're dealing with complex joins or unfamiliar database schemas. These prompts help you describe what data you need in plain English and get working SQL code back. They're useful whether you're a beginner learning SQL or an experienced analyst who wants to save time.
Related Templates
Data Analysis Prompt Templates
AI prompt templates for data analysis. Extract insights, identify patterns, and interpret results.
Data Visualization Prompt Templates
AI prompt templates for data visualization. Create effective charts, dashboards, and visual reports.
Report Generation Prompt Templates
AI prompt templates for generating reports. Create automated reports from data sources.
Have your own prompt to optimize?