Return a subquery of this :class:`_expression.SelectBase`. A subquery is from a SQL perspective a parenthesized, named construct that can be placed in the FROM clause of another SELECT statement. Given a SELECT statement such as:: stmt = select(table.c.
(self, name: Optional[str] = None)
| 3820 | return Lateral._factory(self, name) |
| 3821 | |
| 3822 | def subquery(self, name: Optional[str] = None) -> Subquery: |
| 3823 | """Return a subquery of this :class:`_expression.SelectBase`. |
| 3824 | |
| 3825 | A subquery is from a SQL perspective a parenthesized, named |
| 3826 | construct that can be placed in the FROM clause of another |
| 3827 | SELECT statement. |
| 3828 | |
| 3829 | Given a SELECT statement such as:: |
| 3830 | |
| 3831 | stmt = select(table.c.id, table.c.name) |
| 3832 | |
| 3833 | The above statement might look like: |
| 3834 | |
| 3835 | .. sourcecode:: sql |
| 3836 | |
| 3837 | SELECT table.id, table.name FROM table |
| 3838 | |
| 3839 | The subquery form by itself renders the same way, however when |
| 3840 | embedded into the FROM clause of another SELECT statement, it becomes |
| 3841 | a named sub-element:: |
| 3842 | |
| 3843 | subq = stmt.subquery() |
| 3844 | new_stmt = select(subq) |
| 3845 | |
| 3846 | The above renders as: |
| 3847 | |
| 3848 | .. sourcecode:: sql |
| 3849 | |
| 3850 | SELECT anon_1.id, anon_1.name |
| 3851 | FROM (SELECT table.id, table.name FROM table) AS anon_1 |
| 3852 | |
| 3853 | Historically, :meth:`_expression.SelectBase.subquery` |
| 3854 | is equivalent to calling |
| 3855 | the :meth:`_expression.FromClause.alias` |
| 3856 | method on a FROM object; however, |
| 3857 | as a :class:`_expression.SelectBase` |
| 3858 | object is not directly FROM object, |
| 3859 | the :meth:`_expression.SelectBase.subquery` |
| 3860 | method provides clearer semantics. |
| 3861 | |
| 3862 | .. versionadded:: 1.4 |
| 3863 | |
| 3864 | """ |
| 3865 | |
| 3866 | return Subquery._construct( |
| 3867 | self._ensure_disambiguated_names(), name=name |
| 3868 | ) |
| 3869 | |
| 3870 | @util.preload_module("sqlalchemy.sql.ddl") |
| 3871 | def into( |
no test coverage detected