{"id":13494349,"url":"https://github.com/kayak/pypika","last_synced_at":"2026-02-01T20:14:48.303Z","repository":{"id":37692513,"uuid":"62725884","full_name":"kayak/pypika","owner":"kayak","description":"PyPika is a python SQL query builder that exposes the full richness of the SQL language using a syntax that reflects the resulting query. PyPika excels at all sorts of SQL queries but is especially useful for data analysis.","archived":false,"fork":false,"pushed_at":"2026-01-14T12:33:44.000Z","size":1349,"stargazers_count":2867,"open_issues_count":229,"forks_count":323,"subscribers_count":35,"default_branch":"master","last_synced_at":"2026-01-14T16:46:07.152Z","etag":null,"topics":["builder","data","functional","python","python3","pythonic","query","sql"],"latest_commit_sha":null,"homepage":"http://pypika.readthedocs.io/en/latest/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kayak.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.rst","funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2016-07-06T14:08:50.000Z","updated_at":"2026-01-14T14:53:04.000Z","dependencies_parsed_at":"2023-09-22T11:34:10.466Z","dependency_job_id":"a4fecc1a-0208-48a9-bc86-5b40e8bbc476","html_url":"https://github.com/kayak/pypika","commit_stats":{"total_commits":759,"total_committers":101,"mean_commits":7.514851485148514,"dds":0.5718050065876152,"last_synced_commit":"53a77eb9b5bda70fd0780b66a4f68daa14fe3682"},"previous_names":[],"tags_count":80,"template":false,"template_full_name":null,"purl":"pkg:github/kayak/pypika","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kayak%2Fpypika","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kayak%2Fpypika/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kayak%2Fpypika/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kayak%2Fpypika/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kayak","download_url":"https://codeload.github.com/kayak/pypika/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kayak%2Fpypika/sbom","scorecard":{"id":552118,"data":{"date":"2025-08-11","repo":{"name":"github.com/kayak/pypika","commit":"1c9646f0a019a167c32b649b6f5e6423c5ba2c9b"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":4.2,"checks":[{"name":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Code-Review","score":9,"reason":"Found 26/27 approved changesets -- score normalized to 9","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Token-Permissions","score":0,"reason":"detected GitHub workflow tokens with excessive permissions","details":["Warn: no topLevel permission defined: .github/workflows/ci.yml:1","Info: no jobLevel write permissions found"],"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Pinned-Dependencies","score":0,"reason":"dependency not pinned by hash detected -- score normalized to 0","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/kayak/pypika/ci.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/kayak/pypika/ci.yml/master?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/kayak/pypika/ci.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:26: update your workflow using https://app.stepsecurity.io/secureworkflow/kayak/pypika/ci.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:28: update your workflow using https://app.stepsecurity.io/secureworkflow/kayak/pypika/ci.yml/master?enable=pin","Warn: pipCommand not pinned by hash: .github/workflows/ci.yml:34","Info:   0 out of   4 GitHub-owned GitHubAction dependencies pinned","Info:   0 out of   1 third-party GitHubAction dependencies pinned","Info:   0 out of   1 pipCommand dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE.txt:0","Info: FSF or OSI recognized license: Apache License 2.0: LICENSE.txt:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":-1,"reason":"internal error: error during branchesHandler.setup: internal error: githubv4.Query: Resource not accessible by integration","details":null,"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"Vulnerabilities","score":5,"reason":"5 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: GHSA-cpwx-vrp4-4pq7","Warn: Project is vulnerable to: GHSA-gmj6-6f8f-6699","Warn: Project is vulnerable to: GHSA-h5c8-rqwp-cp95","Warn: Project is vulnerable to: GHSA-h75v-3vvj-5mfj","Warn: Project is vulnerable to: GHSA-q2x7-8rv6-6q7h"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 29 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}}]},"last_synced_at":"2025-08-20T11:09:43.563Z","repository_id":37692513,"created_at":"2025-08-20T11:09:43.563Z","updated_at":"2025-08-20T11:09:43.563Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28478153,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-16T06:30:42.265Z","status":"ssl_error","status_checked_at":"2026-01-16T06:30:16.248Z","response_time":107,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["builder","data","functional","python","python3","pythonic","query","sql"],"created_at":"2024-07-31T19:01:24.182Z","updated_at":"2026-01-16T11:13:28.507Z","avatar_url":"https://github.com/kayak.png","language":"Python","funding_links":[],"categories":["Python","Third-Party Extensions","Database Clients"],"sub_categories":["Databases"],"readme":"PyPika - Python Query Builder\n=============================\n\n.. _intro_start:\n\n|BuildStatus|  |CoverageStatus|  |Codacy|  |Docs|  |PyPi|  |License|\n\nAbstract\n--------\n\nWhat is |Brand|?\n\n|Brand| is a Python API for building SQL queries. The motivation behind |Brand| is to provide a simple interface for\nbuilding SQL queries without limiting the flexibility of handwritten SQL. Designed with data analysis in mind, |Brand|\nleverages the builder design pattern to construct queries to avoid messy string formatting and concatenation. It is also\neasily extended to take full advantage of specific features of SQL database vendors.\n\nWhat are the design goals for |Brand|?\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n|Brand| is a fast, expressive and flexible way to replace handwritten SQL (or even ORM for the courageous souls amongst you).\nValidation of SQL correctness is not an explicit goal of |Brand|. With such a large number of\nSQL database vendors providing a robust validation of input data is difficult. Instead you are encouraged to check inputs you provide to |Brand| or appropriately handle errors raised from\nyour SQL database - just as you would have if you were writing SQL yourself.\n\n.. _intro_end:\n\nRead the docs: http://pypika.readthedocs.io/en/latest/\n\nInstallation\n------------\n\n.. _installation_start:\n\n|Brand| supports is tested for supported Python, i.e. 3.9+. It is tested for PyPy3.9 and PyPy3.10. It may also work Cython, and Jython but is not being tested for in the CI script.\n\nTo install |Brand| run the following command:\n\n.. code-block:: bash\n\n    pip install pypika\n\n\n.. _installation_end:\n\n\nTutorial\n--------\n\n.. _tutorial_start:\n\nThe main classes in pypika are ``pypika.Query``, ``pypika.Table``, and ``pypika.Field``.\n\n.. code-block:: python\n\n    from pypika import Query, Table, Field\n\n\nSelecting Data\n^^^^^^^^^^^^^^\n\nThe entry point for building queries is ``pypika.Query``.  In order to select columns from a table, the table must\nfirst be added to the query.  For simple queries with only one table, tables and columns can be references using\nstrings.  For more sophisticated queries a ``pypika.Table`` must be used.\n\n.. code-block:: python\n\n    q = Query.from_('customers').select('id', 'fname', 'lname', 'phone')\n\nTo convert the query into raw SQL, it can be cast to a string.\n\n.. code-block:: python\n\n    str(q)\n\nAlternatively, you can use the `Query.get_sql()` function:\n\n.. code-block:: python\n\n    q.get_sql()\n\n\nTables, Columns, Schemas, and Databases\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nIn simple queries like the above example, columns in the \"from\" table can be referenced by passing string names into\nthe ``select`` query builder function. In more complex examples, the ``pypika.Table`` class should be used. Columns can be\nreferenced as attributes on instances of ``pypika.Table``.\n\n.. code-block:: python\n\n    from pypika import Table, Query\n\n    customers = Table('customers')\n    q = Query.from_(customers).select(customers.id, customers.fname, customers.lname, customers.phone)\n\nBoth of the above examples result in the following SQL:\n\n.. code-block:: sql\n\n    SELECT id,fname,lname,phone FROM customers\n\nAn alias for the table can be given using the ``.as_`` function on ``pypika.Table``\n\n.. code-block:: sql\n\n    customers = Table('x_view_customers').as_('customers')\n    q = Query.from_(customers).select(customers.id, customers.phone)\n\n.. code-block:: sql\n\n    SELECT id,phone FROM x_view_customers customers\n\nA schema can also be specified. Tables can be referenced as attributes on the schema.\n\n.. code-block:: sql\n\n    from pypika import Table, Query, Schema\n\n    views = Schema('views')\n    q = Query.from_(views.customers).select(customers.id, customers.phone)\n\n.. code-block:: sql\n\n    SELECT id,phone FROM views.customers\n\nAlso references to databases can be used. Schemas can be referenced as attributes on the database.\n\n.. code-block:: sql\n\n    from pypika import Table, Query, Database\n\n    my_db = Database('my_db')\n    q = Query.from_(my_db.analytics.customers).select(customers.id, customers.phone)\n\n.. code-block:: sql\n\n    SELECT id,phone FROM my_db.analytics.customers\n\n\nResults can be ordered by using the following syntax:\n\n.. code-block:: python\n\n    from pypika import Order\n    Query.from_('customers').select('id', 'fname', 'lname', 'phone').orderby('id', order=Order.desc)\n\nThis results in the following SQL:\n\n.. code-block:: sql\n\n    SELECT \"id\",\"fname\",\"lname\",\"phone\" FROM \"customers\" ORDER BY \"id\" DESC\n\nArithmetic\n\"\"\"\"\"\"\"\"\"\"\n\nArithmetic expressions can also be constructed using pypika.  Operators such as `+`, `-`, `*`, and `/` are implemented\nby ``pypika.Field`` which can be used simply with a ``pypika.Table`` or directly.\n\n.. code-block:: python\n\n    from pypika import Field\n\n    q = Query.from_('account').select(\n        Field('revenue') - Field('cost')\n    )\n\n.. code-block:: sql\n\n    SELECT revenue-cost FROM accounts\n\nUsing ``pypika.Table``\n\n.. code-block:: python\n\n    accounts = Table('accounts')\n    q = Query.from_(accounts).select(\n        accounts.revenue - accounts.cost\n    )\n\n.. code-block:: sql\n\n    SELECT revenue-cost FROM accounts\n\nAn alias can also be used for fields and expressions.\n\n.. code-block:: sql\n\n    q = Query.from_(accounts).select(\n        (accounts.revenue - accounts.cost).as_('profit')\n    )\n\n.. code-block:: sql\n\n    SELECT revenue-cost profit FROM accounts\n\nMore arithmetic examples\n\n.. code-block:: python\n\n    table = Table('table')\n    q = Query.from_(table).select(\n        table.foo + table.bar,\n        table.foo - table.bar,\n        table.foo * table.bar,\n        table.foo / table.bar,\n        (table.foo+table.bar) / table.fiz,\n    )\n\n.. code-block:: sql\n\n    SELECT foo+bar,foo-bar,foo*bar,foo/bar,(foo+bar)/fiz FROM table\n\nBitwise operations are also supported using the ``bitwiseand`` and ``bitwiseor`` methods.\n\n.. code-block:: python\n\n    from pypika import Query, Field\n\n    q = Query.from_('flags').select('name').where(Field('permissions').bitwiseand(4) == 4)\n\n.. code-block:: sql\n\n    SELECT \"name\" FROM \"flags\" WHERE (\"permissions\" \u0026 4)=4\n\n.. code-block:: python\n\n    q = Query.from_('flags').select('name').where(Field('permissions').bitwiseor(2) == 3)\n\n.. code-block:: sql\n\n    SELECT \"name\" FROM \"flags\" WHERE (\"permissions\" | 2)=3\n\n\nFiltering\n\"\"\"\"\"\"\"\"\"\n\nQueries can be filtered with ``pypika.Criterion`` by using equality or inequality operators\n\n.. code-block:: python\n\n    customers = Table('customers')\n    q = Query.from_(customers).select(\n        customers.id, customers.fname, customers.lname, customers.phone\n    ).where(\n        customers.lname == 'Mustermann'\n    )\n\n.. code-block:: sql\n\n    SELECT id,fname,lname,phone FROM customers WHERE lname='Mustermann'\n\nQuery methods such as select, where, groupby, and orderby can be called multiple times.  Multiple calls to the where\nmethod will add additional conditions as\n\n.. code-block:: python\n\n    customers = Table('customers')\n    q = Query.from_(customers).select(\n        customers.id, customers.fname, customers.lname, customers.phone\n    ).where(\n        customers.fname == 'Max'\n    ).where(\n        customers.lname == 'Mustermann'\n    )\n\n.. code-block:: sql\n\n    SELECT id,fname,lname,phone FROM customers WHERE fname='Max' AND lname='Mustermann'\n\nFilters such as IN and BETWEEN are also supported\n\n.. code-block:: python\n\n    customers = Table('customers')\n    q = Query.from_(customers).select(\n        customers.id,customers.fname\n    ).where(\n        customers.age[18:65] \u0026 customers.status.isin(['new', 'active'])\n    )\n\n.. code-block:: sql\n\n    SELECT id,fname FROM customers WHERE age BETWEEN 18 AND 65 AND status IN ('new','active')\n\nFiltering with complex criteria can be created using boolean symbols ``\u0026``, ``|``, and ``^``.\n\nAND\n\n.. code-block:: python\n\n    customers = Table('customers')\n    q = Query.from_(customers).select(\n        customers.id, customers.fname, customers.lname, customers.phone\n    ).where(\n        (customers.age \u003e= 18) \u0026 (customers.lname == 'Mustermann')\n    )\n\n.. code-block:: sql\n\n    SELECT id,fname,lname,phone FROM customers WHERE age\u003e=18 AND lname='Mustermann'\n\nOR\n\n.. code-block:: python\n\n    customers = Table('customers')\n    q = Query.from_(customers).select(\n        customers.id, customers.fname, customers.lname, customers.phone\n    ).where(\n        (customers.age \u003e= 18) | (customers.lname == 'Mustermann')\n    )\n\n.. code-block:: sql\n\n    SELECT id,fname,lname,phone FROM customers WHERE age\u003e=18 OR lname='Mustermann'\n\nXOR\n\n.. code-block:: python\n\n customers = Table('customers')\n q = Query.from_(customers).select(\n     customers.id, customers.fname, customers.lname, customers.phone\n ).where(\n     (customers.age \u003e= 18) ^ customers.is_registered\n )\n\n.. code-block:: sql\n\n    SELECT id,fname,lname,phone FROM customers WHERE age\u003e=18 XOR is_registered\n\n\nConvenience Methods\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\nIn the `Criterion` class, there are the static methods `any` and `all` that allow building chains AND and OR expressions with a list of terms.\n\n.. code-block:: python\n\n    from pypika import Criterion\n\n    customers = Table('customers')\n    q = Query.from_(customers).select(\n        customers.id,\n        customers.fname\n    ).where(\n        Criterion.all([\n            customers.is_registered,\n            customers.age \u003e= 18,\n            customers.lname == \"Jones\",\n        ])\n    )\n\n.. code-block:: sql\n\n    SELECT id,fname FROM customers WHERE is_registered AND age\u003e=18 AND lname = \"Jones\"\n\n\nGrouping and Aggregating\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\nGrouping allows for aggregated results and works similar to ``SELECT`` clauses.\n\n.. code-block:: python\n\n    from pypika import functions as fn\n\n    customers = Table('customers')\n    q = Query \\\n        .from_(customers) \\\n        .where(customers.age \u003e= 18) \\\n        .groupby(customers.id) \\\n        .select(customers.id, fn.Sum(customers.revenue))\n\n.. code-block:: sql\n\n    SELECT id,SUM(\"revenue\") FROM \"customers\" WHERE \"age\"\u003e=18 GROUP BY \"id\"\n\nAfter adding a ``GROUP BY`` clause to a query, the ``HAVING`` clause becomes available.  The method\n``Query.having()`` takes a ``Criterion`` parameter similar to the method ``Query.where()``.\n\n.. code-block:: python\n\n    from pypika import functions as fn\n\n    payments = Table('payments')\n    q = Query \\\n        .from_(payments) \\\n        .where(payments.transacted[date(2015, 1, 1):date(2016, 1, 1)]) \\\n        .groupby(payments.customer_id) \\\n        .having(fn.Sum(payments.total) \u003e= 1000) \\\n        .select(payments.customer_id, fn.Sum(payments.total))\n\n.. code-block:: sql\n\n    SELECT customer_id,SUM(total) FROM payments\n    WHERE transacted BETWEEN '2015-01-01' AND '2016-01-01'\n    GROUP BY customer_id HAVING SUM(total)\u003e=1000\n\nThe ``QUALIFY`` clause can be used to filter rows based on window function results. This is particularly useful\nwhen you want to filter after window functions have been evaluated.\n\n.. code-block:: python\n\n    from pypika import Query, Table, analytics as an\n\n    table = Table('events')\n    rank_expr = an.Rank().over(table.user_id).orderby(table.created_at)\n\n    q = Query.from_(table).select('*').qualify(rank_expr == 1)\n\n.. code-block:: sql\n\n    SELECT * FROM \"events\" QUALIFY RANK() OVER(PARTITION BY \"user_id\" ORDER BY \"created_at\")=1\n\nGROUP BY Modifiers\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\nThe ``ROLLUP`` modifier allows for aggregating to higher levels than the given groups, called super-aggregates.\n\n.. code-block:: python\n\n    from pypika import Query, Table, Rollup, functions as fn\n\n    products = Table('products')\n    q = Query.from_(products).select(\n        products.id, products.category, fn.Sum(products.price)\n    ).rollup(products.id, products.category)\n\n.. code-block:: sql\n\n    SELECT \"id\",\"category\",SUM(\"price\") FROM \"products\" GROUP BY ROLLUP(\"id\",\"category\")\n\n\nJoining Tables and Subqueries\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\nTables and subqueries can be joined to any query using the ``Query.join()`` method.  Joins can be performed with either\na ``USING`` or ``ON`` clauses.  The ``USING`` clause can be used when both tables/subqueries contain the same field and\nthe ``ON`` clause can be used with a criterion. To perform a join, ``...join()`` can be chained but then must be\nfollowed immediately by ``...on(\u003ccriterion\u003e)`` or ``...using(*field)``.\n\n\nJoin Types\n~~~~~~~~~~\n\nAll join types are supported by |Brand|.\n\n.. code-block:: python\n\n    Query \\\n        .from_(base_table)\n        ...\n        .join(join_table, JoinType.left)\n        ...\n\n\n.. code-block:: python\n\n    Query \\\n        .from_(base_table)\n        ...\n        .left_join(join_table) \\\n        .left_outer_join(join_table) \\\n        .right_join(join_table) \\\n        .right_outer_join(join_table) \\\n        .inner_join(join_table) \\\n        .outer_join(join_table) \\\n        .full_outer_join(join_table) \\\n        .cross_join(join_table) \\\n        .hash_join(join_table) \\\n        ...\n\nSee the list of join types here ``pypika.enums.JoinTypes``\n\nExample of a join using `ON`\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code-block:: python\n\n    history, customers = Tables('history', 'customers')\n    q = Query \\\n        .from_(history) \\\n        .join(customers) \\\n        .on(history.customer_id == customers.id) \\\n        .select(history.star) \\\n        .where(customers.id == 5)\n\n\n.. code-block:: sql\n\n    SELECT \"history\".* FROM \"history\" JOIN \"customers\" ON \"history\".\"customer_id\"=\"customers\".\"id\" WHERE \"customers\".\"id\"=5\n\nAs a shortcut, the ``Query.join().on_field()`` function is provided for joining the (first) table in the ``FROM`` clause\nwith the joined table when the field name(s) are the same in both tables.\n\nExample of a join using `ON`\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code-block:: python\n\n    history, customers = Tables('history', 'customers')\n    q = Query \\\n        .from_(history) \\\n        .join(customers) \\\n        .on_field('customer_id', 'group') \\\n        .select(history.star) \\\n        .where(customers.group == 'A')\n\n\n.. code-block:: sql\n\n    SELECT \"history\".* FROM \"history\" JOIN \"customers\" ON \"history\".\"customer_id\"=\"customers\".\"customer_id\" AND \"history\".\"group\"=\"customers\".\"group\" WHERE \"customers\".\"group\"='A'\n\n\nExample of a join using `USING`\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code-block:: python\n\n    history, customers = Tables('history', 'customers')\n    q = Query \\\n        .from_(history) \\\n        .join(customers) \\\n        .using('customer_id') \\\n        .select(history.star) \\\n        .where(customers.id == 5)\n\n\n.. code-block:: sql\n\n    SELECT \"history\".* FROM \"history\" JOIN \"customers\" USING \"customer_id\" WHERE \"customers\".\"id\"=5\n\n\nExample of a correlated subquery in the `SELECT`\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code-block:: python\n\n    history, customers = Tables('history', 'customers')\n    last_purchase_at = Query.from_(history).select(\n        history.purchase_at\n    ).where(history.customer_id==customers.customer_id).orderby(\n        history.purchase_at, order=Order.desc\n    ).limit(1)\n    q = Query.from_(customers).select(\n        customers.id, last_purchase_at.as_('last_purchase_at')\n    )\n\n\n.. code-block:: sql\n\n    SELECT\n      \"id\",\n      (SELECT \"history\".\"purchase_at\"\n       FROM \"history\"\n       WHERE \"history\".\"customer_id\" = \"customers\".\"customer_id\"\n       ORDER BY \"history\".\"purchase_at\" DESC\n       LIMIT 1) \"last_purchase_at\"\n    FROM \"customers\"\n\n\nUnions\n\"\"\"\"\"\"\n\nBoth ``UNION`` and ``UNION ALL`` are supported. ``UNION DISTINCT`` is synonymous with ``UNION`` so |Brand| does not\nprovide a separate function for it.  Unions require that queries have the same number of ``SELECT`` clauses so\ntrying to cast a unioned query to string will throw a ``SetOperationException`` if the column sizes are mismatched.\n\nTo create a union query, use either the ``Query.union()`` method or `+` operator with two query instances. For a\nunion all, use ``Query.union_all()`` or the `*` operator.\n\n.. code-block:: python\n\n    provider_a, provider_b = Tables('provider_a', 'provider_b')\n    q = Query.from_(provider_a).select(\n        provider_a.created_time, provider_a.foo, provider_a.bar\n    ) + Query.from_(provider_b).select(\n        provider_b.created_time, provider_b.fiz, provider_b.buz\n    )\n\n.. code-block:: sql\n\n    SELECT \"created_time\",\"foo\",\"bar\" FROM \"provider_a\" UNION SELECT \"created_time\",\"fiz\",\"buz\" FROM \"provider_b\"\n\nIntersect\n\"\"\"\"\"\"\"\"\"\n\n``INTERSECT`` is supported. Intersects require that queries have the same number of ``SELECT`` clauses so\ntrying to cast a intersected query to string will throw a ``SetOperationException`` if the column sizes are mismatched.\n\nTo create a intersect query, use the ``Query.intersect()`` method.\n\n.. code-block:: python\n\n    provider_a, provider_b = Tables('provider_a', 'provider_b')\n    q = Query.from_(provider_a).select(\n        provider_a.created_time, provider_a.foo, provider_a.bar\n    )\n    r = Query.from_(provider_b).select(\n        provider_b.created_time, provider_b.fiz, provider_b.buz\n    )\n    intersected_query = q.intersect(r)\n\n.. code-block:: sql\n\n    SELECT \"created_time\",\"foo\",\"bar\" FROM \"provider_a\" INTERSECT SELECT \"created_time\",\"fiz\",\"buz\" FROM \"provider_b\"\n\nMinus\n\"\"\"\"\"\n\n``MINUS`` is supported. Minus require that queries have the same number of ``SELECT`` clauses so\ntrying to cast a minus query to string will throw a ``SetOperationException`` if the column sizes are mismatched.\n\nTo create a minus query, use either the ``Query.minus()`` method or `-` operator with two query instances.\n\n.. code-block:: python\n\n    provider_a, provider_b = Tables('provider_a', 'provider_b')\n    q = Query.from_(provider_a).select(\n        provider_a.created_time, provider_a.foo, provider_a.bar\n    )\n    r = Query.from_(provider_b).select(\n        provider_b.created_time, provider_b.fiz, provider_b.buz\n    )\n    minus_query = q.minus(r)\n\n    (or)\n\n    minus_query = Query.from_(provider_a).select(\n        provider_a.created_time, provider_a.foo, provider_a.bar\n    ) - Query.from_(provider_b).select(\n        provider_b.created_time, provider_b.fiz, provider_b.buz\n    )\n\n.. code-block:: sql\n\n    SELECT \"created_time\",\"foo\",\"bar\" FROM \"provider_a\" MINUS SELECT \"created_time\",\"fiz\",\"buz\" FROM \"provider_b\"\n\nEXCEPT\n\"\"\"\"\"\"\n\n``EXCEPT`` is supported. Minus require that queries have the same number of ``SELECT`` clauses so\ntrying to cast a except query to string will throw a ``SetOperationException`` if the column sizes are mismatched.\n\nTo create a except query, use the ``Query.except_of()`` method.\n\n.. code-block:: python\n\n    provider_a, provider_b = Tables('provider_a', 'provider_b')\n    q = Query.from_(provider_a).select(\n        provider_a.created_time, provider_a.foo, provider_a.bar\n    )\n    r = Query.from_(provider_b).select(\n        provider_b.created_time, provider_b.fiz, provider_b.buz\n    )\n    minus_query = q.except_of(r)\n\n.. code-block:: sql\n\n    SELECT \"created_time\",\"foo\",\"bar\" FROM \"provider_a\" EXCEPT SELECT \"created_time\",\"fiz\",\"buz\" FROM \"provider_b\"\n\nDate, Time, and Intervals\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\nUsing ``pypika.Interval``, queries can be constructed with date arithmetic.  Any combination of intervals can be\nused except for weeks and quarters, which must be used separately and will ignore any other values if selected.\n\n.. code-block:: python\n\n    from pypika import functions as fn\n\n    fruits = Tables('fruits')\n    q = Query.from_(fruits) \\\n        .select(fruits.id, fruits.name) \\\n        .where(fruits.harvest_date + Interval(months=1) \u003c fn.Now())\n\n.. code-block:: sql\n\n    SELECT id,name FROM fruits WHERE harvest_date+INTERVAL 1 MONTH\u003cNOW()\n\n\nTuples\n\"\"\"\"\"\"\n\nTuples are supported through the class ``pypika.Tuple`` but also through the native python tuple wherever possible.\nTuples can be used with ``pypika.Criterion`` in **WHERE** clauses for pairwise comparisons.\n\n.. code-block:: python\n\n    from pypika import Query, Tuple\n\n    q = Query.from_(self.table_abc) \\\n        .select(self.table_abc.foo, self.table_abc.bar) \\\n        .where(Tuple(self.table_abc.foo, self.table_abc.bar) == Tuple(1, 2))\n\n.. code-block:: sql\n\n    SELECT \"foo\",\"bar\" FROM \"abc\" WHERE (\"foo\",\"bar\")=(1,2)\n\nUsing ``pypika.Tuple`` on both sides of the comparison is redundant and |Brand| supports native python tuples.\n\n.. code-block:: python\n\n    from pypika import Query, Tuple\n\n    q = Query.from_(self.table_abc) \\\n        .select(self.table_abc.foo, self.table_abc.bar) \\\n        .where(Tuple(self.table_abc.foo, self.table_abc.bar) == (1, 2))\n\n.. code-block:: sql\n\n    SELECT \"foo\",\"bar\" FROM \"abc\" WHERE (\"foo\",\"bar\")=(1,2)\n\nTuples can be used in **IN** clauses.\n\n.. code-block:: python\n\n    Query.from_(self.table_abc) \\\n            .select(self.table_abc.foo, self.table_abc.bar) \\\n            .where(Tuple(self.table_abc.foo, self.table_abc.bar).isin([(1, 1), (2, 2), (3, 3)]))\n\n.. code-block:: sql\n\n    SELECT \"foo\",\"bar\" FROM \"abc\" WHERE (\"foo\",\"bar\") IN ((1,1),(2,2),(3,3))\n\n\nStrings Functions\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\nThere are several string operations and function wrappers included in |Brand|.  Function wrappers can be found in the\n``pypika.functions`` package.  In addition, `LIKE` and `REGEX` queries are supported as well.\n\n.. code-block:: python\n\n    from pypika import functions as fn\n\n    customers = Tables('customers')\n    q = Query.from_(customers).select(\n        customers.id,\n        customers.fname,\n        customers.lname,\n    ).where(\n        customers.lname.like('Mc%')\n    )\n\n.. code-block:: sql\n\n    SELECT id,fname,lname FROM customers WHERE lname LIKE 'Mc%'\n\n.. code-block:: python\n\n    from pypika import functions as fn\n\n    customers = Tables('customers')\n    q = Query.from_(customers).select(\n        customers.id,\n        customers.fname,\n        customers.lname,\n    ).where(\n        customers.lname.regex(r'^[abc][a-zA-Z]+\u0026')\n    )\n\n.. code-block:: sql\n\n    SELECT id,fname,lname FROM customers WHERE lname REGEX '^[abc][a-zA-Z]+\u0026';\n\n\n.. code-block:: python\n\n    from pypika import functions as fn\n\n    customers = Tables('customers')\n    q = Query.from_(customers).select(\n        customers.id,\n        fn.Concat(customers.fname, ' ', customers.lname).as_('full_name'),\n    )\n\n.. code-block:: sql\n\n    SELECT id,CONCAT(fname, ' ', lname) full_name FROM customers\n\n\nCustom Functions\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\nCustom Functions allows us to use any function on queries, as some functions are not covered by PyPika as default, we can appeal\nto Custom functions.\n\n.. code-block:: python\n\n    from pypika import CustomFunction\n\n    customers = Tables('customers')\n    DateDiff = CustomFunction('DATE_DIFF', ['interval', 'start_date', 'end_date'])\n\n    q = Query.from_(customers).select(\n        customers.id,\n        customers.fname,\n        customers.lname,\n        DateDiff('day', customers.created_date, customers.updated_date)\n    )\n\n.. code-block:: sql\n\n    SELECT id,fname,lname,DATE_DIFF('day',created_date,updated_date) FROM customers\n\nCase Statements\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\nCase statements allow fow a number of conditions to be checked sequentially and return a value for the first condition\nmet or otherwise a default value.  The Case object can be used to chain conditions together along with their output\nusing the ``when`` method and to set the default value using ``else_``.\n\n\n.. code-block:: python\n\n    from pypika import Case, functions as fn\n\n    customers = Tables('customers')\n    q = Query.from_(customers).select(\n        customers.id,\n        Case()\n           .when(customers.fname == \"Tom\", \"It was Tom\")\n           .when(customers.fname == \"John\", \"It was John\")\n           .else_(\"It was someone else.\").as_('who_was_it')\n    )\n\n\n.. code-block:: sql\n\n    SELECT \"id\",CASE WHEN \"fname\"='Tom' THEN 'It was Tom' WHEN \"fname\"='John' THEN 'It was John' ELSE 'It was someone else.' END \"who_was_it\" FROM \"customers\"\n\n\nPseudo Columns\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\nA pseudo-column is an SQL assigned value (pseudo-field) used in the same context as a column, but not stored on disk.\nThe pseudo-column can change from database to database, so here it's possible to define them.\n\n.. code-block:: python\n\n    from pypika import Query\n    from pypika.terms import PseudoColumn\n\n    CurrentDate = PseudoColumn('current_date')\n    q = Query.from_('products').select(CurrentDate)\n\n.. code-block:: sql\n\n    SELECT current_date FROM \"products\"\n\n\nWith Clause\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\nWith clause allows give a sub-query block a name, which can be referenced in several places within the main SQL query.\nThe SQL WITH clause is basically a drop-in replacement to the normal sub-query.\n\n.. code-block:: python\n\n    from pypika import Table, AliasedQuery, Query\n\n    customers = Table('customers')\n\n    sub_query = (Query\n                .from_(customers)\n                .select('*'))\n\n    test_query = (Query\n                .with_(sub_query, \"an_alias\")\n                .from_(AliasedQuery(\"an_alias\"))\n                .select('*'))\n\nYou can use as much as `.with_()` as you want.\n\n.. code-block:: sql\n\n    WITH an_alias AS (SELECT * FROM \"customers\") SELECT * FROM an_alias\n\n\nInserting Data\n^^^^^^^^^^^^^^\n\nData can be inserted into tables either by providing the values in the query or by selecting them through another query.\n\nBy default, data can be inserted by providing values for all columns in the order that they are defined in the table.\n\nInsert with values\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\n.. code-block:: python\n\n    customers = Table('customers')\n\n    q = Query.into(customers).insert(1, 'Jane', 'Doe', 'jane@example.com')\n\n.. code-block:: sql\n\n    INSERT INTO customers VALUES (1,'Jane','Doe','jane@example.com')\n\n.. code-block:: python\n\n    customers =  Table('customers')\n\n    q = customers.insert(1, 'Jane', 'Doe', 'jane@example.com')\n\n.. code-block:: sql\n\n    INSERT INTO customers VALUES (1,'Jane','Doe','jane@example.com')\n\nMultiple rows of data can be inserted either by chaining the ``insert`` function or passing multiple tuples as args.\n\n.. code-block:: python\n\n    customers = Table('customers')\n\n    q = Query.into(customers).insert(1, 'Jane', 'Doe', 'jane@example.com').insert(2, 'John', 'Doe', 'john@example.com')\n\n.. code-block:: python\n\n    customers = Table('customers')\n\n    q = Query.into(customers).insert((1, 'Jane', 'Doe', 'jane@example.com'),\n                                     (2, 'John', 'Doe', 'john@example.com'))\n\nInsert with constraint violation handling\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\nMySQL\n~~~~~\n\n.. code-block:: python\n\n    customers = Table('customers')\n\n    q = MySQLQuery.into(customers) \\\n        .insert(1, 'Jane', 'Doe', 'jane@example.com') \\\n        .on_duplicate_key_ignore())\n\n.. code-block:: sql\n\n    INSERT INTO `customers` VALUES (1,'Jane','Doe','jane@example.com') ON DUPLICATE KEY IGNORE\n\n.. code-block:: python\n\n    customers = Table('customers')\n\n    q = MySQLQuery.into(customers) \\\n        .insert(1, 'Jane', 'Doe', 'jane@example.com') \\\n        .on_duplicate_key_update(customers.email, Values(customers.email))\n\n.. code-block:: sql\n\n    INSERT INTO `customers` VALUES (1,'Jane','Doe','jane@example.com') ON DUPLICATE KEY UPDATE `email`=VALUES(`email`)\n\n``.on_duplicate_key_update`` works similar to ``.set`` for updating rows, additionally it provides the ``Values``\nwrapper to update to the value specified in the ``INSERT`` clause.\n\nPostgreSQL\n~~~~~~~~~~\n\n.. code-block:: python\n\n    customers = Table('customers')\n\n    q = PostgreSQLQuery.into(customers) \\\n        .insert(1, 'Jane', 'Doe', 'jane@example.com') \\\n        .on_conflict(customers.email) \\\n        .do_nothing()\n\n.. code-block:: sql\n\n    INSERT INTO \"customers\" VALUES (1,'Jane','Doe','jane@example.com') ON CONFLICT (\"email\") DO NOTHING\n\n.. code-block:: python\n\n    customers = Table('customers')\n\n    q = PostgreSQLQuery.into(customers) \\\n        .insert(1, 'Jane', 'Doe', 'jane@example.com') \\\n        .on_conflict(customers.email) \\\n        .do_update(customers.email, 'bob@example.com')\n\n.. code-block:: sql\n\n    INSERT INTO \"customers\" VALUES (1,'Jane','Doe','jane@example.com') ON CONFLICT (\"email\") DO UPDATE SET \"email\"='bob@example.com'\n\n\nInsert from a SELECT Sub-query\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\n.. code-block:: sql\n\n    INSERT INTO \"customers\" VALUES (1,'Jane','Doe','jane@example.com'),(2,'John','Doe','john@example.com')\n\n\nTo specify the columns and the order, use the ``columns`` function.\n\n.. code-block:: python\n\n    customers = Table('customers')\n\n    q = Query.into(customers).columns('id', 'fname', 'lname').insert(1, 'Jane', 'Doe')\n\n.. code-block:: sql\n\n    INSERT INTO customers (id,fname,lname) VALUES (1,'Jane','Doe','jane@example.com')\n\n\nInserting data with a query works the same as querying data with the additional call to the ``into`` method in the\nbuilder chain.\n\n.. code-block:: python\n\n    customers, customers_backup = Tables('customers', 'customers_backup')\n\n    q = Query.into(customers_backup).from_(customers).select('*')\n\n.. code-block:: sql\n\n    INSERT INTO customers_backup SELECT * FROM customers\n\n.. code-block:: python\n\n    customers, customers_backup = Tables('customers', 'customers_backup')\n\n    q = Query.into(customers_backup).columns('id', 'fname', 'lname')\n        .from_(customers).select(customers.id, customers.fname, customers.lname)\n\n.. code-block:: sql\n\n    INSERT INTO customers_backup SELECT \"id\", \"fname\", \"lname\" FROM customers\n\nThe syntax for joining tables is the same as when selecting data\n\n.. code-block:: python\n\n    customers, orders, orders_backup = Tables('customers', 'orders', 'orders_backup')\n\n    q = Query.into(orders_backup).columns('id', 'address', 'customer_fname', 'customer_lname')\n        .from_(customers)\n        .join(orders).on(orders.customer_id == customers.id)\n        .select(orders.id, customers.fname, customers.lname)\n\n.. code-block:: sql\n\n   INSERT INTO \"orders_backup\" (\"id\",\"address\",\"customer_fname\",\"customer_lname\")\n   SELECT \"orders\".\"id\",\"customers\".\"fname\",\"customers\".\"lname\" FROM \"customers\"\n   JOIN \"orders\" ON \"orders\".\"customer_id\"=\"customers\".\"id\"\n\nUpdating Data\n^^^^^^^^^^^^^^\nPyPika allows update queries to be constructed with or without where clauses.\n\n.. code-block:: python\n\n    customers = Table('customers')\n\n    Query.update(customers).set(customers.last_login, '2017-01-01 10:00:00')\n\n    Query.update(customers).set(customers.lname, 'smith').where(customers.id == 10)\n\n.. code-block:: sql\n\n    UPDATE \"customers\" SET \"last_login\"='2017-01-01 10:00:00'\n\n    UPDATE \"customers\" SET \"lname\"='smith' WHERE \"id\"=10\n\nThe syntax for joining tables is the same as when selecting data\n\n.. code-block:: python\n\n    customers, profiles = Tables('customers', 'profiles')\n\n    Query.update(customers)\n         .join(profiles).on(profiles.customer_id == customers.id)\n         .set(customers.lname, profiles.lname)\n\n.. code-block:: sql\n\n   UPDATE \"customers\"\n   JOIN \"profiles\" ON \"profiles\".\"customer_id\"=\"customers\".\"id\"\n   SET \"customers\".\"lname\"=\"profiles\".\"lname\"\n\nUsing ``pypika.Table`` alias to perform the update\n\n.. code-block:: python\n\n    customers = Table('customers')\n\n    customers.update()\n            .set(customers.lname, 'smith')\n            .where(customers.id == 10)\n\n.. code-block:: sql\n\n    UPDATE \"customers\" SET \"lname\"='smith' WHERE \"id\"=10\n\nUsing ``limit`` for performing update\n\n.. code-block:: python\n\n    customers = Table('customers')\n\n    customers.update()\n            .set(customers.lname, 'smith')\n            .limit(2)\n\n.. code-block:: sql\n\n    UPDATE \"customers\" SET \"lname\"='smith' LIMIT 2\n\n.. _advanced_start:\n\nAnalytic Queries\n^^^^^^^^^^^^^^^^\n\nThe ``pypika.analytics`` module contains analytic/window function wrappers. These can be used in ``SELECT`` clauses\nwhen building queries for databases that support them.\n\nNTILE and RANK\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\nThe ``NTILE`` function requires a constant integer argument while the ``RANK`` function takes no arguments.\n\n.. code-block:: python\n\n    from pypika import Query, Table, analytics as an, functions as fn\n\n    sales = Table('sales')\n    q = Query.from_(sales).select(\n        sales.region,\n        fn.Sum(sales.amount).as_('total'),\n        an.NTile(4).over(sales.region).orderby(fn.Sum(sales.amount)).as_('quartile')\n    ).groupby(sales.region)\n\nFIRST_VALUE and LAST_VALUE\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\n``FIRST_VALUE`` and ``LAST_VALUE`` both expect a single argument. They also support an additional ``IGNORE NULLS`` clause.\n\n.. code-block:: python\n\n    from pypika import Query, Table, analytics as an\n\n    t = Table('monthly_data')\n    first_val = an.FirstValue(t.value).over(t.category).orderby(t.month)\n    last_val = an.LastValue(t.value).over(t.category).orderby(t.month).ignore_nulls()\n\n    q = Query.from_(t).select(first_val, last_val)\n\n.. code-block:: sql\n\n    SELECT FIRST_VALUE(\"value\") OVER(PARTITION BY \"category\" ORDER BY \"month\"),LAST_VALUE(\"value\" IGNORE NULLS) OVER(PARTITION BY \"category\" ORDER BY \"month\") FROM \"monthly_data\"\n\nMEDIAN, AVG and STDDEV\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\nThese analytic functions take one or more arguments with window partitioning.\n\n.. code-block:: python\n\n    from pypika import Query, Table, analytics as an\n\n    customers = Table('customers')\n    median_income = an.Median(customers.income).over(customers.state).as_('median')\n    avg_income = an.Avg(customers.income).over(customers.state).as_('avg')\n\n    q = Query.from_(customers).select(median_income, avg_income)\n\nWindow Frames\n\"\"\"\"\"\"\"\"\"\"\"\"\"\n\nFunctions which use window aggregation expose the ``rows()`` and ``range()`` methods to define the window frame.\nBoundaries can be set using ``an.CURRENT_ROW``, ``an.Preceding(n)``, or ``an.Following(n)``.\nUnbounded ranges use ``an.Preceding()`` or ``an.Following()`` without arguments.\n\n.. code-block:: python\n\n    from pypika import Query, Table, analytics as an\n\n    t = Table('transactions')\n    rolling_sum = an.Sum(t.amount).over(t.account_id).orderby(t.date).rows(an.Preceding(7), an.CURRENT_ROW)\n\n    q = Query.from_(t).select(t.date, t.amount, rolling_sum.as_('rolling_7_day'))\n\n.. code-block:: sql\n\n    SELECT \"date\",\"amount\",SUM(\"amount\") OVER(PARTITION BY \"account_id\" ORDER BY \"date\" ROWS BETWEEN 7 PRECEDING AND CURRENT ROW) \"rolling_7_day\" FROM \"transactions\"\n\n\nParametrized Queries\n^^^^^^^^^^^^^^^^^^^^\n\nPyPika allows you to use ``Parameter(str)`` term as a placeholder for parametrized queries.\n\n.. code-block:: python\n\n    customers = Table('customers')\n\n    q = Query.into(customers).columns('id', 'fname', 'lname')\n        .insert(Parameter(':1'), Parameter(':2'), Parameter(':3'))\n\n.. code-block:: sql\n\n    INSERT INTO customers (id,fname,lname) VALUES (:1,:2,:3)\n\nThis allows you to build prepared statements, and/or avoid SQL-injection related risks.\n\nDue to the mix of syntax for parameters, depending on connector/driver, it is required that you specify the\nparameter token explicitly or use one of the specialized Parameter types per [PEP-0249](https://www.python.org/dev/peps/pep-0249/#paramstyle):\n``QmarkParameter()``, ``NumericParameter(int)``,  ``NamedParameter(str)``, ``FormatParameter()``, ``PyformatParameter(str)``\n\nAn example of some common SQL parameter styles used in Python drivers are:\n\nPostgreSQL:\n    ``$number`` OR ``%s`` + ``:name`` (depending on driver)\nMySQL:\n    ``%s``\nSQLite:\n    ``?``\nVertica:\n    ``:name``\nOracle:\n    ``:number`` + ``:name``\nMSSQL:\n    ``%(name)s`` OR ``:name`` + ``:number`` (depending on driver)\n\nYou can find out what parameter style is needed for DBAPI compliant drivers here: https://www.python.org/dev/peps/pep-0249/#paramstyle or in the DB driver documentation.\n\nExtracting Parameter Values\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\nWhen building parameterized queries, you can pass a parameter object to ``get_sql()`` to automatically collect\nparameter values. This is useful for executing queries with database drivers that require separate parameter lists.\n\n.. code-block:: python\n\n    from pypika import Query, Table, QmarkParameter\n\n    customers = Table('customers')\n    q = Query.from_(customers).select('*').where(\n        (customers.status == 'active') \u0026 (customers.age \u003e= 18)\n    )\n\n    parameter = QmarkParameter()\n    sql = q.get_sql(parameter=parameter)\n    params = parameter.get_parameters()\n\n    # sql: SELECT * FROM \"customers\" WHERE \"status\"=? AND \"age\"\u003e=?\n    # params: ['active', 18]\n\nThis works with all parameter types. For dict-based parameters like ``NamedParameter``:\n\n.. code-block:: python\n\n    from pypika import Query, Table, NamedParameter\n\n    customers = Table('customers')\n    q = Query.from_(customers).select('*').where(customers.status == 'active')\n\n    parameter = NamedParameter()\n    sql = q.get_sql(parameter=parameter)\n    params = parameter.get_parameters()\n\n    # sql: SELECT * FROM \"customers\" WHERE \"status\"=:param1\n    # params: {'param1': 'active'}\n\nTemporal support\n^^^^^^^^^^^^^^^^\n\nTemporal criteria can be added to the tables.\n\nSelect\n\"\"\"\"\"\"\n\nHere is a select using system time.\n\n.. code-block:: python\n\n    t = Table(\"abc\")\n    q = Query.from_(t.for_(SYSTEM_TIME.as_of('2020-01-01'))).select(\"*\")\n\nThis produces:\n\n.. code-block:: sql\n\n    SELECT * FROM \"abc\" FOR SYSTEM_TIME AS OF '2020-01-01'\n\nYou can also use between.\n\n.. code-block:: python\n\n    t = Table(\"abc\")\n    q = Query.from_(\n        t.for_(SYSTEM_TIME.between('2020-01-01', '2020-02-01'))\n    ).select(\"*\")\n\nThis produces:\n\n.. code-block:: sql\n\n    SELECT * FROM \"abc\" FOR SYSTEM_TIME BETWEEN '2020-01-01' AND '2020-02-01'\n\nYou can also use a period range.\n\n.. code-block:: python\n\n    t = Table(\"abc\")\n    q = Query.from_(\n        t.for_(SYSTEM_TIME.from_to('2020-01-01', '2020-02-01'))\n    ).select(\"*\")\n\nThis produces:\n\n.. code-block:: sql\n\n    SELECT * FROM \"abc\" FOR SYSTEM_TIME FROM '2020-01-01' TO '2020-02-01'\n\nFinally you can select for all times:\n\n.. code-block:: python\n\n    t = Table(\"abc\")\n    q = Query.from_(t.for_(SYSTEM_TIME.all_())).select(\"*\")\n\nThis produces:\n\n.. code-block:: sql\n\n    SELECT * FROM \"abc\" FOR SYSTEM_TIME ALL\n\nA user defined period can also be used in the following manner.\n\n.. code-block:: python\n\n    t = Table(\"abc\")\n    q = Query.from_(\n        t.for_(t.valid_period.between('2020-01-01', '2020-02-01'))\n    ).select(\"*\")\n\nThis produces:\n\n.. code-block:: sql\n\n    SELECT * FROM \"abc\" FOR \"valid_period\" BETWEEN '2020-01-01' AND '2020-02-01'\n\nJoins\n\"\"\"\"\"\n\nWith joins, when the table object is used when specifying columns, it is\nimportant to use the table from which the temporal constraint was generated.\nThis is because `Table(\"abc\")` is not the same table as `Table(\"abc\").for_(...)`.\nThe following example demonstrates this.\n\n.. code-block:: python\n\n    t0 = Table(\"abc\").for_(SYSTEM_TIME.as_of('2020-01-01'))\n    t1 = Table(\"efg\").for_(SYSTEM_TIME.as_of('2020-01-01'))\n    query = (\n        Query.from_(t0)\n        .join(t1)\n        .on(t0.foo == t1.bar)\n        .select(\"*\")\n    )\n\nThis produces:\n\n.. code-block:: sql\n\n    SELECT * FROM \"abc\" FOR SYSTEM_TIME AS OF '2020-01-01'\n    JOIN \"efg\" FOR SYSTEM_TIME AS OF '2020-01-01'\n    ON \"abc\".\"foo\"=\"efg\".\"bar\"\n\nUpdate \u0026 Deletes\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\nAn update can be written as follows:\n\n.. code-block:: python\n\n    t = Table(\"abc\")\n    q = Query.update(\n        t.for_portion(\n            SYSTEM_TIME.from_to('2020-01-01', '2020-02-01')\n        )\n    ).set(\"foo\", \"bar\")\n\nThis produces:\n\n.. code-block:: sql\n\n    UPDATE \"abc\"\n    FOR PORTION OF SYSTEM_TIME FROM '2020-01-01' TO '2020-02-01'\n    SET \"foo\"='bar'\n\nHere is a delete:\n\n.. code-block:: python\n\n    t = Table(\"abc\")\n    q = Query.from_(\n        t.for_portion(t.valid_period.from_to('2020-01-01', '2020-02-01'))\n    ).delete()\n\nThis produces:\n\n.. code-block:: sql\n\n    DELETE FROM \"abc\"\n    FOR PORTION OF \"valid_period\" FROM '2020-01-01' TO '2020-02-01'\n\nCreating Tables\n^^^^^^^^^^^^^^^\n\nThe entry point for creating tables is ``pypika.Query.create_table``, which is used with the class ``pypika.Column``.\nAs with selecting data, first the table should be specified. This can be either a\nstring or a `pypika.Table`. Then the columns, and constraints. Here's an example\nthat demonstrates much of the functionality.\n\n.. code-block:: python\n\n    stmt = Query \\\n        .create_table(\"person\") \\\n        .columns(\n            Column(\"id\", \"INT\", nullable=False),\n            Column(\"first_name\", \"VARCHAR(100)\", nullable=False),\n            Column(\"last_name\", \"VARCHAR(100)\", nullable=False),\n            Column(\"phone_number\", \"VARCHAR(20)\", nullable=True),\n            Column(\"status\", \"VARCHAR(20)\", nullable=False, default=ValueWrapper(\"NEW\")),\n            Column(\"date_of_birth\", \"DATETIME\")) \\\n        .unique(\"last_name\", \"first_name\") \\\n        .primary_key(\"id\")\n\nThis produces:\n\n.. code-block:: sql\n\n    CREATE TABLE \"person\" (\n        \"id\" INT NOT NULL,\n        \"first_name\" VARCHAR(100) NOT NULL,\n        \"last_name\" VARCHAR(100) NOT NULL,\n        \"phone_number\" VARCHAR(20) NULL,\n        \"status\" VARCHAR(20) NOT NULL DEFAULT 'NEW',\n        \"date_of_birth\" DATETIME,\n        UNIQUE (\"last_name\",\"first_name\"),\n        PRIMARY KEY (\"id\")\n    )\n\nThere is also support for creating a table from a query.\n\n.. code-block:: python\n\n    stmt = Query.create_table(\"names\").as_select(\n        Query.from_(\"person\").select(\"last_name\", \"first_name\")\n    )\n\nThis produces:\n\n.. code-block:: sql\n\n        CREATE TABLE \"names\" AS (SELECT \"last_name\",\"first_name\" FROM \"person\")\n\nTEMPORARY and UNLOGGED tables can also be created:\n\n.. code-block:: python\n\n    from pypika import Query, Table, Columns\n\n    columns = Columns(('id', 'INT'), ('name', 'VARCHAR(100)'))\n\n    Query.create_table('temp_items').columns(*columns).temporary()\n    Query.create_table('fast_items').columns(*columns).unlogged()\n\n.. code-block:: sql\n\n    CREATE TEMPORARY TABLE \"temp_items\" (\"id\" INT,\"name\" VARCHAR(100))\n\n    CREATE UNLOGGED TABLE \"fast_items\" (\"id\" INT,\"name\" VARCHAR(100))\n\nManaging Table Indices\n^^^^^^^^^^^^^^^^^^^^^^\n\nCreate Indices\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\nThe entry point for creating indices is ``pypika.Query.create_index``.\nAn index name (as ``str``) or a ``pypika.terms.Index`` a table (as ``str`` or ``pypika.Table``) and\ncolumns (as ``pypika.Column``) must be specified.\n\n.. code-block:: python\n\n    my_index = Index(\"my_index\")\n    person = Table(\"person\")\n    stmt = Query \\\n        .create_index(my_index) \\\n        .on(person) \\\n        .columns(person.first_name, person.last_name)\n\nThis produces:\n\n.. code-block:: sql\n\n    CREATE INDEX my_index\n    ON person (first_name, last_name)\n\nIt is also possible to create a unique index\n\n.. code-block:: python\n\n    my_index = Index(\"my_index\")\n    person = Table(\"person\")\n    stmt = Query \\\n        .create_index(my_index) \\\n        .on(person) \\\n        .columns(person.first_name, person.last_name) \\\n        .unique()\n\nThis produces:\n\n.. code-block:: sql\n\n        CREATE UNIQUE INDEX my_index\n        ON person (first_name, last_name)\n\nIt is also possible to create an index if it does not exist\n\n.. code-block:: python\n\n    my_index = Index(\"my_index\")\n    person = Table(\"person\")\n    stmt = Query \\\n        .create_index(my_index) \\\n        .on(person) \\\n        .columns(person.first_name, person.last_name) \\\n        .if_not_exists()\n\nThis produces:\n\n.. code-block:: sql\n\n        CREATE INDEX IF NOT EXISTS my_index\n        ON person (first_name, last_name)\n\nDrop Indices\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\nThen entry point for dropping indices is ``pypika.Query.drop_index``.\nIt takes either ``str`` or ``pypika.terms.Index`` as an argument.\n\n.. code-block:: python\n\n    my_index = Index(\"my_index\")\n    stmt = Query.drop_index(my_index)\n\nThis produces:\n\n.. code-block:: sql\n\n    DROP INDEX my_index\n\nIt is also possible to drop an index if it exists\n\n.. code-block:: python\n\n    my_index = Index(\"my_index\")\n    stmt = Query.drop_index(my_index).if_exists()\n\nThis produces:\n\n.. code-block:: sql\n\n    DROP INDEX IF EXISTS my_index\n\n\nHandling Different Database Platforms\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nThere can sometimes be differences between how database vendors implement SQL in their platform, for example\nwhich quote characters are used. To ensure that the correct SQL standard is used for your platform,\nthe platform-specific Query classes can be used.\n\n.. code-block:: python\n\n    from pypika import MySQLQuery, MSSQLQuery, PostgreSQLQuery, OracleQuery, VerticaQuery, ClickHouseQuery\n\nYou can use these query classes as a drop in replacement for the default ``Query`` class shown in the other examples.\n\n\nClickHouse-Specific Features\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n|Brand| provides several ClickHouse-specific query features through the ``ClickHouseQuery`` class.\n\nFINAL\n\"\"\"\"\"\n\nThe ``FINAL`` modifier forces ClickHouse to fully merge data before returning results, useful with\nReplacingMergeTree and CollapsingMergeTree tables.\n\n.. code-block:: python\n\n    from pypika import ClickHouseQuery, Table\n\n    t = Table('events')\n    q = ClickHouseQuery.from_(t).select(t.user_id, t.event).final()\n\n.. code-block:: sql\n\n    SELECT \"user_id\",\"event\" FROM \"events\" FINAL\n\nSAMPLE\n\"\"\"\"\"\"\n\nThe ``SAMPLE`` clause enables approximate query processing on a fraction of data.\n\n.. code-block:: python\n\n    from pypika import ClickHouseQuery, Table\n\n    t = Table('events')\n    q = ClickHouseQuery.from_(t).select(t.user_id).sample(10)\n\n.. code-block:: sql\n\n    SELECT \"user_id\" FROM \"events\" SAMPLE 10\n\nYou can also specify an offset:\n\n.. code-block:: python\n\n    q = ClickHouseQuery.from_(t).select(t.user_id).sample(10, 5)\n\n.. code-block:: sql\n\n    SELECT \"user_id\" FROM \"events\" SAMPLE 10 OFFSET 5\n\nDISTINCT ON\n\"\"\"\"\"\"\"\"\"\"\"\n\nClickHouse supports ``DISTINCT ON`` to return distinct rows based on specific columns.\n\n.. code-block:: python\n\n    from pypika import ClickHouseQuery, Table\n\n    t = Table('users')\n    q = ClickHouseQuery.from_(t).distinct_on('department', t.role).select('name', 'department', 'role')\n\n.. code-block:: sql\n\n    SELECT DISTINCT ON(\"department\",\"role\") \"name\",\"department\",\"role\" FROM \"users\"\n\nLIMIT BY\n\"\"\"\"\"\"\"\"\n\nThe ``LIMIT BY`` clause limits the number of rows per group of column values.\n\n.. code-block:: python\n\n    from pypika import ClickHouseQuery, Table\n\n    t = Table('events')\n    q = ClickHouseQuery.from_(t).select('user_id', 'event', 'timestamp').limit_by(3, 'user_id')\n\n.. code-block:: sql\n\n    SELECT \"user_id\",\"event\",\"timestamp\" FROM \"events\" LIMIT 3 BY (\"user_id\")\n\nYou can also specify an offset with ``limit_offset_by``:\n\n.. code-block:: python\n\n    q = ClickHouseQuery.from_(t).select('user_id', 'event').limit_offset_by(3, 1, 'user_id')\n\n.. code-block:: sql\n\n    SELECT \"user_id\",\"event\" FROM \"events\" LIMIT 3 OFFSET 1 BY (\"user_id\")\n\n\nOracle-Specific Features\n^^^^^^^^^^^^^^^^^^^^^^^^\n\nLIMIT and OFFSET\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\nOracle queries support ``LIMIT`` and ``OFFSET`` using the ``FETCH NEXT ... ROWS ONLY`` and ``OFFSET ... ROWS`` syntax.\n\n.. code-block:: python\n\n    from pypika import OracleQuery, Table\n\n    t = Table('employees')\n    q = OracleQuery.from_(t).select(t.name).limit(10)\n\n.. code-block:: sql\n\n    SELECT name FROM employees FETCH NEXT 10 ROWS ONLY\n\nWith offset:\n\n.. code-block:: python\n\n    q = OracleQuery.from_(t).select(t.name).limit(10).offset(20)\n\n.. code-block:: sql\n\n    SELECT name FROM employees OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY\n\n\nJira Query Language (JQL)\n^^^^^^^^^^^^^^^^^^^^^^^^^\n\n|Brand| supports generating Jira Query Language expressions through the ``JiraQuery`` class.\n\n.. code-block:: python\n\n    from pypika import JiraQuery\n\n    J = JiraQuery.Table()\n    query = (\n        JiraQuery.where(J.project.isin([\"PROJ1\", \"PROJ2\"]))\n        .where(J.issuetype == \"Bug\")\n        .where(J.labels.isempty() | J.labels.notin([\"stale\", \"wontfix\"]))\n    )\n\n.. code-block:: sql\n\n    project IN (\"PROJ1\",\"PROJ2\") AND issuetype=\"Bug\" AND (labels is EMPTY OR labels NOT IN (\"stale\",\"wontfix\"))\n\nJQL fields support ``isempty()`` and ``notempty()`` methods for checking empty/non-empty values.\n\n.. _advanced_end:\n\nChaining Functions\n^^^^^^^^^^^^^^^^^^\n\nThe ``QueryBuilder.pipe`` method gives a more readable alternative while chaining functions.\n\n.. code-block:: python \n\n    # This \n    (\n        query\n        .pipe(func1, *args)\n        .pipe(func2, **kwargs)\n        .pipe(func3)\n    )\n\n    # Is equivalent to this\n    func3(func2(func1(query, *args), **kwargs))\n\nOr for a more concrete example:\n\n.. code-block:: python \n\n    from pypika import Field, Query, functions as fn\n    from pypika.queries import QueryBuilder\n\n    def filter_days(query: QueryBuilder, col, num_days: int) -\u003e QueryBuilder: \n        if isinstance(col, str): \n            col = Field(col)\n\n        return query.where(col \u003e fn.Now() - num_days)\n\n    def count_groups(query: QueryBuilder, *groups) -\u003e QueryBuilder: \n        return query.groupby(*groups).select(*groups, fn.Count(\"*\").as_(\"n_rows\"))\n\n    base_query = Query.from_(\"table\")\n\n    query = (\n        base_query\n        .pipe(filter_days, \"date\", num_days=7)\n        .pipe(count_groups, \"col1\", \"col2\")\n    )\n\nThis produces: \n\n.. code-block:: sql\n\n    SELECT \"col1\",\"col2\",COUNT(*) n_rows \n    FROM \"table\" \n    WHERE \"date\"\u003eNOW()-7 \n    GROUP BY \"col1\",\"col2\"\n\n.. _tutorial_end:\n\n.. _contributing_start: \n\nContributing\n------------\n\nWe welcome community contributions to |Brand|. Please see the `contributing guide \u003c6_contributing.html\u003e`_ to more info.\n\n.. _contributing_end:\n\n\n.. _license_start:\n\nLicense\n-------\n\nCopyright 2020 KAYAK Germany, GmbH\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n    http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n\n\nCrafted with ♥ in Berlin.\n\n.. _license_end:\n\n\n.. _appendix_start:\n\n.. |Brand| replace:: *PyPika*\n\n.. _appendix_end:\n\n.. _available_badges_start:\n\n.. |BuildStatus| image:: https://github.com/kayak/pypika/workflows/Unit%20Tests/badge.svg\n   :target: https://github.com/kayak/pypika/actions\n.. |CoverageStatus| image:: https://coveralls.io/repos/kayak/pypika/badge.svg?branch=master\n   :target: https://coveralls.io/github/kayak/pypika?branch=master\n.. |Codacy| image:: https://api.codacy.com/project/badge/Grade/6d7e44e5628b4839a23da0bd82eaafcf\n   :target: https://www.codacy.com/app/twheys/pypika\n.. |Docs| image:: https://readthedocs.org/projects/pypika/badge/?version=latest\n   :target: http://pypika.readthedocs.io/en/latest/\n.. |PyPi| image:: https://img.shields.io/pypi/v/pypika.svg?style=flat\n   :target: https://pypi.python.org/pypi/pypika\n.. |License| image:: https://img.shields.io/hexpm/l/plug.svg?maxAge=2592000\n   :target: http://www.apache.org/licenses/LICENSE-2.0\n\n.. _available_badges_end:\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkayak%2Fpypika","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkayak%2Fpypika","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkayak%2Fpypika/lists"}