How to Share a SQL Query for Review (Formatting, Context and Safety)

How to Share a SQL Query for Review (Formatting, Context and Safety)

A 60-line query pasted into chat as one long line is nearly impossible to review. The same query, formatted, with the table structure and an EXPLAIN plan, can often be fixed in minutes. Here's how to share SQL so the reviewer can actually help, without exposing real data.

Step 1: format it

Before:

select o.id,c.name,sum(i.qty*i.price) total from orders o join customers c on c.id=o.customer_id join order_items i on i.order_id=o.id where o.created_at>='2026-10-01' and o.status!='cancelled' group by o.id,c.name having sum(i.qty*i.price)>100 order by total desc

After:

SELECT
    o.id,
    c.name,
    SUM(i.qty * i.price) AS total
FROM orders AS o
JOIN customers   AS c ON c.id = o.customer_id
JOIN order_items AS i ON i.order_id = o.id
WHERE o.created_at >= '2026-10-01'
  AND o.status <> 'cancelled'
GROUP BY o.id, c.name
HAVING SUM(i.qty * i.price) > 100
ORDER BY total DESC;

Conventions that help reviewers:

Step 2: add the context

IncludeWhy
Database and versionPostgreSQL, MySQL, SQL Server and SQLite differ in syntax and optimiser behaviour
Relevant table definitionsColumn types and constraints explain many bugs
IndexesThe first thing to check for slow queries
Approximate row countsA query fine on 1,000 rows may crawl on 10 million
EXPLAIN outputShows how the database actually runs the query
Expected vs actual result"Returns 0 rows, should return about 40"

Get the plan with EXPLAIN (MySQL), EXPLAIN ANALYZE (PostgreSQL, which also runs the query) or "Display Estimated Execution Plan" (SQL Server). Plans are long, which is another reason to share them as a link rather than in chat.

Step 3: remove real data

Step 4: share it as a link

  1. Open the code sharing tool.
  2. Paste the query, choose SQL, and add a title like "Monthly totals: slow on orders > 5M rows".
  3. Optionally create a second snippet for the EXPLAIN output and table definitions.
  4. Send the links with a one-sentence question.

Line numbers let reviewers comment precisely: "the join on line 7 needs an index on order_items.order_id".

A message template

Question: this report query takes ~40s on production (PostgreSQL 16).
Query:        https://sshot.online/c/Qy7Lm2a
EXPLAIN plan: https://sshot.online/c/Pn4Kx8d
Tables: orders (~5M rows), order_items (~18M), customers (~300k)
Indexes: orders(created_at), order_items(order_id)
Expected: under 2s for the dashboard

Common things reviewers spot

Paste your query, choose SQL, and share a link with line numbers and a raw view. Free, no account.

Share a SQL query

Frequently asked questions

How should I format a SQL query before sharing?

Put each main clause (SELECT, FROM, JOIN, WHERE, GROUP BY, ORDER BY) on its own line, indent conditions, use uppercase keywords consistently, and give tables short, meaningful aliases.

What context helps someone review a slow query?

The table definitions or relevant columns, the indexes, approximate row counts, and the output of EXPLAIN (or EXPLAIN ANALYZE) for your database.

Is it safe to share query results?

Only after removing personal and confidential data. Share a few anonymised sample rows, never real customer records.

Why not paste SQL into chat?

Long queries lose indentation and get hard to discuss in chat. A snippet link keeps formatting and adds line numbers so reviewers can point to exact lines.

Related articles

← All guides