How I Teach Juniors Maintainable SQL with Five Real Exercises

A practical, exercise‑based playbook to teach juniors how to write maintainable SQL that survives production quirks and small‑team realities in India.

Written by: Rohan Deshpande

A developer at a laptop reviewing SQL queries on a screen, with notes and coffee nearby.
Image credit: Photo by Scott Graham on Pexels

If you’ve ever scrolled through a legacy repo and found a query that reads like a tax form, you know why maintainable SQL matters. In small Indian startups I’ve worked with, juniors are often thrust into schema fires: urgent bug, customer complaining, and a senior tucked away in another timezone. Code review catches bugs, but it rarely teaches the craft. Teaching maintainable SQL needs hands‑on practice, clear feedback, and realistic constraints.

Here’s a concise, repeatable exercise set I use in hiring and onboarding. Each exercise can be run in 30–60 minutes with a pair: one junior, one reviewer. The goal is not perfect SQL but predictable, readable, and performant queries that a teammate can own.

Why this approach

Exercise 1 — Read, explain, and sketch Give the junior an unfamiliar query (real but safe, from a sanitized dump). Ask them to:

Exercise 2 — Refactor for readability Provide the same query and ask them to refactor it for clarity:

Exercise 3 — Replace a correlated subquery with a join (and measure) Give a correlated subquery version that’s correct but slow. Ask them to:

Exercise 4 — Indexing with intent Hand them the schema and an EXPLAIN (or set a default slow plan). Ask:

Exercise 5 — Tests and edge cases Provide a tiny dataset with edge cases: NULLs, duplicate keys, soft‑deleted rows, timezone differences. Ask them to:

A simple rubric to give feedback When reviewing, score each run on:

Tools and setup (India‑friendly)

Real constraint and tradeoff This approach consumes mentor time. Pairing juniors for five exercises takes 2–3 hours initially. You won’t fix legacy SQL in one sitting. But the payoff is faster onboarding, fewer emergency rollbacks, and a shared vocabulary for future refactors. Also, sometimes a readable query is 20–30% slower than the most optimized version; in low‑throughput endpoints that tradeoff is acceptable. Reserve micro‑optimization for measured hotspots.

Conclusion Maintainable SQL is teachable, but not through random code reviews alone. Short, focused exercises force juniors to verbalize intent, consider scale, and write queries people can read at 2 a.m. Start with these five exercises, keep the feedback tight, and accept that clarity will often win over cleverness. Your future on‑call self will thank you.