{"id":1656,"library":"pypika","title":"PyPika","description":"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.","status":"active","version":"0.51.1","language":"en","source_language":"en","source_url":"https://github.com/kayak/pypika","tags":["sql","query builder","database","orm"],"install":[{"cmd":"pip install pypika","lang":"bash","label":"Default install"}],"dependencies":[{"reason":"PyPika requires Python 3.8 or newer.","package":"Python","optional":false}],"imports":[{"symbol":"Query","correct":"from pypika import Query"},{"symbol":"Table","correct":"from pypika import Table"},{"symbol":"Field","correct":"from pypika import Field"},{"note":"Commonly imported with an alias for brevity.","symbol":"functions","correct":"from pypika import functions as fn"},{"note":"Dialect-specific query classes are in submodules.","wrong":"from pypika import PostgreSQLQuery","symbol":"PostgreSQLQuery","correct":"from pypika.postgres import PostgreSQLQuery"}],"quickstart":{"code":"from pypika import Query, Table, Field\nfrom pypika import functions as fn\n\ncustomers = Table('customers')\norders = Table('orders')\n\n# Basic SELECT statement\nq1 = Query.from_(customers).select(customers.id, customers.name, customers.email)\nprint(f\"\\nBasic Select:\\n{q1.get_sql()}\")\n\n# Select with WHERE clause and function\nq2 = Query.from_(orders).select(orders.id, orders.amount).where(orders.amount > 100)\nprint(f\"\\nSelect with WHERE:\\n{q2.get_sql()}\")\n\n# Select with JOIN and GROUP BY\nq3 = Query.from_(customers).join(orders).on(customers.id == orders.customer_id) \\\n    .groupby(customers.name).select(customers.name, fn.Sum(orders.amount))\nprint(f\"\\nSelect with JOIN and GROUP BY:\\n{q3.get_sql()}\")","lang":"python","description":"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."},"warnings":[{"fix":"Append `.get_sql()` to your query chain, e.g., `query_object.get_sql()`.","message":"Always call `.get_sql()` on your query object to render the SQL string. Without it, you'll be working with the PyPika query object itself, not the executable SQL.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"Be mindful of SQL dialect differences, especially with quoting characters and specific functions. PyPika defaults to standard SQL but allows specifying a dialect.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"Do not confuse string literals with `Field` objects. When referring to column names, use `Table.field_name` or `Field('field_name')`. Only use raw strings for actual string values in WHERE clauses, etc., to prevent SQL injection.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}