Query troubleshooting
SQL Errors
Find what is going wrong in your query and jump straight to the cause and the fix.
33 errors to work through
Browse by symptom
Browse by symptomAll
33 results
The query will not run
7Ambiguous column when joining tablesThe column exists in more than one table and the engine does not know which one you mean. Put the table alias in front of the column, as in c.id.
DISTINCT fails with an ORDER BY columnThe sort depends on a column that does not define each distinct row in the result. Sort by a selected column, or define a rule per group such as MIN(id), without changing what the query means.
The alias does not work in WHEREA SELECT alias is not yet available in WHERE, at least not portably. Repeat the expression in WHERE or compute the alias inside a subquery.
The column does not existThe name does not exist in the source or is not visible at that level of the query. Check the schema, fix the name and review which columns the subquery returns.
The query has a syntax errorThe SQL does not fit the engine's grammar, and the cause may sit before the point reported. Check commas, quotes and clause order; trim the query down and fix one thing at a time.
The CTE does not exist in the next queryA CTE only exists within the statement attached to WITH. Reuse it within that statement, or use a view or a temporary table if several queries need it.
Cannot convert text to dateThe text holds an impossible date, an ambiguous format or a value that is not a date at all. Validate the calendar and format in a staging table before converting and loading it.
Missing rows
4LEFT JOIN loses rows from the left tableA WHERE filter on the right table discards the rows padded with NULL. To keep every row from the left table, move the matching condition into ON.
NOT IN does not return expected rowsThe exclusion list contains NULL, so the comparison becomes unknown. Remove the NULLs that do not stand for an exclusion, or use NOT EXISTS with an explicit relationship.
NULL in WHERE: why = NULL returns no rowsYou are comparing NULL with =, and that condition is never true. Use IS NULL to find missing values and IS NOT NULL to find values that are present.
WHERE with <> also excludes NULLsAn ordinary comparison with NULL evaluates to unknown, and WHERE discards it. If missing values should be included, add OR column IS NULL to the condition.
Extra rows appear
3COUNT with LEFT JOIN returns 1 instead of 0LEFT JOIN keeps a row even when there is no match, and COUNT(*) counts it. Count a non-null column from the right table instead, such as COUNT(p.id).
JOIN repeats rowsA row has several matches on the right, or the ON condition is incomplete. Complete the JOIN key, aggregate to the level you need, or use EXISTS if you only want to test for existence.
Pagination repeats or skips rowsORDER BY leaves ties, or rows change between requests. Add a unique key to the sort; if data changes concurrently, consider a cursor or a consistent snapshot.
GROUP BY and calculations
6COUNT or SUM does not work in WHEREWHERE evaluates rows before the aggregate exists. Use HAVING for the condition on the aggregate and keep the per-row conditions in WHERE.
Division by zeroThe divisor is zero, so the engine may reject the division or return NULL. Use NULLIF(divisor, 0) and decide what that missing result should mean.
GROUP BY leaves a column ungroupedYou selected a column that is not determined by the grouping key. Group by the correct key or remove that column; do not add an aggregate without deciding what it should represent.
Division drops the decimalsOn some engines, dividing two integers gives an integer. Convert one operand to a decimal before dividing, for example 1.0 * 5 / 2.
Aggregates cannot be nestedYou are trying to compute a second summary over an aggregate at the same level. Get the first summary in a subquery or CTE and apply the second aggregate from outside.
ROW_NUMBER does not work in WHEREThe window is computed after that level's WHERE filter. Compute the window in a subquery or CTE and filter on the position from outside.
INSERT, UPDATE, DELETE and schema
8CHECK rejects the valueThe value makes one of the table's CHECK conditions false. Fix the data, or review the business rule before you change the constraint.
DROP COLUMN fails due to dependenciesThe column is still used by indexes, views or constraints. Identify each dependency and change or remove only the ones that need it before dropping the column.
The text does not fit in VARCHARThe value exceeds the defined limit, or the unit the engine measures it in. Fix the data if it is invalid; widen the column with a migration if the value is legitimate.
FOREIGN KEY blocks an insert or deleteThe child reference has no parent, or you are trying to delete a parent that is still referenced. Check the key and the load order; before deleting, look at the child rows and the action defined on the key.
INSERT has a different number of columns and valuesEach VALUES row needs one value for every column listed in the INSERT. Line up both lists, or drop the column that should get its DEFAULT from the target list.
NOT NULL rejects a missing valueA required column receives NULL from an insert, update or transformation. Fix the source of the data; leave the column out only when a valid DEFAULT exists.
UNIQUE rejects a duplicate valueA row already exists with the value or combination that must be unique. Find that row and decide whether to update it, ignore the duplicate or fix the incoming data.
UPDATE or DELETE modifies too many rowsThe statement does not limit which rows should change. Test the same filter with SELECT and rehearse the change in a transaction you can roll back before you COMMIT.
Subqueries and set operations
4The subquery returns too many columnsThe context needs a single value, but the inner SELECT returns several columns. Select only the column you need, or write a row comparison if you really need several.
The subquery returns too many rowsA scalar comparison expects one value, but the subquery returns several. Use IN if you are testing membership in a set; if you need a single value, guarantee uniqueness with a specific key or rule.
UNION has a different number of columnsThe SELECT statements do not return the same number of columns. Decide what each position represents and line up the column lists in both queries.
UNION has incompatible typesThe columns in the same position cannot be converted to a common type in your engine. Decide what that column means and use CAST to a suitable type in each branch.
Locks and concurrency
1No results. Try another word or .