Skip to main content

SQL Query Explainer

Paste any SELECT query and get a plain-English breakdown of each clause — SELECT, FROM, JOIN, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT. Each explanation includes performance tips and common pitfalls.

✓ All major SQL clauses ✓ Performance tips ✓ Runs in browser

Frequently Asked Questions

What is the order of SQL clause execution?▼

SQL executes clauses in this logical order: FROM → JOIN → WHERE → GROUP BY → HAVING → SELECT → DISTINCT → ORDER BY → LIMIT. This is different from the order you write them. Understanding execution order explains why you can't use SELECT aliases in WHERE (WHERE runs before SELECT), but you can use them in ORDER BY (which runs after SELECT).

What is the difference between WHERE and HAVING?▼

WHERE filters individual rows BEFORE aggregation — it cannot use aggregate functions. HAVING filters groups AFTER aggregation — it can reference aggregate functions like SUM(), COUNT(). Rule: if the condition uses an aggregate function, it must be in HAVING. If not, prefer WHERE (better performance, runs earlier).

What is the difference between INNER JOIN, LEFT JOIN, and FULL JOIN?▼

INNER JOIN returns only rows where the ON condition matches in BOTH tables. LEFT JOIN returns all rows from the left table plus matching rows from the right (NULLs for no match). RIGHT JOIN is the mirror of LEFT JOIN. FULL OUTER JOIN returns all rows from both tables, with NULLs where no match exists. CROSS JOIN produces every combination of rows (Cartesian product).

What is a CTE (WITH clause) and when should I use it?▼

A CTE (Common Table Expression) creates a named temporary result set within a query. Use CTEs to: (1) Break complex queries into readable steps. (2) Reference the same subquery multiple times without duplication. (3) Write recursive queries (e.g., tree traversal). CTEs in PostgreSQL 12+ are inlined by default (treated as subqueries), which allows the optimizer to push WHERE filters through them.

How can I improve slow SQL query performance?▼

Key optimizations: (1) Add indexes on columns used in WHERE, JOIN, and ORDER BY. (2) Avoid functions on indexed columns in WHERE (prevents index use). (3) Use EXPLAIN ANALYZE to see the query plan. (4) Replace SELECT * with specific columns. (5) Add covering indexes for frequently queried column sets. (6) Avoid N+1 queries — use JOINs or batch loading instead. (7) For large tables, partition by date or other high-cardinality columns.

What is the difference between UNION and UNION ALL?▼

UNION combines results from two SELECT statements and removes duplicates (expensive — requires sorting/hashing). UNION ALL combines results without removing duplicates (faster — no deduplication). Use UNION ALL unless you specifically need to remove duplicates. Both require the same number of columns with compatible types in each SELECT.

Share this tool: X / Twitter LinkedIn
✓ 60,000+ developers use DevPlaybook ★★★★★ 4.8 / 5 on Gumroad ✓ MIT licensed · Instant download ✓ No login required

Related Products

on Gumroad

You Might Also Need

More Free Tools

Enjoying this tool? Get more like it.

Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.

DevPlaybook Pro 7 days free

Unlock AI-Powered Dev Tools

  • ⚡ AI Code Review, Doc Generator & SQL Builder
  • ⚡ All premium templates & early access
  • ⚡ Member discounts on Gumroad products
Start Free Trial →

$9/mo after trial · Cancel anytime

Want the full toolkit?

Get DevPlaybook Pro

Every template, guide, boilerplate, and automation script in one bundle. 13 premium products — grab them all at once and save big.

$79
$241 separately Save $162 (67% off)
Get DevPlaybook Pro — $79 →

MIT licensed · Instant download · No subscription

See what's included → Browse all deals →