sqlstreak

SQL query explainer and formatter

Paste a query to tidy it up, see what every clause does in plain English, and catch common mistakes before you run it.

Runs in your browser: nothing is uploaded

2 things to check

  • Careful WHERE cancels the LEFT JOIN

    o.status = 'delivered' filters on the LEFT JOINed table. Rows without a match have NULL there, so WHERE drops them and the LEFT JOIN acts like a plain JOIN. If you meant to keep them, move the condition into the ON clause.

    Practise it: LEFT JOIN →
  • Tip Function around a column in WHERE

    extract(year from o.ordered_on) wraps o.ordered_on in EXTRACT(), so an index on o.ordered_on can't help: the database has to work the function out for every row. Compare the bare column with a date range instead, e.g. o.ordered_on >= '2026-03-01' AND o.ordered_on < '2026-04-01' for March 2026.

    Practise it: Index-friendly filters →

What it does

A SELECT query that joins 2 tables, summarised per c.city, with a filter, sorted, cut to the first rows.

  1. SELECT

    Returns 3 columns, one per line below.

    • city from c
    • the number of non-NULL o.id values, named orders
    • the total of o.total, named revenue
    Lesson: Count, sum, average
  2. FROM

    Reads rows from the customers table (nicknamed c).

    Lesson: Your first SELECT
  3. LEFT JOIN

    Matches each row so far with rows from the orders table (nicknamed o) where o.customer_id equals c.id, and keeps rows that have no match: their orders columns are NULL.

    Lesson: LEFT JOIN
  4. WHERE

    Keeps only the rows where o.status equals 'delivered' and extract(year from o.ordered_on) equals 2026. Rows that don't pass are dropped before anything is grouped or returned.

    Lesson: AND, OR and IN
  5. GROUP BY

    Puts rows with the same c.city into one group, and each group becomes one result row. Aggregates such as COUNT(*) or SUM(…) are then worked out per group.

    Lesson: GROUP BY
  6. HAVING

    Keeps only the groups where count(o.id) is greater than 2. HAVING filters groups after GROUP BY; WHERE filters single rows before it.

    Lesson: HAVING
  7. ORDER BY

    Sorts the result by revenue from highest to lowest (DESC).

    Lesson: Sort and limit
  8. LIMIT

    Returns at most 5 rows.

    Lesson: Sort and limit

The order the database runs it

  1. FROM and JOINs pick and combine the rows
  2. WHERE filters single rows
  3. GROUP BY makes groups
  4. HAVING filters groups
  5. SELECT works out the columns (and window functions)
  6. ORDER BY sorts
  7. LIMIT/OFFSET keep a slice
Lesson · Joining tables

Practise this in 5 minutes

Reading queries gets easy once you've written a few yourself. Our lesson LEFT JOIN walks you through it step by step on a real database, with instant feedback.

Start the lesson

Get the sqlstreak newsletter

Short SQL tips you can use the same day, plus a heads-up when we ship new free tools and lessons. It lands now and then, not every other morning.

No spam, unsubscribe in one click.

The order SQL runs your query

You write SELECT first, but the database reads it nearly last. Knowing the real order explains most "why doesn't this work?" moments, like why a column name made in SELECT can't be used in WHERE.

  1. FROM and JOIN pick the tables and match their rows.
  2. WHERE keeps only the rows you want.
  3. GROUP BY puts rows into groups.
  4. HAVING keeps only the groups you want.
  5. SELECT works out the columns (aggregates and window functions too).
  6. DISTINCT removes duplicate rows.
  7. ORDER BY sorts, then LIMIT keeps the first few.

Mistakes this checker catches

Each of these runs without complaint in at least one database and quietly gives the wrong answer, or works today and breaks later.

Why format SQL at all?

One clause per line, keywords in capitals and indented column lists make a query easy to scan: you can see at a glance what's filtered, grouped and sorted, and a missing condition stands out. Formatted SQL also makes code reviews and diffs far easier to read.

Frequently asked questions

Is my SQL sent to a server?
No. Formatting and explaining both happen in your browser. Nothing you paste here is uploaded or stored.
Which SQL dialects does it format?
PostgreSQL and SQLite, the two databases sqlstreak teaches. Standard SQL from MySQL, SQL Server or BigQuery usually formats fine too, apart from their special syntax.
How does the explainer work?
It splits your query into its clauses (WITH, SELECT, FROM, JOIN, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT) and describes each one in plain words, then lists the order the database actually works through them. It reads the SQL; it doesn't run it.
Can it find every bug?
No. It catches common mistakes that are visible in the SQL itself. It can't know your tables, so it can't tell whether a column exists or a result is what you meant. Run the query to be sure.

Got a CSV export to dig into? Try Query a CSV with SQL.