SQL functions are not uniformly compatible with DDL and DML: most scalar functions work in both DDL (schema definition, defaults, constraints, generated columns, indexes) and DML (SELECT, INSERT, UPDATE, DELETE), but the specific function, its volatility classification, and the DBMS determine whether it can legally appear in a given DDL or DML context, and whether it can appear in an index, constraint, or generated column at all.
What the Question Is Really Asking
"SQL functions compatibility with DDL and DML" is ambiguous in three ways, and those ambiguities drive different answers:
1: Which functions? Built-in scalar functions (e.g., UPPER, COALESCE, CURRENT_DATE), aggregate functions (SUM, COUNT), window functions (ROW_NUMBER), table-valued functions, and user-defined functions (UDFs) all behave differently. 2: Which DDL/DML contexts? A function might be usable in a CHECK constraint but not in a partitioning expression, or in a DEFAULT but not in a WHERE clause of a view with CHECK OPTION. 3: Which DBMS? PostgreSQL, MySQL, SQL Server, Oracle, and SQLite differ substantially on what is allowed where, especially around volatility and determinism requirements.
The question cannot be answered as a single yes/no. The general principle is answerable; the product-specific details must be checked against the engine and version in use, and no version-specific claims are made here as verified.
The Core Distinction: Volatility and Determinism
The main reason a function is or is not allowed in a DDL construct is volatility — whether the function's result is stable for the same inputs across repeated evaluation.
Immutable / deterministic: the function returns the same result for the same arguments forever (e.g., ABS, UPPER, LENGTH). These are usually safe in indexes, generated columns, and constraints. Stable / deterministic-per-statement: the result is fixed within a single statement but may change between statements (e.g., CURRENT_DATE, NOW() in some contexts). These often work in DML but are typically rejected in index expressions. Volatile / non-deterministic: the result can change between rows or calls (e.g., RANDOM(), NEWID(), sequences). These are usually banned from index keys, generated columns, and CHECK constraints.
The reason is mechanical: an index stores a value computed once, so a function that can produce different results on the same row would make the index inconsistent with the base table. A CHECK constraint must be re-evaluable and stable, or the row could violate the constraint without any change to stored data. A generated column must be a deterministic function of its inputs to be reproducible on rebuild.
This is a principle, not a rule with universal constants. SQL Server uses the term "deterministic" and enforces it more strictly in indexed views and computed columns; PostgreSQL uses IMMUTABLE, STABLE, and VOLATILE markers; MySQL's rules around generated columns and functional indexes vary by version and storage engine. The distinction is real; the thresholds differ.
Built-in Scalar Functions in DML
Scalar functions are the most portable category. UPPER, LOWER, TRIM, SUBSTRING, COALESCE, NULLIF, CAST, ROUND, ABS, date arithmetic, and string concatenation generally work in SELECT, WHERE, JOIN, GROUP BY, HAVING, and ORDER BY across major engines, with syntax differences (e.g., || versus CONCAT, CAST versus ::).
Example in DML:
Here UPPER is immutable and CURRENT_DATE is stable-per-statement; both are valid in SELECT and WHERE. This is the ordinary case and rarely causes problems.
Built-in Scalar Functions in DDL
The same functions appear in DDL in three main places:
DEFAULT clauses: DEFAULT CURRENT_TIMESTAMP, DEFAULT UPPER('x'). Immutable and many stable functions are accepted; volatile ones usually are not. CHECK constraints: CHECK (LENGTH(code) = 8), CHECK (price > 0). Immutable functions are generally fine; volatile ones are usually rejected or create enforcement problems. Generated columns: GENERATED ALWAYS AS (price * quantity) STORED. The expression must be deterministic in engines that enforce this. Index expressions / functional indexes: CREATE INDEX idx ON t (LOWER(email)). Requires an immutable function in PostgreSQL; requires deterministic, schema-bound functions in SQL Server; MySQL's functional indexes have their own constraints.
Example in DDL:
CURRENT_TIMESTAMP in a DEFAULT is broadly accepted. A CHECK on created_at > CURRENT_DATE would be unusual and often rejected or semantically fragile because it would make old rows invalid over time — a case where the function is valid but the constraint is wrong.
Aggregate and Window Functions
Aggregate functions (SUM, COUNT, AVG, MIN, MAX) are not valid in DDL contexts such as DEFAULT, CHECK, generated columns, or index expressions, because they require a set of rows and DDL constructs operate per row or per definition.
In DML, aggregates are valid in SELECT with GROUP BY, in HAVING, and in subqueries. Window functions (ROW_NUMBER, RANK, SUM() OVER (...)) are valid only in SELECT and ORDER BY (and in some engines in QUALIFY), not in WHERE, GROUP BY, or HAVING directly — the filter must be applied in an outer query or CTE. This is a DML-only constraint and is generally consistent across engines, though QUALIFY support varies.
User-Defined Functions: The Real Compatibility Frontier
UDFs are where compatibility breaks down most sharply.
Scalar UDFs can often be used in SELECT and WHERE, but whether they can be used in a CHECK, generated column, or index expression depends on the engine's determinism enforcement and on whether the function is schema-bound or marked immutable. Table-valued functions are used in the FROM clause; they are not DDL constructs and have no place in DEFAULT, CHECK, or index expressions. Stored procedures are not functions and are called separately (e.g., CALL, EXEC); they are generally not usable as expressions inside DML or DDL.
In PostgreSQL, a function marked IMMUTABLE can be used in an index expression; one marked STABLE or VOLATILE cannot. In SQL Server, a computed column or indexed view referencing a UDF requires the function to be deterministic and, for indexed views, schema-bound with specific SET options. In MySQL, stored functions in generated columns and functional indexes have version-dependent restrictions. These are the practical boundaries the question is really about.
Where Confusion Comes From
Three misunderstandings recur:
1: "Functions work in SQL, so they work in DDL." False. DML tolerance is broad; DDL tolerance is narrow and tied to determinism and per-row evaluability. 2: "A function that works in a CHECK will work in an index." Not necessarily. A CHECK may be evaluated at write time; an index expression must be stable across rebuilds and lookups, which is a stricter requirement. 3: "Volatility labels are portable." They are not. IMMUTABLE in PostgreSQL is not the same as DETERMINISTIC in MySQL, and SQL Server's determinism rules are enforced differently in computed columns versus indexed views.
Practical Guide
| Context | Scalar built-in | Aggregate | Window | Scalar UDF |
|---|---|---|---|---|
| SELECT / WHERE (DML) | Yes | In SELECT with GROUP BY | SELECT only | Usually yes |
| DEFAULT (DDL) | Often (stable/immutable) | No | No | Engine-dependent |
| CHECK (DDL) | Usually immutable | No | No | Engine-dependent |
| Generated column (DDL) | Deterministic only | No | No | Deterministic only |
| Index expression (DDL) | Immutable only | No | No | Immutable/deterministic only |
This table summarizes the principle; the "engine-dependent" cells must be verified against the specific DBMS and version, which the question does not name.
What Is Answerable and What Is Not
Answerable: the general rule that DML is permissive and DDL is restrictive, that the restriction is driven by determinism and per-row evaluability, and that aggregates and window functions are DML-only in expression contexts.
Not answerable without more information: whether a specific function is legal in a specific DDL construct in a specific engine version. The question does not name a DBMS, a version, or a function, so any claim of the form "function X works in construct Y" would be an unsupported generalization.