How to fix common sql mistakes

You expected forty rows and got twelve. Or the query ran, the total looked plausible, and a colleague asked why it was half the real figure. Debugging SQL is not about staring harder at the statement. It is a short sequence of checks that tells you where the mistake lives, and the sequence barely changes from one problem to the next.

Read the message instead of rewriting the query

Most beginners change three things at once the moment an error appears, which destroys the evidence. Read the message first: it names a position, a token, or a rule. The three families that cover most failures are worth recognising.

  • Syntax near a word. Something is missing or extra just before the position it reports — a comma, a quote, or a closing parenthesis. Count the parentheses when the message points at the end of the statement.
  • Ambiguous or unknown column. Two joined tables share a column name, or you guessed a name that does not exist. Qualify the column with its table.
  • A column must appear in a grouping clause. You selected something the engine cannot reduce to one value per group. Either group by it or aggregate it.

If the message is genuinely cryptic, cut the query down until the smallest version still fails. That version is the real question.

Count rows at every step

Build the query in pieces and check the row count after each addition. Start with one table and count. Add a filter and count again — does the drop make sense? Add the join and count again. A jump from two hundred to four thousand rows is the answer to your problem, and it usually means a key repeats on one side.

Use COUNT of a specific column when you care about missing values, since counting the star will include rows where that column is empty. Then check the distinct count of the key you joined on: if it differs from the row count, you have duplicates and a multiplication waiting to happen.

When it runs and the numbers are wrong

No error means the statement is legal, not that it is right. Work through the usual suspects, in this order, because the cheap checks come first.

  • An inner join silently dropping unmatched rows that you expected to keep.
  • A comparison against an empty value that returns nothing, because it is null rather than blank or zero.
  • A condition placed before grouping when you meant to filter the groups afterwards.
  • A DISTINCT added to hide duplication instead of finding its source.
  • A date boundary that excludes the first or last day because of a time component.
  • Text compared in a case-sensitive way, so “Paid” and “paid” are two categories.

Write the cause next to the fix

A fix you cannot explain is a fix you will undo. When the query finally behaves, add one comment naming the cause, then save it. Two weeks later, that line is the difference between solving the problem again and recognising it.

Keep the broken version somewhere, at least until the project ends. Comparing before and after is how you learn which change actually mattered.

Debugging gets fast with repetition because the checks are always the same: read, isolate, count, list suspects, record the cause. Speed is not the goal though — understanding is, and the counts are what give it to you.