# SQLAlchemy 2.0 Documentation

# SQLAlchemy 2.0 Documentation

# ORM Querying Guide

This section provides an overview of emitting queries with the
SQLAlchemy ORM using [2.0 style](https://docs.sqlalchemy.org/en/20/glossary.html#term-2.0-style) usage.

Readers of this section should be familiar with the SQLAlchemy overview
at [SQLAlchemy Unified Tutorial](https://docs.sqlalchemy.org/en/20/tutorial/index.html#unified-tutorial), and in particular most of the content here expands
upon the content at [Using SELECT Statements](https://docs.sqlalchemy.org/en/20/tutorial/data_select.html#tutorial-selecting-data).

For users of SQLAlchemy 1.x

In the SQLAlchemy 2.x series, SQL SELECT statements for the ORM are
constructed using the same [select()](https://docs.sqlalchemy.org/en/20/core/selectable.html#sqlalchemy.sql.expression.select) construct as is used in
Core, which is then invoked in terms of a [Session](https://docs.sqlalchemy.org/en/20/orm/session_api.html#sqlalchemy.orm.Session) using the
[Session.execute()](https://docs.sqlalchemy.org/en/20/orm/session_api.html#sqlalchemy.orm.Session.execute) method (as are the [update()](https://docs.sqlalchemy.org/en/20/core/dml.html#sqlalchemy.sql.expression.update) and
[delete()](https://docs.sqlalchemy.org/en/20/core/dml.html#sqlalchemy.sql.expression.delete) constructs now used for the
[ORM-Enabled INSERT, UPDATE, and DELETE statements](https://docs.sqlalchemy.org/en/20/orm/queryguide/dml.html#orm-expression-update-delete) feature). However, the legacy
[Query](https://docs.sqlalchemy.org/en/20/orm/queryguide/query.html#sqlalchemy.orm.Query) object, which performs these same steps as more of an
“all-in-one” object, continues to remain available as a thin facade over
this new system, to support applications that were built on the 1.x series
without the need for wholesale replacement of all queries. For reference on
this object, see the section [Legacy Query API](https://docs.sqlalchemy.org/en/20/orm/queryguide/query.html).

- [Writing SELECT statements for ORM Mapped Classes](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html)
  - [Selecting ORM Entities and Attributes](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#selecting-orm-entities-and-attributes)
    - [Selecting ORM Entities](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#selecting-orm-entities)
    - [Selecting Multiple ORM Entities Simultaneously](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#selecting-multiple-orm-entities-simultaneously)
    - [Selecting Individual Attributes](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#selecting-individual-attributes)
    - [Grouping Selected Attributes with Bundles](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#grouping-selected-attributes-with-bundles)
    - [Selecting ORM Aliases](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#selecting-orm-aliases)
    - [Getting ORM Results from Textual Statements](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#getting-orm-results-from-textual-statements)
    - [Selecting Entities from Subqueries](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#selecting-entities-from-subqueries)
    - [Selecting Entities from UNIONs and other set operations](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#selecting-entities-from-unions-and-other-set-operations)
  - [Joins](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#joins)
    - [Simple Relationship Joins](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#simple-relationship-joins)
    - [Chaining Multiple Joins](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#chaining-multiple-joins)
    - [Joins to a Target Entity](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#joins-to-a-target-entity)
    - [Joins to a Target with an ON Clause](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#joins-to-a-target-with-an-on-clause)
    - [Combining Relationship with Custom ON Criteria](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#combining-relationship-with-custom-on-criteria)
    - [Using Relationship to join between aliased targets](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#using-relationship-to-join-between-aliased-targets)
    - [Joining to Subqueries](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#joining-to-subqueries)
    - [Joining to Subqueries along Relationship paths](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#joining-to-subqueries-along-relationship-paths)
    - [Subqueries that Refer to Multiple Entities](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#subqueries-that-refer-to-multiple-entities)
    - [Setting the leftmost FROM clause in a join](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#setting-the-leftmost-from-clause-in-a-join)
  - [Relationship WHERE Operators](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#relationship-where-operators)
    - [EXISTS forms: has() / any()](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#exists-forms-has-any)
    - [Relationship Instance Comparison Operators](https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#relationship-instance-comparison-operators)
- [Writing SELECT statements for Inheritance Mappings](https://docs.sqlalchemy.org/en/20/orm/queryguide/inheritance.html)
  - [SELECTing from the base class vs. specific sub-classes](https://docs.sqlalchemy.org/en/20/orm/queryguide/inheritance.html#selecting-from-the-base-class-vs-specific-sub-classes)
  - [Using selectin_polymorphic()](https://docs.sqlalchemy.org/en/20/orm/queryguide/inheritance.html#using-selectin-polymorphic)
    - [Applying selectin_polymorphic() to an existing eager load](https://docs.sqlalchemy.org/en/20/orm/queryguide/inheritance.html#applying-selectin-polymorphic-to-an-existing-eager-load)
    - [Applying loader options to the subclasses loaded by selectin_polymorphic](https://docs.sqlalchemy.org/en/20/orm/queryguide/inheritance.html#applying-loader-options-to-the-subclasses-loaded-by-selectin-polymorphic)
    - [Configuring selectin_polymorphic() on mappers](https://docs.sqlalchemy.org/en/20/orm/queryguide/inheritance.html#configuring-selectin-polymorphic-on-mappers)
  - [Using with_polymorphic()](https://docs.sqlalchemy.org/en/20/orm/queryguide/inheritance.html#using-with-polymorphic)
    - [Filtering Subclass Attributes with with_polymorphic()](https://docs.sqlalchemy.org/en/20/orm/queryguide/inheritance.html#filtering-subclass-attributes-with-with-polymorphic)
    - [Using aliasing with with_polymorphic](https://docs.sqlalchemy.org/en/20/orm/queryguide/inheritance.html#using-aliasing-with-with-polymorphic)
    - [Configuring with_polymorphic() on mappers](https://docs.sqlalchemy.org/en/20/orm/queryguide/inheritance.html#configuring-with-polymorphic-on-mappers)
  - [Joining to specific sub-types or with_polymorphic() entities](https://docs.sqlalchemy.org/en/20/orm/queryguide/inheritance.html#joining-to-specific-sub-types-or-with-polymorphic-entities)
    - [Eager Loading of Polymorphic Subtypes](https://docs.sqlalchemy.org/en/20/orm/queryguide/inheritance.html#eager-loading-of-polymorphic-subtypes)
  - [SELECT Statements for Single Inheritance Mappings](https://docs.sqlalchemy.org/en/20/orm/queryguide/inheritance.html#select-statements-for-single-inheritance-mappings)
    - [Optimizing Attribute Loads for Single Inheritance](https://docs.sqlalchemy.org/en/20/orm/queryguide/inheritance.html#optimizing-attribute-loads-for-single-inheritance)
  - [Inheritance Loading API](https://docs.sqlalchemy.org/en/20/orm/queryguide/inheritance.html#inheritance-loading-api)
    - [with_polymorphic()](https://docs.sqlalchemy.org/en/20/orm/queryguide/inheritance.html#sqlalchemy.orm.with_polymorphic)
    - [selectin_polymorphic()](https://docs.sqlalchemy.org/en/20/orm/queryguide/inheritance.html#sqlalchemy.orm.selectin_polymorphic)
- [ORM-Enabled INSERT, UPDATE, and DELETE statements](https://docs.sqlalchemy.org/en/20/orm/queryguide/dml.html)
  - [ORM Bulk INSERT Statements](https://docs.sqlalchemy.org/en/20/orm/queryguide/dml.html#orm-bulk-insert-statements)
    - [Getting new objects with RETURNING](https://docs.sqlalchemy.org/en/20/orm/queryguide/dml.html#getting-new-objects-with-returning)
    - [Using Heterogeneous Parameter Dictionaries](https://docs.sqlalchemy.org/en/20/orm/queryguide/dml.html#using-heterogeneous-parameter-dictionaries)
    - [Sending NULL values in ORM bulk INSERT statements](https://docs.sqlalchemy.org/en/20/orm/queryguide/dml.html#sending-null-values-in-orm-bulk-insert-statements)
    - [Bulk INSERT for Joined Table Inheritance](https://docs.sqlalchemy.org/en/20/orm/queryguide/dml.html#bulk-insert-for-joined-table-inheritance)
    - [ORM Bulk Insert with SQL Expressions](https://docs.sqlalchemy.org/en/20/orm/queryguide/dml.html#orm-bulk-insert-with-sql-expressions)
    - [Legacy Session Bulk INSERT Methods](https://docs.sqlalchemy.org/en/20/orm/queryguide/dml.html#legacy-session-bulk-insert-methods)
    - [ORM “upsert” Statements](https://docs.sqlalchemy.org/en/20/orm/queryguide/dml.html#orm-upsert-statements)
  - [ORM Bulk UPDATE by Primary Key](https://docs.sqlalchemy.org/en/20/orm/queryguide/dml.html#orm-bulk-update-by-primary-key)
    - [Disabling Bulk ORM Update by Primary Key for an UPDATE statement with multiple parameter sets](https://docs.sqlalchemy.org/en/20/orm/queryguide/dml.html#disabling-bulk-orm-update-by-primary-key-for-an-update-statement-with-multiple-parameter-sets)
    - [Bulk UPDATE by Primary Key for Joined Table Inheritance](https://docs.sqlalchemy.org/en/20/orm/queryguide/dml.html#bulk-update-by-primary-key-for-joined-table-inheritance)
    - [Legacy Session Bulk UPDATE Methods](https://docs.sqlalchemy.org/en/20/orm/queryguide/dml.html#legacy-session-bulk-update-methods)
  - [ORM UPDATE and DELETE with Custom WHERE Criteria](https://docs.sqlalchemy.org/en/20/orm/queryguide/dml.html#orm-update-and-delete-with-custom-where-criteria)
    - [Important Notes and Caveats for ORM-Enabled Update and Delete](https://docs.sqlalchemy.org/en/20/orm/queryguide/dml.html#important-notes-and-caveats-for-orm-enabled-update-and-delete)
    - [Selecting a Synchronization Strategy](https://docs.sqlalchemy.org/en/20/orm/queryguide/dml.html#selecting-a-synchronization-strategy)
    - [Using RETURNING with UPDATE/DELETE and Custom WHERE Criteria](https://docs.sqlalchemy.org/en/20/orm/queryguide/dml.html#using-returning-with-update-delete-and-custom-where-criteria)
    - [UPDATE/DELETE with Custom WHERE Criteria for Joined Table Inheritance](https://docs.sqlalchemy.org/en/20/orm/queryguide/dml.html#update-delete-with-custom-where-criteria-for-joined-table-inheritance)
    - [Legacy Query Methods](https://docs.sqlalchemy.org/en/20/orm/queryguide/dml.html#legacy-query-methods)
- [Column Loading Options](https://docs.sqlalchemy.org/en/20/orm/queryguide/columns.html)
  - [Limiting which Columns Load with Column Deferral](https://docs.sqlalchemy.org/en/20/orm/queryguide/columns.html#limiting-which-columns-load-with-column-deferral)
    - [Usingload_only()to reduce loaded columns](https://docs.sqlalchemy.org/en/20/orm/queryguide/columns.html#using-load-only-to-reduce-loaded-columns)
    - [Usingdefer()to omit specific columns](https://docs.sqlalchemy.org/en/20/orm/queryguide/columns.html#using-defer-to-omit-specific-columns)
    - [Using raiseload to prevent deferred column loads](https://docs.sqlalchemy.org/en/20/orm/queryguide/columns.html#using-raiseload-to-prevent-deferred-column-loads)
  - [Configuring Column Deferral on Mappings](https://docs.sqlalchemy.org/en/20/orm/queryguide/columns.html#configuring-column-deferral-on-mappings)
    - [Usingdeferred()for imperative mappers, mapped SQL expressions](https://docs.sqlalchemy.org/en/20/orm/queryguide/columns.html#using-deferred-for-imperative-mappers-mapped-sql-expressions)
    - [Usingundefer()to “eagerly” load deferred columns](https://docs.sqlalchemy.org/en/20/orm/queryguide/columns.html#using-undefer-to-eagerly-load-deferred-columns)
    - [Loading deferred columns in groups](https://docs.sqlalchemy.org/en/20/orm/queryguide/columns.html#loading-deferred-columns-in-groups)
    - [Undeferring by group withundefer_group()](https://docs.sqlalchemy.org/en/20/orm/queryguide/columns.html#undeferring-by-group-with-undefer-group)
    - [Undeferring on wildcards](https://docs.sqlalchemy.org/en/20/orm/queryguide/columns.html#undeferring-on-wildcards)
    - [Configuring mapper-level “raiseload” behavior](https://docs.sqlalchemy.org/en/20/orm/queryguide/columns.html#configuring-mapper-level-raiseload-behavior)
  - [Loading Arbitrary SQL Expressions onto Objects](https://docs.sqlalchemy.org/en/20/orm/queryguide/columns.html#loading-arbitrary-sql-expressions-onto-objects)
    - [Usingwith_expression()with UNIONs, other subqueries](https://docs.sqlalchemy.org/en/20/orm/queryguide/columns.html#using-with-expression-with-unions-other-subqueries)
  - [Column Loading API](https://docs.sqlalchemy.org/en/20/orm/queryguide/columns.html#column-loading-api)
    - [defer()](https://docs.sqlalchemy.org/en/20/orm/queryguide/columns.html#sqlalchemy.orm.defer)
    - [deferred()](https://docs.sqlalchemy.org/en/20/orm/queryguide/columns.html#sqlalchemy.orm.deferred)
    - [query_expression()](https://docs.sqlalchemy.org/en/20/orm/queryguide/columns.html#sqlalchemy.orm.query_expression)
    - [load_only()](https://docs.sqlalchemy.org/en/20/orm/queryguide/columns.html#sqlalchemy.orm.load_only)
    - [undefer()](https://docs.sqlalchemy.org/en/20/orm/queryguide/columns.html#sqlalchemy.orm.undefer)
    - [undefer_group()](https://docs.sqlalchemy.org/en/20/orm/queryguide/columns.html#sqlalchemy.orm.undefer_group)
    - [with_expression()](https://docs.sqlalchemy.org/en/20/orm/queryguide/columns.html#sqlalchemy.orm.with_expression)
- [Relationship Loading Techniques](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html)
  - [Summary of Relationship Loading Styles](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#summary-of-relationship-loading-styles)
  - [Configuring Loader Strategies at Mapping Time](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#configuring-loader-strategies-at-mapping-time)
  - [Relationship Loading with Loader Options](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#relationship-loading-with-loader-options)
    - [Adding Criteria to loader options](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#adding-criteria-to-loader-options)
    - [Specifying Sub-Options with Load.options()](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#specifying-sub-options-with-load-options)
  - [Lazy Loading](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#lazy-loading)
    - [Preventing unwanted lazy loads using raiseload](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#preventing-unwanted-lazy-loads-using-raiseload)
  - [Joined Eager Loading](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#joined-eager-loading)
    - [The Zen of Joined Eager Loading](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#the-zen-of-joined-eager-loading)
  - [Select IN loading](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#select-in-loading)
  - [Subquery Eager Loading](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#subquery-eager-loading)
  - [What Kind of Loading to Use ?](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#what-kind-of-loading-to-use)
  - [Polymorphic Eager Loading](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#polymorphic-eager-loading)
  - [Wildcard Loading Strategies](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#wildcard-loading-strategies)
    - [Per-Entity Wildcard Loading Strategies](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#per-entity-wildcard-loading-strategies)
  - [Routing Explicit Joins/Statements into Eagerly Loaded Collections](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#routing-explicit-joins-statements-into-eagerly-loaded-collections)
    - [Using contains_eager() to load a custom-filtered collection result](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#using-contains-eager-to-load-a-custom-filtered-collection-result)
  - [Relationship Loader API](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#relationship-loader-api)
    - [contains_eager()](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#sqlalchemy.orm.contains_eager)
    - [defaultload()](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#sqlalchemy.orm.defaultload)
    - [immediateload()](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#sqlalchemy.orm.immediateload)
    - [joinedload()](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#sqlalchemy.orm.joinedload)
    - [lazyload()](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#sqlalchemy.orm.lazyload)
    - [Load](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#sqlalchemy.orm.Load)
    - [noload()](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#sqlalchemy.orm.noload)
    - [raiseload()](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#sqlalchemy.orm.raiseload)
    - [selectinload()](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#sqlalchemy.orm.selectinload)
    - [subqueryload()](https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#sqlalchemy.orm.subqueryload)
- [ORM API Features for Querying](https://docs.sqlalchemy.org/en/20/orm/queryguide/api.html)
  - [ORM Loader Options](https://docs.sqlalchemy.org/en/20/orm/queryguide/api.html#orm-loader-options)
  - [ORM Execution Options](https://docs.sqlalchemy.org/en/20/orm/queryguide/api.html#orm-execution-options)
    - [Populate Existing](https://docs.sqlalchemy.org/en/20/orm/queryguide/api.html#populate-existing)
    - [Autoflush](https://docs.sqlalchemy.org/en/20/orm/queryguide/api.html#autoflush)
    - [Fetching Large Result Sets with Yield Per](https://docs.sqlalchemy.org/en/20/orm/queryguide/api.html#fetching-large-result-sets-with-yield-per)
    - [Identity Token](https://docs.sqlalchemy.org/en/20/orm/queryguide/api.html#queryguide-identity-token)
- [Legacy Query API](https://docs.sqlalchemy.org/en/20/orm/queryguide/query.html)
  - [The Query Object](https://docs.sqlalchemy.org/en/20/orm/queryguide/query.html#the-query-object)
    - [Query](https://docs.sqlalchemy.org/en/20/orm/queryguide/query.html#sqlalchemy.orm.Query)
  - [ORM-Specific Query Constructs](https://docs.sqlalchemy.org/en/20/orm/queryguide/query.html#orm-specific-query-constructs)
