SQL ยท Chapter 42 of 42
SQL Best Practices
Small habits keep your SQL fast, safe and easy to maintain.
Name consistently, filter early, index thoughtfully, and use transactions where they matter.
Do
Name columns clearly. Explicit column lists (avoid SELECT *). Use JOINs (not comma-separated FROM). Parameterized queries always. EXPLAIN slow queries.
Don't
Don't use `SELECT *` in production. Don't run UPDATE/DELETE without WHERE. Don't ignore NULL semantics. Don't index every column.
Example 1 (sql)
-- GOOD
SELECT id, name, email FROM users WHERE active = TRUE;
-- BAD
SELECT * FROM users;Output
prefer explicit columnsExplicit columns = stable, faster, safer.
Example 2 (sql)
-- Always try a SELECT before UPDATE
SELECT id FROM users WHERE created_at < '2020-01-01';
-- ...only then:
UPDATE users SET status='archived' WHERE created_at < '2020-01-01';Output
safer workflowPreview before mutating.
Key points
- Prefer explicit column lists.
- Always parameterize user input.
- Preview UPDATE/DELETE with SELECT first.
- Add indexes only where needed.
๐ก Note: Formatting matters โ keywords on new lines, aligned commas, consistent case. Future you will thank present you.
