sql-metadata
Uses sqlglot to parse SQL queries and extract metadata
What it is and what it does
sql-metadata wraps sqlglot to parse SQL queries and extract structural metadata without executing them. It identifies columns, tables, aliases, query types (SELECT, INSERT, UPDATE, DELETE, etc.), and values from INSERT statements. The parser automatically resolves column aliases back to their source columns, table aliases to their actual table names, and subquery aliases to their definitions—useful when you need to understand query dependencies without running the query itself.
The package supports MySQL, PostgreSQL, SQLite, MSSQL, and Apache Hive syntax. It provides both raw token extraction and semantic analysis: you can get columns grouped by their clause (SELECT, WHERE, JOIN, ORDER BY, etc.), output column names preserving aliases, WITH clause definitions, and limit/offset values. It raises InvalidQueryDefinition for structurally invalid SQL, making error handling straightforward.
Use it for:
- Extract column and table dependencies from queries to build data lineage or impact analysis tools
- Validate query structure and detect referenced tables before execution in data pipelines
- Resolve aliases and normalize queries for logging, auditing, or query rewriting systems
- Analyze INSERT statements to map input values to their target columns programmatically
- Build IDE or documentation tools that need to understand query structure without parsing SQL yourself
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Parses SQL queries using sqlglot and extracts metadata: column names, table names, aliases, query types, and values from INSERT statements.
Yes. Low install friction, active maintenance, MIT license, no known vulnerabilities, and a single well-maintained dependency. Install it if you need to extract or analyze SQL query structure—it handles the parsing complexity so you don't have to.
Install
sql-metadata on PyPI
pip
pip install sql-metadatauv
uv add sql-metadatapoetry
poetry add sql-metadataInstalling sql-metadata
Before you install
Low friction: pure Python wheel with a single runtime dependency (sqlglot). Actively maintained with a release 45 days ago and 882 repository stars.
License in practice
MIT license permits unrestricted use, modification, and distribution in both open-source and commercial projects.
Quickstart
pip install sql-metadata
from sql_metadata import Parser
parser = Parser("SELECT a, b FROM users WHERE id = 1")
print(parser.columns) # ['a', 'b']
print(parser.tables) # ['users']
Verify before relying
- Whether performance characteristics are acceptable for large or complex queries
- Coverage completeness for edge cases across all five supported SQL dialects
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (<4.0,>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — sqlglot |
| Maintenance | actively maintained — 45 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 6,799,628/month — #1,845 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: sql_metadata-3.0.1-py3-none-any.whl
Tags
More Database packages
psycopg2-binary is a PostgreSQL database…
copyleft · top 1,000 on PyPI
redisPython client library for connecting to and…
permissive · top 1,000 on PyPI
ydbYDB Python SDK is the official client library…
permissive · top 1,000 on PyPI
snowflake-connector-pythonConnects Python applications to Snowflake data…
permissive · top 1,000 on PyPI
sqlparsesqlparse tokenizes SQL text into a tree of…
permissive · top 1,000 on PyPI
dbt-adaptersProvides base adapter protocols and shared…
permissive · top 1,000 on PyPI
sqlglotSQLGlot parses, transpiles, optimizes, and…
permissive · top 1,000 on PyPI
sqlglotrsA SQL parser, transpiler, and optimizer that…
permissive · top 5,000 on PyPI
sqllineageSQLLineage parses SQL statements to extract and…
permissive · top 5,000 on PyPI
sqlglotcProvides mypyc-compiled C extensions that…
permissive · top 5,000 on PyPI
acryl-sqlglotParses, transpiles, and optimizes SQL across 23…
permissive · top 15,000 on PyPI
collate-sqllineageAnalyzes SQL statements to extract source and…
permissive · top 5,000 on PyPI
DataPropertyExtracts and classifies properties from…
permissive · top 5,000 on PyPI
sqlvalidatorFormats, validates, and checks SQL SELECT…
permissive · top 15,000 on PyPI
python-sqlConstructs SQL queries programmatically using…
permissive · top 15,000 on PyPI
syntaqliteParse, format, validate, and tokenize SQLite…
permissive · top 15,000 on PyPI