Ten SQL Habits Worth Building in Your First Year

When I think about what helps beginners most, it is rarely a clever trick. It is small habits, repeated until they feel automatic. Here are ten I wish every learner picked up in their first year of writing SQL.

Writing your first SQL queries illustration

Habits for writing queries

  1. Write the WHERE clause before the UPDATE or DELETE. Type WHERE id = 5 first, then go back and add DELETE FROM pets. It is a small trick that prevents the classic mistake of changing every row.
  2. Run a SELECT with the same WHERE first. Before DELETE FROM pets WHERE age > 10;, run SELECT * FROM pets WHERE age > 10; and check the rows are the ones you mean.
  3. Name columns in SELECT once things matter. SELECT * is great while exploring. In code, list the columns you use so a new column does not surprise your program.
  4. Format queries over several lines. One clause per line makes mistakes easier to spot:
SELECT name, city
FROM friends
WHERE city = 'Lisbon'
ORDER BY name;

Habits for your data

  1. Give every table a primary key. It is the reliable way to point at exactly one row, which makes updates and deletes safe.
  2. Use NOT NULL for things that must exist. A pet without a name is probably a bug. Let the database refuse it.
  3. Pick clear, consistent names. Plural or singular table names, snake_case or not: choose once and stick with it. Future you will be grateful.

Habits for working safely

  1. Keep your statements in files. A schema.sql and a seed.sql mean you can rebuild your practice database whenever an experiment goes wrong.
  2. Use placeholders from day one. Whenever your code puts a value into a query, use ? or %s or $1 as your driver expects, never string gluing. That one habit prevents SQL injection.
  3. Try risky changes inside a transaction. BEGIN;, make the change, look at the result, then COMMIT; or ROLLBACK;. It is an undo button you can use while learning.

Why habits instead of tricks

Tricks are fun, but they are hard to remember when you are tired or rushed. Habits work exactly when you need them, because you no longer have to think. Nearly every serious database mistake I have seen in beginners' projects, from a deleted table to a leaked password, would have been prevented by one of the ten above.

How to build them

WeekFocus on
1Habits 1, 2 and 8: careful changes and saved scripts
2Habits 5, 6 and 7: better tables
3Habits 3 and 4: readable queries
4Habits 9 and 10: safe code and transactions

Pick one habit at a time and practise it until you notice yourself doing it without thinking. Then add the next.

Going further

Our December practice calendar gives small daily exercises that use most of these habits, and the SQL injection hands-on exercise shows why habit nine matters so much.

A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.

Get new pages by email

Related pages on this site