A query returns nothing, returns the wrong rows, or takes too long. These are three different problems with three different methods, and treating them as one is why general questions about queries produce vague answers.
What is the first step for all three?
Run the query directly against the database, outside your application. That separates a query problem from a code problem, and it lets you change it and see results immediately rather than through a layer.
Printing the query your code actually sends is part of this, since it frequently differs from what you think it sends.
Why does it return nothing?
- The condition matches nothing. Frequently a value that is not what you assume.
- A join eliminated everything. An inner join where rows are missing on one side.
- Case or whitespace. Trailing spaces are the classic invisible cause.
- Type mismatch. Comparing text with a number.
- You are querying the wrong place. A different database or environment.
How do you find it?
Remove conditions until rows appear, then add them back one at a time. The condition that empties the result is the one to look at, and this takes about a minute.
It is the same reduction method that works everywhere and it is particularly effective here.
Why does it return the wrong rows?
Usually a join producing duplicates, or a condition combining in an unexpected way. A join to a table with several matching rows multiplies the results, and totals calculated over that are wrong in a way that looks plausible.
Counting before and after each join is how you spot it.
What is the request?
Here is the query, the tables involved and what I expected. Do not rewrite it. Tell me which part of it could produce this result, and what I should run to confirm which condition is eliminating or duplicating rows.
Why is the data usually the problem?
Because queries are precise and data is messy. Trailing whitespace, inconsistent casing, values stored as text, dates in mixed formats and empty strings that are not the same as absent values all produce correct queries returning wrong results.
Looking at the actual values, rather than at the query, resolves a surprising proportion.
What about slow queries?
Ask the database rather than guessing. Every relational database can explain how it executed a query, and a scan where an index was expected is visible immediately in that output.
What does AI do well here?
Explaining query plans and suggesting what to check, which are both knowledge tasks. It cannot see your data, so anything about why a particular condition matches nothing has to come from you looking.
What about the code that builds the query?
A separate suspect worth eliminating early. A query assembled conditionally can produce something you never intended, particularly where clauses are added in loops or where an empty list produces a condition matching nothing.
Logging the final query, with its parameters, is what settles this. It is also worth checking how parameters are being supplied, since anything building a query by joining strings with user input is both a correctness problem and a security one, and generated code produces that pattern more often than it should.
What is the habit?
Look at the data before reading the query again. The query says what it says; the data is where the surprise usually is.