PyPika is a SQL query builder API for Python, allowing users to construct SQL queries programmatically. It supports various SQL dialects like PostgreSQL, MySQL, Oracle, and Redshift, and even JQL. The library is actively maintained, with frequent patch and minor releases, currently at version 0.51.1.
pip install pypikaVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to construct basic SELECT queries, including filtering with WHERE, using aggregate functions, and performing JOINs and GROUP BY clauses using PyPika's fluent API.
Append `.get_sql()` to your query chain, e.g., `query_object.get_sql()`.
Instantiate a dialect-specific query class (e.g., `PostgreSQLQuery`, `MySQLQuery`) or pass `dialect=Dialects.POSTGRES` (or similar) to `get_sql()`. For quoting issues, `get_sql(quote_char=None)` can be useful for databases like SQLite or when embedding in environments that handle quoting.
Use `table.column_name` for column references. For string values in comparisons, ensure they are passed as regular Python strings, PyPika handles their escaping/quoting.