Select & Clause Builders¶
from sqlspec.builder import (
Case,
CaseBuilder,
CommonTableExpressionMixin,
HavingClauseMixin,
JoinBuilder,
LimitOffsetClauseMixin,
OrderByClauseMixin,
PivotClauseMixin,
Select,
SelectClauseMixin,
SetOperationMixin,
SubqueryBuilder,
UnpivotClauseMixin,
WhereClauseMixin,
WindowFunctionBuilder,
)
Fluent SELECT query builder supporting joins, CTEs, window functions, CASE expressions, subqueries, PIVOT/UNPIVOT, and set operations (UNION, INTERSECT, EXCEPT). See the Query Builder Guide for usage examples.
Select Builder¶
Select
¶
Bases: QueryBuilder, WhereClauseMixin, OrderByClauseMixin, LimitOffsetClauseMixin, SelectClauseMixin, JoinClauseMixin, HavingClauseMixin, SetOperationMixin, CommonTableExpressionMixin, PivotClauseMixin, UnpivotClauseMixin, ExplainMixin
Builder for SELECT queries.
Provides a fluent interface for constructing SQL SELECT statements with parameter binding and validation.
with_hint
¶
Attach an optimizer or dialect-specific hint to the query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hint
|
str
|
The raw hint string. |
required |
location
|
str
|
Where to apply the hint ('statement', 'table'). |
'statement'
|
table
|
str | None
|
Table name if the hint is for a specific table. |
None
|
dialect
|
str | None
|
Restrict the hint to a specific dialect (optional). |
None
|
Returns:
| Type | Description |
|---|---|
Self
|
The current builder instance for method chaining. |
for_update
¶
Add FOR UPDATE clause to SELECT statement for row-level locking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
skip_locked
|
bool
|
Skip rows that are already locked (SKIP LOCKED) |
False
|
nowait
|
bool
|
Return immediately if row is locked (NOWAIT) |
False
|
of
|
str | list[str] | None
|
Table names/aliases to lock (FOR UPDATE OF table) |
None
|
Returns:
| Type | Description |
|---|---|
Self
|
Self for method chaining |
for_share
¶
Add FOR SHARE clause for shared row-level locking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
skip_locked
|
bool
|
Skip rows that are already locked (SKIP LOCKED) |
False
|
nowait
|
bool
|
Return immediately if row is locked (NOWAIT) |
False
|
of
|
str | list[str] | None
|
Table names/aliases to lock (FOR SHARE OF table) |
None
|
Returns:
| Type | Description |
|---|---|
Self
|
Self for method chaining |
for_key_share
¶
Add FOR KEY SHARE clause (PostgreSQL-specific).
FOR KEY SHARE is like FOR SHARE, but the lock is weaker: SELECT FOR UPDATE is blocked, but not SELECT FOR NO KEY UPDATE.
Returns:
| Type | Description |
|---|---|
Self
|
Self for method chaining |
Join, Window, Case & Subquery Builders¶
JoinBuilder
¶
Builder for JOIN operations with fluent syntax.
on
¶
Set the join condition and build the JOIN expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
condition
|
str | Expr
|
JOIN condition |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
Complete JOIN expression |
WindowFunctionBuilder
¶
CaseBuilder
¶
Fluent builder for CASE expressions used within SELECT clauses.
Case
¶
SubqueryBuilder
¶
Helper to build subquery expressions for EXISTS/IN/ANY/ALL operations.
Clause Mixins¶
SelectClauseMixin
¶
Mixin providing SELECT clause methods.
WhereClauseMixin
¶
OrderByClauseMixin
¶
LimitOffsetClauseMixin
¶
HavingClauseMixin
¶
CommonTableExpressionMixin
¶
Mixin for Common Table Expression (CTE) support.
CTE functionality is provided directly by :class:~sqlspec.builder._base.QueryBuilder.
This class is retained for backward compatibility.