Analyzing the Parsed Statement

When the parse() function is called the returned value is a tree-ish representation of the analyzed statements. The returned objects can be used by applications to retrieve further information about the parsed SQL.

Base Classes

All returned objects inherit from these base classes. The Token class represents a single token and TokenList class is a group of tokens. The latter provides methods for inspecting its child tokens.

class sqlparse.sql.Token(ttype, value)

Base class for all other classes in this module.

It represents a single token and has two instance attributes: value is the unchanged value of the token and ttype is the type of the token.


Resolve subgroups.


Returns True if other is in this tokens ancestry.


Returns True if this token is a direct child of other.

match(ttype, values, regex=False)

Checks whether the token matches the given arguments.

ttype is a token type. If this token doesn’t match the given token type. values is a list of possible values for this token. The values are OR’ed together so if only one of the values matches True is returned. Except for keyword tokens the comparison is case-sensitive. For convenience it’s OK to pass in a single string. If regex is True (default is False) the given values are treated as regular expressions.


Returns True if this token is within group_cls.

Use this method for example to check if an identifier is within a function: t.within(sql.Function).

class sqlparse.sql.TokenList(tokens=None)

A group of tokens.

It has an additional instance attribute tokens which holds a list of child-tokens.


Generator yielding ungrouped tokens.

This method is recursively called for all child tokens.


Returns the alias for this identifier or None.


Returns the name of this identifier.

This is either it’s alias or it’s real name. The returned valued can be considered as the name under which the object corresponding to this identifier is known within the current statement.


Return name of the parent object if any.

A parent object is identified by the first occurring dot.


Returns the real name (object name) of this identifier.


Returns the token that is on position offset.

group_tokens(grp_cls, start, end, include_end=True, extend=False)

Replace tokens by an instance of grp_cls.


Returns True if an alias is present.

insert_after(where, token, skip_ws=True)

Inserts token after where.

insert_before(where, token)

Inserts token before where.

token_first(skip_ws=True, skip_cm=False)

Returns the first child token.

If skip_ws is True (the default), whitespace tokens are ignored.

if skip_cm is True (default: False), comments are ignored too.

token_index(token, start=0)

Return list index of token.

token_next(idx, skip_ws=True, skip_cm=False, _reverse=False)

Returns the next token relative to idx.

If skip_ws is True (the default) whitespace tokens are ignored. If skip_cm is True comments are ignored. None is returned if there’s no next token.

token_prev(idx, skip_ws=True, skip_cm=False)

Returns the previous token relative to idx.

If skip_ws is True (the default) whitespace tokens are ignored. If skip_cm is True comments are ignored. None is returned if there’s no previous token.

SQL Representing Classes

The following classes represent distinct parts of a SQL statement.

class sqlparse.sql.Statement(tokens=None)

Represents a SQL statement.


Returns the type of a statement.

The returned value is a string holding an upper-cased reprint of the first DML or DDL keyword. If the first token in this group isn’t a DML or DDL keyword “UNKNOWN” is returned.

Whitespaces and comments at the beginning of the statement are ignored.

class sqlparse.sql.Comment(tokens=None)

A comment.

class sqlparse.sql.Identifier(tokens=None)

Represents an identifier.

Identifiers may have aliases or typecasts.


Returns an iterator of index token lists


Returns the ordering or None as uppercase string.


Returns the typecast or None of this object as a string.


Return True if this identifier contains a wildcard.

class sqlparse.sql.IdentifierList(tokens=None)

A list of Identifier’s.


Returns the identifiers.

Whitespaces and punctuations are not included in this generator.

class sqlparse.sql.Where(tokens=None)

A WHERE clause.

class sqlparse.sql.Case(tokens=None)

A CASE statement with one or more WHEN and possibly an ELSE part.


Returns a list of 2-tuples (condition, value).

If an ELSE exists condition is None.

class sqlparse.sql.Parenthesis(tokens=None)

Tokens between parenthesis.

class sqlparse.sql.If(tokens=None)

An ‘if’ clause with possible ‘else if’ or ‘else’ parts.

class sqlparse.sql.For(tokens=None)

A ‘FOR’ loop.

class sqlparse.sql.Assignment(tokens=None)

An assignment like ‘var := val;’

class sqlparse.sql.Comparison(tokens=None)

A comparison used for example in WHERE clauses.