# sqlparse: A Powerful Non-Validating SQL Parser for Python

This repository profile is provided by osrepos.com, an open source repository discovery platform.

Source: osrepos.com
Repository profile: https://osrepos.com/repo/andialbrecht-sqlparse
Generated for open source discovery and AI-assisted research.

sqlparse is a robust, non-validating SQL parser module for Python, designed to tokenize SQL text and group its components into a structured tree. It serves as a foundational tool for building formatters, linters, and query analysis applications. Developers can leverage its capabilities to split SQL scripts, format statements, and deeply inspect their token trees.

GitHub: https://github.com/andialbrecht/sqlparse
OSRepos URL: https://osrepos.com/repo/andialbrecht-sqlparse

## Summary

sqlparse is a robust, non-validating SQL parser module for Python, designed to tokenize SQL text and group its components into a structured tree. It serves as a foundational tool for building formatters, linters, and query analysis applications. Developers can leverage its capabilities to split SQL scripts, format statements, and deeply inspect their token trees.

## Topics

- Python
- SQL
- SQL Parser
- SQL Formatter
- Tokenizer
- Code Quality
- Development
- Open Source

## Repository Information

Last analyzed by OSRepos: Thu Jul 30 2026 00:45:01 GMT+0100 (Western European Summer Time)
Detail views: 1
GitHub clicks: 0

## Safety Notice

OSRepos shares public repositories for knowledge and discovery only. Review source code, dependencies, licenses, and security implications before running or installing anything.

## Content

## Introduction

`sqlparse` is a non-validating SQL parser for Python, offering powerful capabilities to process SQL text. It excels at splitting scripts into individual statements, formatting them according to various styles, and allowing users to traverse their intricate token trees.

This library tokenizes SQL text, organizing the recognized parts into a hierarchical structure of statements, clauses, identifiers, and expressions. A key advantage of `sqlparse` is its flexibility: it accepts any input without validation and makes no assumptions about specific SQL dialects. This makes it highly adaptable for handling vendor extensions and templated SQL, serving as an essential building block for formatters, linters, editors, and advanced query analysis tools.

## Installation

Getting started with `sqlparse` is straightforward. You can install it using pip:

bash
pip install sqlparse


## Examples

`sqlparse` provides three primary module-level functions to cover most common needs: `split()`, `format()`, and `parse()`.

### Split a script into statements

Easily break down a SQL script containing multiple statements into a list of individual statements:

python
import sqlparse

sqlparse.split("select * from foo; select * from bar;")
# ['select * from foo;', 'select * from bar;']


### Format a statement

Enhance the readability of your SQL queries with `sqlparse`'s formatting capabilities. You can specify options like reindentation and keyword casing:

python
print(sqlparse.format("select id,name from users where active=1",
                      reindent=True, keyword_case="upper"))


This will produce:

sql
SELECT id,
       name
FROM users
WHERE active=1


The `format()` function supports various options, including `reindent`, `keyword_case`, `strip_comments`, and `indent_width`, similar to its command-line counterpart.

### Parse and inspect a statement

For deeper analysis, `sqlparse` allows you to parse a statement and inspect its token tree. This is invaluable for understanding the structure of complex queries:

python
statement = sqlparse.parse("select id, name from users where active = 1")[0]

print(statement.get_type())
for token in statement.tokens:
    if not token.is_whitespace:
        print(f"{token.ttype or type(token).__name__!s:20} {token!s}")


Output will resemble:


SELECT
Token.Keyword.DML    select
IdentifierList       id, name
Token.Keyword        from
Identifier           users
Where                where active = 1


Tokens with a `ttype` are leaf nodes, while others represent groups that can be further explored. This hierarchical representation is how nested constructs, such as subqueries and `CASE` expressions, are managed. For more details on traversing the API, refer to the [Analyzing SQL statements](https://sqlparse.readthedocs.io/en/latest/analyzing.html) documentation.

## Why Use sqlparse?

`sqlparse` stands out as an indispensable tool for anyone working with SQL in Python due to several key features:

*   **Flexibility**: Its non-validating nature and dialect-agnostic approach mean it can parse virtually any SQL, including vendor-specific extensions and templated queries.
*   **Foundation for Tools**: It provides the core parsing logic needed to build sophisticated SQL formatters, linters, editors, and advanced query analysis applications.
*   **Simple API**: Common tasks like splitting, formatting, and parsing SQL are made easy with intuitive module-level functions.
*   **Command-Line Utility**: The `sqlformat` command-line tool offers quick formatting directly from your terminal, supporting in-place file modifications.
*   **Pre-commit Integration**: Seamlessly integrate `sqlparse` into your development workflow using pre-commit hooks to ensure consistent SQL formatting across your projects.

## Links

Explore `sqlparse` further with these official resources:

*   Documentation: [https://sqlparse.readthedocs.io/](https://sqlparse.readthedocs.io/)
*   Release notes: [https://sqlparse.readthedocs.io/en/latest/changes.html](https://sqlparse.readthedocs.io/en/latest/changes.html)
*   Issues: [https://github.com/andialbrecht/sqlparse/issues](https://github.com/andialbrecht/sqlparse/issues)
*   Discussions: [https://github.com/andialbrecht/sqlparse/discussions](https://github.com/andialbrecht/sqlparse/discussions)
*   Online demo: [https://sqlformat.org/](https://sqlformat.org/)