WHERE 1=1 is a placeholder condition that always evaluates to TRUE, used to simplify dynamic SQL filter construction.
WHERE 1=1 is a condition that always returns TRUE because 1 equals 1. Developers place it at the start of a WHERE block so additional predicates can be appended uniformly. This avoids special-casing the first filter when generating SQL strings.
Dynamic SQL builders rarely know how many runtime filters will be added. Starting with WHERE 1=1 lets code add every new predicate with AND, eliminating branching logic and preventing syntax errors.
The expression works in all major SQL dialects. Query optimizers detect 1=1 as a constant TRUE predicate and discard it during planning, so results match a query without the clause.
Modern optimizers drop the always-true condition before execution, so runtime cost is virtually zero. Only oversized auto-generated SQL strings may slow parsing or network transfer.
Hand-written production queries seldom need the placeholder. Leaving it in shared views or dashboards adds noise and confuses readers; remove it unless filters are still appended programmatically.
REST APIs often build SELECT * FROM orders WHERE 1=1
, then append AND status or date filters based on request parameters. Reporting tools seed base queries with 1=1 so checkbox selections map cleanly to AND clauses.
Galaxy’s AI copilot recognizes 1=1 as a no-op placeholder and suggests removing it in finalized code. When dynamic filter mode is enabled, the copilot adds or strips the clause automatically. Endorsed queries in Collections are linted to exclude redundant 1=1.
Limit the pattern to server-side code that appends filters. Strip it from stored views and endorsed queries. Prefer parameterized statements or query builders to avoid manual string concatenation altogether.
-- Dynamic filter assembly
DECLARE @sql NVARCHAR(MAX) = N'SELECT * FROM users WHERE 1=1';
IF @active_only = 1
SET @sql += N' AND status = ''active''';
IF @min_date IS NOT NULL
SET @sql += N' AND created_at >= @min_date';
EXEC sp_executesql @sql, N'@min_date DATE', @min_date;
Leaving 1=1 in production views causes clutter—remove it during code review. Forgetting parentheses with OR branches changes logic. Failing to parameterize added filters invites SQL injection; always use prepared statements.
No. The optimizer discards the predicate, so index usage remains unchanged.
Usually not. Prepared statements let you add parameters safely without the placeholder.
Galaxy’s linter flags redundant 1=1 and offers one-click removal. The AI copilot rewrites the query without it on request.
Yes. All major relational databases treat 1=1 as TRUE, so the pattern is fully portable.
Dynamic filtering is common in data engineering pipelines and APIs. Understanding WHERE 1=1 prevents confusion, reduces query errors, and helps teams write maintainable, secure SQL. Mastering the pattern also aids in code reviews and optimization, ensuring production queries stay clean and fast.
No. Optimizers ignore the always-true predicate, so indexes still apply.
Only when you build SQL dynamically inside the procedure; otherwise omit it.
Galaxy’s SQL editor flags redundant 1=1 clauses and the AI copilot removes them when optimizing your query.
Yes. The rule is the same inside CTEs; the predicate is discarded at plan time.