DEV Community

A DELETE with no WHERE deletes every row - and it passes code review

The most dangerous line in most codebases is not exotic. It is DELETE FROM products; - a cleanup script that looks harmless, passes review, and wipes a table the day it runs against the wrong database.

The same goes for SELECT * FROM orders CROSS JOIN customers;, which pairs every row with every row before anyone notices the report has 300 billion rows.

I built a free skill that catches these before production does: SQL Query Smell Detector.

TL;DR

Hand it a JSON file of queries (or a plain .sql file). A stdlib-only Python script runs deterministic static checks - destructive statements, cartesian joins, index killers, unbounded scans, implicit joins, OR across columns, DISTINCT band-aids - and emits a severity-ranked findings report with a concrete fix per finding. A second script validates the report. It never executes SQL: a DELETE in the input is flagged as text, never run.

The one finding that matters most

The check I reach for first is NO_WHERE_DESTRUCTIVE. Here is the rule, straight from the skill's pipeline:

DESTRUCTIVE_RE = re.compile(r"^\s*(UPDATE|DELETE)\b", re.I)
WHERE_RE = re.compile(r"\bWHERE\b", re.I)
# High: destructive statement with no WHERE - would touch every row.
if DESTRUCTIVE_RE.search(sql) and not WHERE_RE.search(sql):
    # flag NO_WHERE_DESTRUCTIVE (High)

The fix it suggests is a habit, not just a patch: preview with SELECT ... WHERE <condition> first, then run the DELETE inside a transaction with the same condition.

On the included fictional sample (8 queries against fictional US e-commerce tables), this rule fired on DELETE FROM products; - one of the two High findings in the run.

How it works

  • You supply the queries: a JSON file with name, sql, plus table, row_count, and context where known.
  • main_pipeline.py runs eight regex-based static checks and writes the ranked report as Markdown and JSON.
  • validate_output.py gates the report - schema, ranked order, consistent counts - and must print Validation passed: 0 errors before anything is delivered.

The other checks cover the everyday smells: SELECT * (wasteful read), YEAR(order_date) = 2026 (a function on an indexed column - rewrite as a date range so the index applies), OR across different columns (the planner often cannot use either index - split into a UNION), comma joins in FROM (join intent hidden in WHERE), and SELECT DISTINCT used to hide duplicates from a bad join.

The proof

Real dry run on the included fictional sample - 8 queries, 7 with findings, 11 findings total:

  • High: a DELETE with no WHERE; a CROSS JOIN of 1.2M orders × customers
  • Medium: two unbounded scans with no LIMIT, two SELECT *, an OR across columns, an implicit comma join, a function on an indexed column
  • Low: a SELECT DISTINCT band-aid
  • Clean: one query - "Recent order for one customer" (narrow WHERE, LIMIT 10)
  • Validator: 0 errors.

No database connection, no credentials, no data leaves your machine.

Limits, honestly

These are static text checks - they catch common shapes, not every slow query. They cannot see query plans, index definitions, or table statistics, so confirm slow patterns with EXPLAIN / EXPLAIN ANALYZE before rewriting a production query. And row counts come from your input file; if they are wrong, the large-scan severity is wrong.

Try it

It's free (MIT licensed) - a lead magnet, and I mean that plainly: I built it to be useful first.

Run it against your own queries before your next release. Which SQL smell has bitten you hardest - a DELETE without WHERE, or a query that was slow for no obvious reason?

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.