Home › Prompts › Grok › Write and explain a SQL query from a schema
Write and explain a SQL query from a schema
Generate a correct, efficient SQL query from a schema and a plain-English question, with explanation and index suggestions.
Written for Grok 4.7 · Updated 2026-10-16
Note: Review and test before use. Provided as is, without warranty. See Terms.
Prompt
DownloadDatabase: [Postgres/MySQL/SQLite/BigQuery, version]. Schema:
```
[CREATE TABLE statements]
```
Question: [plain English, e.g. monthly active users by plan for the last 6 months].
Write the query, explain each part briefly, note assumptions about the data, and suggest indexes if it would be slow on [row count] rows.Replace everything in [square brackets] with your own details.
Settings & parameters
grok-4.7, reasoning medium
Variations
- Ask for a version with window functions vs. without.
- Convert between SQL dialects.
Tips
- Run on a sample first; check NULL handling and time zones.