SQL AST Parser & Explorer¶
Turn a SQL statement into a readable map of its statements, clauses, tables, columns, functions, and expressions. The live preview uses the real General SQL Parser (GSP) Java engine and shows the parser nodes together with their locations in the source SQL.
Open SQL Playground Read AST reference
Looking for the former interactive demo?
The maintained parser now runs in the SQL Playground. This page remains at the original AST Explorer URL as the guide: use it to understand the output, then open the Playground to parse your own SQL.
Try the SQL AST explorer¶
The shortest path from SQL text to an AST preview has three steps:
- Open the SQL Playground and choose the database dialect that produced the SQL.
- Paste a statement or script and select Analyze SQL. Fix any reported syntax errors before continuing.
- Open the AST tab. Each row identifies a GSP node, its place in the node hierarchy, and the source range it covers.
The adjacent Overview and Objects tabs answer different questions. Overview reports statement and parser-error information; Objects lists the tables and column references found in the SQL. The AST tab is the structural view.
Public-preview boundaries
The Playground accepts at most 64 KiB of SQL and returns a bounded AST preview. Node labels and source coordinates come from GSP, but SQL fragments are omitted and a large tree may be truncated. Use the local SDK when you need the complete tree, programmatic traversal, or production-scale parsing.
What a SQL AST represents¶
An abstract syntax tree records the grammatical structure of SQL rather than
displaying it as one flat string. A SELECT statement becomes a parent node;
its result columns, table sources, join conditions, predicates, grouping, and
ordering appear beneath it as related nodes.
That structure lets software ask precise questions such as:
- Which expression produces this result column?
- Is this table a direct source, a joined table, a CTE, or a derived table?
- Which operands belong to an
AND,OR, comparison, or function call? - Where did a node begin and end in the original SQL?
- Which part of the tree should a rewrite change without touching literals, comments, or unrelated identifiers?
An AST is therefore useful for SQL validation, editor tooling, linting, object extraction, policy checks, SQL rewriting, migration analysis, and the semantic work that precedes data lineage.
Read a real SQL AST¶
Consider this PostgreSQL query:
1 2 3 4 5 6 7 | |
The diagram below is a deliberately simplified view of the GSP nodes returned for that query. The complete preview also contains intermediate nodes that preserve identifiers, aliases, expression structure, and source positions.
flowchart TD
A[TSelectSqlStatement] --> B[TSelectSqlNode]
B --> C[TResultColumn: c.customer_id]
B --> D[TResultColumn: SUM of o.amount]
D --> E[TFunctionCall: SUM]
B --> F[TJoin: customers and orders]
F --> G[TTable: customers c]
F --> H[TTable: orders o]
B --> I[TWhereClause]
I --> J[TExpression: status comparison]
B --> K[TGroupBy: c.customer_id]
B --> L[TOrderBy: total_amount descending]
Read it from the root downward:
- Statement —
TSelectSqlStatementidentifies the top-level SQL operation. A script with multiple statements has multiple statement roots. - Projection — each
TResultColumnrepresents one item in theSELECTlist. The aggregate result contains aTFunctionCallforSUM. - Sources —
TTable,TJoin, and related nodes describe the two table sources and the join that connects them. - Predicate —
TWhereClauseowns aTExpressionfor the comparison with'paid'. Complex boolean conditions form deeper expression trees. - Result shape —
TGroupByandTOrderByrecord grouping and ordering separately from the projection and predicate.
Common nodes you will encounter¶
| GSP node | What it represents | What to inspect |
|---|---|---|
TSelectSqlStatement |
A complete SELECT, including nested or set-operation queries |
Statement type and major clauses |
TResultColumn |
One expression in a SELECT list |
Expression, alias, and source span |
TTable |
A table, view, CTE reference, derived table, or join source | Name, alias, and table-source type |
TJoin / TJoinExpr |
A joined table structure | Left and right sources, join type, and condition |
TWhereClause |
The WHERE clause wrapper |
Its condition expression |
TExpression |
An identifier, literal, operator, predicate, or nested expression | Expression type and operands |
TFunctionCall |
A scalar, aggregate, analytic, or dialect-specific function call | Function name and arguments |
TObjectName |
A table, column, alias, schema, or other identifier occurrence | Identifier parts and resolved source |
TGroupBy / TOrderBy |
Grouping and ordering clauses | Items, expressions, and direction |
Repeated TExpression and TObjectName nodes are normal. The same visible
fragment can participate in several structural relationships, and a complex
predicate is itself a tree of smaller expressions.
AST, tokens, objects, lineage, and query plans¶
These views are related, but they are not interchangeable:
| View | The question it answers | Example |
|---|---|---|
| Tokens | What lexical units did the parser recognize? | SELECT, identifier, comma, string literal |
| AST / parse tree | How is the SQL grammatically structured? | A comparison expression inside a WHERE clause |
| Resolved objects | Which tables and columns does an identifier refer to? | o.amount belongs to orders aliased as o |
| Data lineage | How does data move from sources to targets? | orders.amount contributes to total_amount |
| Database query plan | How will a database engine execute the statement? | Index scan, hash join, sort |
GSP builds parser and semantic structures without asking a database to execute the SQL. A database query plan is produced by the database optimizer and is not the same thing as an AST.
The online tab is described as an AST preview because that is the common developer term. More precisely, it is a bounded token-span parse-tree view: it keeps concrete GSP node types and source coordinates so you can connect tree structure back to the input text.
Select the dialect before interpreting the tree¶
SQL has no single universal grammar. PostgreSQL, Oracle, SQL Server, MySQL, Snowflake, BigQuery, and other systems assign different meanings to keywords, operators, procedural blocks, quoting rules, and vendor extensions.
Choose the dialect that matches the system that produced the SQL. Parsing a valid vendor extension with the wrong grammar can produce a syntax error or a different structure, and that difference matters when your application later traverses or rewrites the tree.
Review SQL syntax support by database
Move from the preview to the SDK¶
Use the online explorer when you need to understand an unfamiliar statement, compare dialect behavior, inspect node names, or reproduce a small parser question. Use the Java or .NET SDK when you need to:
- traverse every node without the public preview limit;
- integrate parsing into an IDE, service, build, or desktop application;
- retain complete SQL text and node relationships inside your process;
- resolve table and column references with application metadata;
- modify AST nodes and regenerate SQL;
- run offline or keep SQL inside your own environment.
The Playground sends the SQL to Gudu's demo service for parsing over HTTPS. It does not execute the SQL or connect to your database. Do not paste passwords, credentials, or other secrets into any online tool. The SDK performs parsing in your own Java or .NET process and does not require a database connection.
Continue learning¶
- Install GSP and parse your first statement
- Build a first SQL analysis tool
- Learn basic SQL parsing and AST inspection
- Browse statement, clause, expression, and table nodes
- Modify a SQL AST and regenerate SQL safely
- Walk deep AST structures without recursion
Frequently asked questions¶
Is the SQL AST explorer free?¶
The public SQL Playground is free to try, rate-limited, and intended for evaluation and small examples. The Java and .NET SDKs are separate libraries for local and production use.
Does the explorer execute my SQL?¶
No. It parses the text and returns structural information. It does not run the statement or connect to the tables named in it.
Is the online AST the complete tree?¶
Not necessarily. The public response is bounded and reports when its node limit has been reached. Run the SDK locally when every node is required.
Why does the AST change when I select another database?¶
Each dialect has its own grammar and vendor-specific constructs. The selected grammar determines which tokens and productions are valid and therefore which nodes the parser creates.
Can I use the AST to change SQL?¶
Yes. GSP exposes nodes for inspection and modification, and its script generator can serialize a changed tree back to SQL. Follow the verified AST rewrite guide for a complete Java and .NET example with automated tests.