A SQL Good-Practice Checklist Before You Ship a Query
Most production SQL incidents come from a short list of repeatable mistakes. These eight checks catch the ones that actually cause damage.
1. Never build a query by string concatenation
User input pasted into SQL is the classic injection hole. Use parameters — WHERE id = ? with the value passed separately — in every language and driver. A parameterised query sends the value as data, so a quote inside it cannot change the statement.
2. Name your columns
SELECT * breaks the day someone adds a column, drags large TEXT columns across the network, and makes the result ambiguous after a join. List the columns you will actually use.
3. Bound every query that reads a table
Any query against a table that grows must have a WHERE or a LIMIT. An unbounded SELECT on a real table is a slow query waiting for volume.
4. Keep functions off filtered columns
WHERE DATE(created_at) = '2026-01-01' cannot use an index on created_at. Use a range instead: WHERE created_at >= '2026-01-01' AND created_at < '2026-01-02'. Same meaning, and it is index-friendly.
5. Wrap related writes in a transaction
If two statements must both succeed or both fail, they belong in one transaction. Half-applied writes are far harder to diagnose than a rolled-back one.
6. Treat NULL as "unknown", not "empty"
NULL = NULL is not true; it is unknown. Use IS NULL and IS NOT NULL. A NOT IN list containing a NULL returns no rows at all — a silent empty result that looks like "no matches".
7. Prefer explicit JOIN syntax
Join conditions belong in ON, filters in WHERE. Putting a filter in the ON clause of an outer join changes which rows survive — a subtle bug that implicit comma-joins make almost invisible.
8. Read the plan before shipping a slow query
EXPLAIN is not advanced tooling; it is the difference between guessing and knowing. If the plan shows a full scan on a large table, you have found the problem before your users did.