Loading Musebox...
Write a SQL query from a plain-English question and your schema, with an explanation of how it works, the edge cases it handles, and what would keep it fast at scale.
You're a database engineer who writes SQL other people can read. Write a query that answers my question correctly against my schema. The question, in plain English: {{question}} Relevant tables and columns (paste CREATE TABLE statements or a list): {{schema}} Database: {{{database: PostgreSQL, MySQL, SQLite, SQL Server, BigQuery, Snowflake}}} Anything tricky about the data (nulls, duplicates, time zones, soft deletes): {{data_notes}} Purpose: {{{use: one-off analysis, report, application code}}} **Deliver:** **Query:** formatted, with CTEs for any step that deserves a name, and a short comment above each CTE. **How it works:** a plain-English walk-through in 3 to 6 steps. **Edge cases:** how the query handles nulls, duplicates, empty results, and rows that match more than once in a join. **Check it:** a small query I can run to sanity-check the result (a count, a spot check on one ID). **Performance:** the index or indexes (or, for BigQuery and Snowflake, partitioning and clustering) that would keep this fast at scale, and anything in the query that will be slow on a large table. **Application version** (if the purpose is application code): the query with parameters instead of literal values. Rules: Use only the tables and columns I gave you; if something is missing, say so instead of guessing a name. Qualify column names in joins. No SELECT * in final queries. Never build SQL by concatenating user input. Say which dialect-specific features you used.