A saved query is a small promise: that the next time someone asks the same question, the answer is already written. Those promises break quietly. The database rarely changes overnight; what disappears is the context — which table the row count belonged to, why the date filter starts on the second of the month, which join was deliberately left outer. Caring for finished SQL means preserving that context, not polishing the syntax.
Name a query after the question it answers
A filename such as final_query_v2 or test_join tells a future reader nothing, and version numbers multiply faster than anyone expects. Name the file for the business question instead: monthly_active_customers, unpaid_invoices_over_30_days, orders_missing_shipping_address. If two queries answer similar questions, the names should still be distinguishable at a glance.
- Keep the grain in the name when it matters — customers_per_month is clearer than customer_report.
- Put throwaway explorations in a folder called scratch and treat nothing there as reusable.
- Include the source table or system when several exist, because the same word means different things across them.
Comment the assumptions, not the syntax
Nobody needs a comment explaining what SELECT does. What they need is the reasoning that is invisible in the code: who asked for this, what one row represents, and which filter is arbitrary. A short block at the top of the file is enough, and it takes about a minute to write while the knowledge is still fresh.
- State the grain explicitly: one row per customer, per month, per order line.
- Flag filters that exist for a reason a reader would not guess, such as excluding internal test accounts.
- Note known gaps, like records deleted before a migration or a timestamp stored in another timezone.
- Record the date the query was last checked against real data, and by whom.
Keep a version you can get back to
Editing a working query in place is the fastest way to lose it. Copy the file before a significant change, or keep the scripts in a folder that a version control tool already watches. A comment line with the change and the reason is worth more than a perfect commit message nobody reads.
When a query feeds a report, treat the query and the report as one object. Store them together, or at least cross-reference them, so the person updating the report can find the code that produced the numbers.
Retire what has stopped earning its place
Old queries are not harmless. They get copied, adapted, and trusted long after the table they read from was rebuilt. Once a quarter, check which saved items still run on a schedule or were opened recently, and move the rest into an archive folder with a one-line note explaining why they were set aside.
Deletion is a decision someone should own. If a query belongs to another team, ask before removing it; if it is yours and clearly dead, keeping it in the archive is usually enough of a compromise.
Finished SQL stays useful when it explains itself: a name that states the question, comments that carry the assumptions, a copy old enough to fall back on, and a shelf that gets cleared now and then. The maintenance is small and boring, which is exactly why it works.