SQL Query Builder and Optimizer

SQL query builder with optimization tips, performance analysis, and edge case handling.

datasqldatabaseanalytics
Edit View
Prompt
Help me write and optimize SQL queries.

Database type: {{database}}
Tables involved: {{schema}}
What I need: {{query_goal}}

For each query, provide:

1. **The query**: Clean, formatted SQL with clear aliases
2. **Explanation**: Line-by-line breakdown of what each part does
3. **Performance notes**:
   - Estimated complexity
   - Indexes that would help (CREATE INDEX statements)
   - Potential issues with large datasets

4. **Alternative approaches**:
   - Subquery vs JOIN vs CTE comparison
   - Which is fastest and why

5. **Edge cases**:
   - What happens with NULL values
   - What happens with empty results
   - Duplicate handling

6. **Testing**: Sample data to test the query with expected results

Common request types I can help with:
- Complex JOINs across multiple tables
- Window functions (ROW_NUMBER, RANK, LAG, LEAD)
- CTEs and recursive queries
- Aggregation with GROUP BY and HAVING
- Date/time manipulation
- Pivoting/unpivoting data
- Query performance optimization
- Migration scripts

Save this prompt to your library

Organize, version, and access your best prompts across ChatGPT, Claude, and Cursor.