Four habits for writing queries that are easier to read, check and change later.
Most database bugs I fix are not clever. They come from a query nobody could read, or a change that nobody could check. These habits have saved me the most time.
Name the columns you want
Selecting every column is quick to type and slow to debug. When a table gains a column, an unrelated page can suddenly change.
select id, customer_id, total_cents, paid_at
from invoices
where paid_at is null
and due_on < current_date
order by due_on;Store money as whole units
Keep amounts as integers in the smallest unit, such as cents. Rounding errors disappear, and totals add up the way a person would add them.
Check a change on a copy
Before running an update on real data, run the same condition as a select and count the rows. If you expected twelve and see twelve hundred, you have just saved yourself an afternoon.
-- how many rows would this touch?
select count(*) from invoices where status = 'sent' and due_on < '2026-01-01';