Good SQL is correct, readable and safe. A handful of habits separate professional queries from fragile ones.
Core habits
- List columns — avoid
SELECT *in production; name what you need. - Always WHERE on writes — preview UPDATE/DELETE with a SELECT first.
- Parameterise input — never build SQL by string concatenation (SQL injection).
- Format for readability — uppercase keywords, one clause per line, meaningful aliases.
- Use transactions for multi-step changes; index thoughtfully.
- Let the database do the work — filter, join and aggregate in SQL, not in application code.
The security non-negotiable
Key takeaways
- 01Name columns instead of SELECT *; always WHERE on UPDATE/DELETE (preview with SELECT).
- 02Never build SQL by string concatenation — use parameterised queries to stop injection.
- 03Format for readability: uppercase keywords, one clause per line, clear aliases.
- 04Use transactions for multi-step changes and let the database do the filtering/aggregation.
