Last updated:
SQL Visualizer
This SQL visualizer shows which tables, columns and joins a SELECT query uses: paste your database schema and the query, and the parts it references are highlighted in blue on a diagram, while the tables it doesn't touch are grayed out.
What this tool does
Reading a multi-table SELECT and holding the implied data flow in your head is a skill that breaks down somewhere around the fifth join. The Query Visualizer takes the schema you paste in and the query you are trying to understand, parses both, and projects the result onto an interactive entity-relationship diagram. Tables touched by the query light up; columns in the SELECT list and the join conditions get individual highlights; everything else fades into the background so the active surface area of the query becomes obvious at a glance.
The whole pipeline runs in your browser. No driver is loaded, no schema is uploaded, no query is executed. That matters because the most useful queries to visualize are usually the most sensitive ones: production reporting queries, half-finished migrations, customer-data extracts. You can paste a schema with personally identifiable column names or a query that joins your billing tables; neither leaves the browser, and we record only counts, such as how many tables and joins the query has, never the SQL. It reads the SELECT queries of everyday reporting, CTEs (WITH and WITH RECURSIVE), subqueries and UNIONs included, with no setup or signup.
When to use the Query Visualizer
Reviewing a teammate's pull request that adds a new reporting query. The diff shows the SQL but not which tables are now coupled to which. Pasting the query into the visualizer shows every table it touches and how they're joined.
Diagnosing a query that returns the wrong number of rows. When a count comes back too high or too low, the cause is almost always a join condition that creates duplicates or a WHERE filter that silently converts an outer join into an inner one. Seeing the join graph and the conditions side-by-side makes the failure mode jump out.
Onboarding to a database you have never seen before. The fastest way to learn a new schema is to read its real queries. Visualizing a handful of representative reporting queries shows which tables are central, which are leaf nodes, and which fact tables fan out to which dimensions.
Pruning queries before they hit a slow path. A query that joins eight tables but only uses columns from three is a refactor candidate. The visualizer shows which tables are joined and which columns the SELECT list uses, so a join that contributes no selected columns stands out as a candidate for an EXISTS subquery.
Walkthrough with a real example
Paste the following schema into the left pane:
CREATE TABLE customers (
id BIGSERIAL PRIMARY KEY,
email TEXT NOT NULL UNIQUE,
signup_date DATE NOT NULL
);
CREATE TABLE orders (
id BIGSERIAL PRIMARY KEY,
customer_id BIGINT NOT NULL REFERENCES customers(id),
status TEXT NOT NULL,
placed_at TIMESTAMPTZ NOT NULL,
total_cents INTEGER NOT NULL
);
CREATE TABLE order_items (
id BIGSERIAL PRIMARY KEY,
order_id BIGINT NOT NULL REFERENCES orders(id),
product_id BIGINT NOT NULL,
quantity INTEGER NOT NULL
);
CREATE TABLE refunds (
id BIGSERIAL PRIMARY KEY,
order_id BIGINT NOT NULL REFERENCES orders(id),
reason TEXT,
amount_cents INTEGER NOT NULL
); Then paste this query, which calculates lifetime net revenue per high-value customer:
SELECT
c.id,
c.email,
COUNT(DISTINCT o.id) AS lifetime_orders,
SUM(o.total_cents) - COALESCE(SUM(r.amount_cents), 0) AS net_cents
FROM customers c
LEFT JOIN orders o
ON o.customer_id = c.id
AND o.status = 'completed'
LEFT JOIN refunds r ON r.order_id = o.id
WHERE c.signup_date >= '2025-01-01'
GROUP BY c.id, c.email
HAVING SUM(o.total_cents) > 50000
ORDER BY net_cents DESC
LIMIT 100;
The diagram immediately lights up customers, orders, and refunds as the active set, leaving order_items grayed out — visually confirming that this query does not need the line items table even though it is part of the same business entity. Join edges run from orders to customers and from refunds to orders, each labeled with the join type and the joined columns (LEFT JOIN, customer_id = id). Inside each node, the columns from the SELECT list and the join conditions are highlighted: id and email on customers, customer_id and id on orders, order_id on refunds. Columns used only inside functions or in the WHERE, GROUP BY, and HAVING clauses, such as signup_date and total_cents, aren't highlighted yet. The first join's condition runs over two lines and is read as a whole: the edge shows its first column pair, and the analysis panel the full condition.
The analysis panel breaks the query into its parts: three tables (with their aliases), four selected expressions (two columns, two aggregates), the two joins with their full ON clauses, and the WHERE predicate on signup_date. It doesn't list the GROUP BY, HAVING, or LIMIT clauses yet. There are no notes: the WHERE filters customers, the side the LEFT JOINs preserve, and the status filter sits in the ON clause, so customers without orders still come through. Move o.status = 'completed' into the WHERE clause and the tool flags "LEFT JOIN with WHERE conditions on the joined table may behave like INNER JOIN".
Four worked examples
Each of these runs on the tool's sample schema, a small blog with users, departments, posts, categories and comments. Each link loads the schema and the query into the SQL visualizer above and runs it.
A JOIN across three tables
SELECT u.name, d.name AS department, p.title
FROM users u
INNER JOIN departments d ON d.id = u.department_id
INNER JOIN posts p ON p.user_id = u.id
WHERE p.published = true users, departments and posts light up, and categories and comments fade out. Two INNER JOIN edges run from users, one to departments (labeled id = department_id) and one to posts (user_id = id). The analysis panel lists the three tables with their aliases, the three selected columns, both joins and the one WHERE condition, with no notes. Open the JOIN example in the visualizer.
GROUP BY with a LEFT JOIN
SELECT c.name AS category, COUNT(p.id) AS post_count
FROM categories c
LEFT JOIN posts p ON p.category_id = c.id
GROUP BY c.name
ORDER BY post_count DESC
This counts posts per category, keeping categories that have none: the LEFT JOIN preserves every category, and COUNT(p.id) counts 0 for the padded rows. The diagram shows categories and posts joined by a LEFT JOIN edge labeled category_id = id. The analysis panel lists COUNT(p.id) as a selected expression next to c.name, but not the GROUP BY or ORDER BY clauses yet. Its note, "No WHERE clause on a multi-table query", is expected here: the GROUP BY collapses the rows to one per category. Open the GROUP BY example in the visualizer.
A NOT IN subquery, rewritten as a join
"Users who have never commented" is often written with a subquery:
SELECT u.id, u.email
FROM users u
WHERE u.id NOT IN (SELECT user_id FROM comments);
The visualizer reads that subquery and highlights comments as a table it reads, but no join edge links the two tables, because there's no join. The same question in its JOIN form, an anti-join, shows the link:
SELECT u.id, u.email
FROM users u
LEFT JOIN comments cm ON cm.user_id = u.id
WHERE cm.id IS NULL
The LEFT JOIN keeps every user, and cm.id IS NULL keeps only those with no matching comment. The diagram shows users and comments joined by a LEFT JOIN edge labeled user_id = id, and the panel notes "LEFT JOIN with WHERE conditions on the joined table may behave like INNER JOIN". For an IS NULL test that's the intent, not a bug: the condition only matches the rows the LEFT JOIN padded. The JOIN form also avoids a trap in NOT IN, which returns no rows at all when the subquery returns a NULL. Open the anti-join example in the visualizer.
A CTE joined to two tables
WITH active_authors AS (
SELECT user_id, COUNT(*) AS posts
FROM posts
WHERE published = true
GROUP BY user_id
)
SELECT u.name, d.name AS department, a.posts
FROM active_authors a
INNER JOIN users u ON u.id = a.user_id
LEFT JOIN departments d ON d.id = u.department_id
ORDER BY a.posts DESC
The CTE active_authors counts each author's published posts, and the main query joins it to users and departments. The diagram adds a node for the CTE, marked CTE and listing its two columns, user_id and posts, with a dashed arrow from posts, the table it reads. An INNER JOIN edge links the CTE and users (labeled id = user_id), and a LEFT JOIN edge links users and departments (id = department_id). Above the diagram the tool reports 3 tables and "WITH: 1 CTE (active_authors)", and the analysis panel lists posts as read in active_authors. The note "No WHERE clause on a multi-table query" applies to the main query: the CTE already filtered the posts, so it's expected here. A WITH RECURSIVE query is drawn the same way, with the CTE marked Recursive CTE. Open the CTE example in the visualizer.
SQL concepts you should know
Join types and what they preserve. An INNER JOIN returns only rows where the ON predicate is satisfied on both sides. A LEFT JOIN preserves every row from the left input and pads the right side with NULLs when no match exists; RIGHT JOIN does the mirror. FULL OUTER JOIN preserves unmatched rows from both sides. CROSS JOIN produces every pair, with no condition — almost always a mistake outside of generating ranges or pivoting. The visualizer labels each edge with its type so the preservation semantics are explicit.
The ON clause vs the WHERE clause. Predicates in the ON clause are part of the join itself and run before the rows are combined. Predicates in the WHERE clause run after. For an INNER JOIN this distinction is invisible — the optimizer can move predicates between the two. For an OUTER JOIN it is decisive: a WHERE predicate that references the nullable side filters out the NULL-padded rows the OUTER JOIN was supposed to preserve, collapsing it into an INNER JOIN. This is the single most common cause of "my LEFT JOIN is acting like an INNER JOIN" bug reports.
Subqueries vs CTEs vs derived tables. A subquery in the SELECT or WHERE returns a value or a set of values per outer row. A derived table is a subquery in the FROM clause that acts as an inline relation. A CTE (the WITH clause) is the same idea, but named and reusable within the query. Modern optimizers usually inline non-recursive CTEs into the surrounding query, so the choice between a CTE and a derived table is mostly about readability — except in PostgreSQL versions before 12, where CTEs were optimization fences.
The logical execution order of a query. SQL reads top-to-bottom but executes in a different order: FROM and JOIN first, then WHERE, then GROUP BY, then HAVING, then SELECT, then DISTINCT, then ORDER BY, then LIMIT. That is why a column alias defined in the SELECT cannot be referenced in the WHERE — the WHERE runs first. It is also why HAVING can filter on aggregates but WHERE cannot. Internalizing this order is the single biggest unlock for writing correct queries on the first try.
Common mistakes
Filtering the nullable side of a LEFT JOIN in the WHERE clause. The classic outer-join collapse:
-- Looks like a LEFT JOIN, behaves like an INNER JOIN
SELECT u.id, p.title
FROM users u
LEFT JOIN posts p ON p.user_id = u.id
WHERE p.published_at > '2025-01-01';
Customers with no posts disappear because p.published_at is NULL for them, and NULL fails the comparison. Move the predicate into the ON clause and the LEFT JOIN behaves as intended:
-- Predicate moved into the ON clause: LEFT JOIN preserved
SELECT u.id, p.title
FROM users u
LEFT JOIN posts p
ON p.user_id = u.id
AND p.published_at > '2025-01-01'; Forgetting the ON clause and getting a Cartesian product. Writing FROM users, orders without a join condition multiplies row counts together. On a small dev database it returns slowly; on production it can exhaust memory.
Aggregating before joining and doubling counts. Joining to a table that has multiple matching rows per parent row, then doing a SUM or COUNT, multiplies the parent's other columns. Aggregate first in a subquery or CTE, then join the aggregate to the parent.
Using SELECT * in production code. It hides which columns are actually used, breaks downstream consumers when the schema changes, and makes the visualizer's column-highlighting useless because every column is "referenced." Name the columns explicitly.
Assuming join order matches execution order. The optimizer reorders joins based on statistics, not on the order you wrote them. Putting a small filtering table first in the FROM clause is a hint, not a guarantee — and on modern planners, often not even a hint.
FAQ
How do I visualize a SQL query?
Paste your CREATE TABLE statements in the Schema box, paste a SELECT query in the SQL Query box, and choose Visualize. The diagram highlights the tables and columns the query uses and draws its joins between them. To try it without your own SQL, open one of the worked examples, which load into the tool from a link.
What does the SQL visualizer show?
A diagram of the schema with the tables the query uses highlighted and the rest grayed out, the columns from its SELECT list and join conditions highlighted inside each table, and an edge for each join labeled with its type and columns. Next to it, an analysis panel lists the tables and their aliases, the selected expressions, each join with its ON clause, the WHERE conditions, and notes on patterns worth a second look, such as a multi-table query with no WHERE clause.
Does the visualizer execute my query against a real database?
No. The tool reads your CREATE TABLE statements and the SELECT query as text, in your browser. No driver is loaded, no database connection is opened, and neither the schema nor the query is sent anywhere. We record only anonymous counts about each run, such as how many tables and joins the query has, never the SQL or its names.
Which SQL dialects are supported for the query input?
SELECT queries in PostgreSQL, MySQL, SQLite, or SQL Server syntax, including ones that start with WITH or WITH RECURSIVE: quoted, backticked, bracketed and schema-qualified names, INNER, LEFT, RIGHT, FULL, and CROSS joins, JOIN … USING, NATURAL joins, and ON conditions over several lines. Window functions are listed as selected expressions, but the columns inside them aren't highlighted. INSERT, UPDATE, DELETE, and other statements aren't drawn: the tool names the statement it found, such as an INSERT … SELECT or an UPDATE … JOIN, so you can paste the SELECT part instead.
Can it visualize Common Table Expressions (CTEs) and subqueries?
Yes. A query that starts with WITH or WITH RECURSIVE gets a node for each CTE, listing its columns, with a dashed arrow from each table the CTE reads, and the main query's joins to a CTE are drawn like any other join. The tables a subquery reads, in the FROM clause, the WHERE clause or the SELECT list, are highlighted too and listed as in a subquery, but a subquery in FROM has no node of its own. Try the CTE example.
How does it handle UNION, INTERSECT, and set operations?
Each SELECT in a UNION, INTERSECT, or EXCEPT is read, and the tables of all of them are highlighted in one diagram, with their joins. Above the diagram, the tool says which operator joins how many SELECTs, such as UNION ALL of 2 SELECTs. Columns Selected lists the first SELECT's columns, which name the result's columns.
What about views and materialized views?
Not yet. Only CREATE TABLE statements are read, so a view named in the query is listed under Tables Used but has no node in the diagram. Paste the view's underlying SELECT instead.
Related tools and guides
- SQL to Diagram — when you have CREATE TABLE statements but no query yet, this tool gives you the bare ER diagram. Use it for documentation and onboarding before you start writing queries.
- Visual Schema Designer — design a new schema from scratch on a visual canvas, then export DDL for PostgreSQL, MySQL, SQLite, or SQL Server when you are ready to run migrations.
- Schema Diff — compare two versions of a schema and generate the migration to go from one to the other, for you to review before running it.
- Database Design Fundamentals — the underlying theory behind normalization, key selection, and indexing strategies that make queries fast in the first place.
- API Versioning Guide — when your queries back an API, the schema changes you make are also API contract changes; this guide covers how to evolve them without breaking clients.