SQL Query Analyzer

Paste a SQL query to count joins, subqueries and filters and flag SELECT *, leading LIKE wildcards, NOT IN traps and missing limits. Text-only heuristics.

The interactive tool loads here when JavaScript runs.

About this tool

What is the SQL Query Analyzer?

The SQL Query Analyzer reads the text of a query and points out patterns that often make queries slow or wrong. It counts JOINs, subqueries, aggregate functions and filter conditions, turns those counts into a rough complexity score, and lists issues such as SELECT *, a LIKE pattern that starts with %, a function wrapped around a filtered column, NOT IN over a subquery, and a sorted or joined result with no row limit.

Be clear about what this is: a heuristic review of the SQL text. It never connects to a database, so it does not know your table sizes, your indexes, or the plan the optimizer will choose. A query it calls High complexity can run in a millisecond on small tables, and a short query it calls Low can scan a billion rows. Use it as a checklist before you reach for EXPLAIN, not as a replacement for EXPLAIN.

Comments and the contents of string literals are stripped before analysis, so a JOIN mentioned inside a comment or a quoted value is not counted.

  • Counts JOINs, subqueries, aggregates and conditions
  • Complexity score with Low, Medium and High bands
  • Flags SELECT *, leading wildcards, functions on filtered columns, NOT IN over subqueries and missing limits
  • Ignores keywords inside comments and string literals
  • Text-only: runs in your browser and never touches a database
Read the guide: SQL Indexes and EXPLAIN Plans: Finding Why a Query Is Slow

Updated

How to use the SQL Query Analyzer

  1. Paste a query into SQL Query Input, or click Load Sample to try a query with a LEFT JOIN, an EXISTS subquery and aggregates.
  2. Click Analyze Query.
  3. Read Query Metrics for the complexity badge, the score and the JOIN, subquery, aggregate and condition counts.
  4. Work through Issues Found and Optimization Suggestions, then confirm anything that matters with EXPLAIN on a real database.

Worked examples

Three issues in one short query

Input

SELECT * FROM orders WHERE customer_email LIKE '%@gmail.com' OR LOWER(name) = 'x' ORDER BY id

Output

Complexity Low (score 6). Issues: SELECT *, no row limit on a query that sorts or joins, leading wildcard in LIKE, function applied to a column in the WHERE clause.

The score is low because there are no joins or subqueries, yet every flagged pattern can force a full table scan. Complexity and cost are different things.

The built-in sample

Input

Load Sample (users LEFT JOIN orders, EXISTS subquery, COUNT and SUM, GROUP BY, HAVING, LIMIT 100)

Output

Complexity High (score 55): 1 JOIN, 1 subquery, 3 aggregates, 5 conditions. No issues.

High complexity with no issues is a normal result for a reporting query. The query is long, but it names its columns and limits its output.

Background and insight

Most slow queries come down to a handful of shapes. SELECT * fetches columns nobody uses and stops the database answering from a covering index. A LIKE '%term' pattern cannot use a normal B-tree index because the index is sorted by the start of the string. Wrapping a column in a function, as in WHERE LOWER(email) = ? or WHERE YEAR(created_at) = 2026, hides the column from a plain index on it; the fix is an expression index or rewriting the filter as a range.

NOT IN is a correctness trap as much as a performance one. If the subquery returns even one NULL, x NOT IN (...) evaluates to unknown for every row and the query returns nothing. NOT EXISTS does not have that problem, and most planners turn it into an efficient anti-join.

The score is simple arithmetic: 10 points per JOIN, 15 per subquery, 5 per aggregate and 3 per WHERE, AND or OR, capped at 100. Under 20 is Low, under 50 is Medium, anything else is High. It is a way to compare two versions of the same query, not an absolute measure.

Common use cases

  • Pre-review checklist

    Run a query through the analyzer before opening a pull request so reviewers do not have to point out SELECT * or a leading wildcard.

  • Reading ORM output

    Paste the SQL an ORM logged and see at a glance how many joins and subqueries it really produced.

  • Teaching query hygiene

    Show junior developers why LOWER(column) in a WHERE clause or NOT IN over a subquery can hurt.

  • Comparing rewrites

    Score two versions of a query to check that a rewrite actually removed a subquery or a join.

Tips and limitations

  • The analyzer cannot see indexes or row counts. Confirm any real performance question with EXPLAIN (PostgreSQL, MySQL) or the execution plan in SQL Server Management Studio.
  • A missing LIMIT is only a problem when the caller does not need every row. Reports and exports often do.
  • Rewriting OR across different columns as UNION ALL of two indexed queries can help, but test it; modern planners can combine indexes with bitmap scans.
  • For substring search, PostgreSQL's pg_trgm extension or a full-text index handles LIKE '%term%' far better than a B-tree.
  • The checks are tuned for SELECT statements. INSERT and DDL will get metrics but few meaningful issues.

Sources and further reading

  1. Indexes on Expressions, PostgreSQL Global Development Group. Indexing LOWER(email) and similar expressions
  2. Subquery Expressions, PostgreSQL Global Development Group. How NOT IN and NOT EXISTS treat NULL
  3. Index Types, PostgreSQL Global Development Group. When a B-tree index can serve LIKE patterns
  4. Using EXPLAIN, PostgreSQL Global Development Group. Checking what the planner actually does with a query

Frequently asked questions

Can a SQL analyzer tell me how fast my query will run?

No tool that only reads the SQL text can. Speed depends on table sizes, indexes, statistics and the plan the optimizer picks. This analyzer flags patterns that are often slow; EXPLAIN ANALYZE on the real database tells you what actually happens.

Why is SELECT * bad for performance?

It reads and sends every column, including large text or JSON ones you do not use, and it prevents an index-only (covering) scan because the index cannot contain every column. It also breaks code silently when someone adds a column. Name the columns you need.

Why can't an index help with LIKE '%term'?

A B-tree index is sorted by the beginning of each value, so it can find strings that start with term but not ones that merely contain it. A pattern that starts with % forces a scan. Trigram indexes or full-text search are the usual fixes.

What is wrong with NOT IN and a subquery?

If the subquery returns a NULL, NOT IN compares every row against an unknown value and the whole condition becomes unknown, so the query returns no rows. NOT EXISTS ignores NULLs correctly and is usually planned as an efficient anti-join.

How is the complexity score calculated?

10 points per JOIN, 15 per subquery, 5 per aggregate function and 3 per WHERE, AND or OR, capped at 100. Below 20 is Low and below 50 is Medium. It measures how much is going on in the text, not how much work the database does.

Is there an in-depth guide to the SQL Query Analyzer?

Yes. “SQL Indexes and EXPLAIN Plans: Finding Why a Query Is Slow” covers the background, how it works, common pitfalls and worked examples. Read the full guide.