SQLDebugging

How to Fix a SQL Query with AI

The query worked yesterday. Now it returns ORA-00979, or 1064, or 42601 — and the message points at a line that looks fine. Here is how to get from an error to a working query.

Aug 11, 20268 min

The error message points near the mistake, not at it

Database parsers stop where parsing becomes impossible, which is generally one token after the actual error. A missing comma on line 4 gets reported on line 5. An unclosed parenthesis gets reported at the end of the statement.

So the first move on any syntax error is to look at the line before the one named, and to count parentheses and quotes.

The errors that account for most of the pain

ErrorDatabaseWhat it usually means
ORA-00979OracleA selected column is missing from GROUP BY
ORA-00942OracleTable exists but your user cannot see it, or it needs a schema prefix
ORA-00933OracleA clause from another dialect, often LIMIT
1064MySQLGeneric syntax error — check the token right before the quoted position
42601PostgreSQLSyntax error; frequently a trailing comma or a reserved word used as a column
42703PostgreSQLColumn does not exist — usually a case-sensitivity or quoting problem
156SQL ServerIncorrect syntax near a keyword, often a missing alias or comma

Half of "broken" queries are just written for another database

A query that runs on MySQL and fails on Oracle is not broken; it is in the wrong dialect. The frequent offenders:

  • LIMIT 10 — MySQL and PostgreSQL. Oracle wants FETCH FIRST 10 ROWS ONLY, SQL Server wants TOP 10.
  • Double quotes — an identifier in PostgreSQL and Oracle, a string in MySQL by default. This is why SELECT "name" FROM users behaves differently everywhere.
  • NOW() vs SYSDATE vs GETDATE().
  • String concatenation: || in Oracle and PostgreSQL, CONCAT() in MySQL, + in SQL Server.

A worked example

This fails on Oracle with ORA-00979:

SELECT c.name, c.region, SUM(o.total)
FROM   customers c
JOIN   orders o ON o.customer_id = c.id
GROUP  BY c.name

c.region is selected but not grouped. Two valid fixes, and they answer different questions: add c.region to the GROUP BY (one row per name and region), or wrap it in an aggregate such as MAX(c.region) (one row per name). A fixer that just makes the error go away might pick the wrong one, which is why the corrected query is worth reading rather than pasting straight back into production.

Getting the fix

Paste the query and the error message into the box at the top of this page. The message is what makes this fast: the code identifies both the dialect and the failure class, so the correction is usually one specific change rather than a rewrite.

Then check three things before you ship it: that the fix answers the question you originally asked, that no join type changed, and that the row count is what you expect.

What a fixer cannot do for you

  • Permissions. ORA-00942 on a table you can see in the catalogue is a grant problem, not a syntax problem.
  • Queries that run but return the wrong number. There is no error to work from. That is a reading problem — have the query explained instead.
  • Timeouts. A query that never finishes is a performance problem; see optimising slow queries.

Paste the query and the error

Free, no account needed for the first one. Oracle, SQL Server, PostgreSQL, MySQL and more.

Get Started Free

No credit card required