WHERE 1=1 in SQL Server (Using AdventureWorks2019 Tables)

Introduction

When writing SQL queries, especially those that build dynamically based on user input, one common pattern developers use is WHERE 1=1.
At first glance, it might seem unnecessary—after all, 1=1 is always true. But this small trick plays a big role in simplifying query logic and improving code readability.

In this article, you’ll learn exactly what WHERE 1=1 does, why it’s used, and how to implement it with practical examples from the AdventureWorks2019 database.

What Does WHERE 1=1 Mean in SQL Server?

The condition 1=1 always evaluates to TRUE.
This means that when you write:

SELECT * FROM HumanResources.Employee
WHERE 1=1;

The table HumanResources.Employee has 290 records. If we use where 1=1 in the query,the result could not be affected. see the image below.

SQL Server WHERE 1=1 Example using AdventureWorks2019 tables

SQL Server interprets it as “select all rows” — because the condition is always true.

So, why bother adding it?

Because it allows developers to append multiple conditions easily when constructing queries dynamically or step-by-step without worrying about when to add WHERE or AND.

Why WHERE 1=1 Is Useful

Let’s say you want to filter employees from the AdventureWorks2019 database based on different optional parameters like department, job title, or hire date.
When building such queries dynamically, it can get messy to decide whether to start with WHERE or AND. That’s where WHERE 1=1 helps.

Benefits:

  1. Simplifies dynamic SQL generation
  2. Avoids special logic for the first condition
  3. Improves code readability
  4. Optimized automatically by SQL Server (no performance loss)

Step-by-Step Examples Using AdventureWorks2019

Example 1: Basic Query with WHERE 1=1

Let’s retrieve employees from the HumanResources.Employee table who are currently active and were hired after 2012.

SELECT BusinessEntityID, JobTitle, HireDate, CurrentFlag
FROM HumanResources.Employee
WHERE 1 = 1
AND HireDate > '2012-01-01'
AND CurrentFlag = 1;
SQL Server WHERE 1=1 Example using AdventureWorks2019 tables

Explanation:

  • The WHERE 1=1 ensures we can freely append more AND conditions later.
  • The query retrieves all active employees hired after 2012.

Example 2: Dynamic Query Building

Suppose you want to filter employees dynamically based on user-selected parameters like department name or gender.

We’ll use tables HumanResources.Employee, Person.Person, and HumanResources.Department.

DECLARE @sql NVARCHAR(MAX);
DECLARE @DepartmentName NVARCHAR(50) = 'Engineering';
DECLARE @Gender NCHAR(1) = 'M';

SET @sql = N'
SELECT e.BusinessEntityID, p.FirstName, p.LastName, e.JobTitle, d.Name AS Department
FROM HumanResources.Employee e
JOIN Person.Person p ON e.BusinessEntityID = p.BusinessEntityID
JOIN HumanResources.EmployeeDepartmentHistory edh ON e.BusinessEntityID = edh.BusinessEntityID
JOIN HumanResources.Department d ON edh.DepartmentID = d.DepartmentID
WHERE 1=1';

IF @DepartmentName IS NOT NULL
    SET @sql += N' AND d.Name = @DeptName';

IF @Gender IS NOT NULL
    SET @sql += N' AND e.Gender = @Gender';

EXEC sp_executesql 
    @sql, 
    N'@DeptName NVARCHAR(50), @Gender NCHAR(1)', 
    @DeptName = @DepartmentName, 
    @Gender = @Gender;

Step-by-step breakdown:

  1. The base query starts with WHERE 1=1.
  2. Each optional filter (department, gender) is added dynamically with AND.
  3. sp_executesql is used for parameter safety and plan reuse.
  4. You can extend this easily with additional filters like HireDate, JobTitle, etc.

Example 3: Adding Filters for Location

Let’s add address filtering using Person.Address and Person.StateProvince.

DECLARE @StateProvince NVARCHAR(50) = 'Washington';

DECLARE @sql NVARCHAR(MAX) = N'
SELECT p.FirstName, p.LastName, a.City, s.Name AS State
FROM Person.Person p
JOIN Person.BusinessEntityAddress bea ON p.BusinessEntityID = bea.BusinessEntityID
JOIN Person.Address a ON bea.AddressID = a.AddressID
JOIN Person.StateProvince s ON a.StateProvinceID = s.StateProvinceID
WHERE 1=1';

IF @StateProvince IS NOT NULL
    SET @sql += N' AND s.Name = @StateProvince';

EXEC sp_executesql 
    @sql,
    N'@StateProvince NVARCHAR(50)',
    @StateProvince = @StateProvince;

When to Use WHERE 1=1

Use it when:

  • You’re building dynamic SQL queries (especially in stored procedures or app code).
  • You need to append multiple filters based on user input.
  • You want to keep code clean and uniform when adding AND clauses.

Avoid it when:

  • Writing simple static queries (no dynamic filters).
  • You’re tempted to use it for security — it doesn’t prevent SQL injection! (Always use parameters.)

Performance Impact

SQL Server’s optimizer is smart enough to remove the redundant 1=1 during execution.
Therefore, it has no measurable performance impact on your query plan.
Focus on optimizing your actual filters, indexing, and joins instead.

Best Practices

  1. Use WHERE 1=1 only for dynamic query building.
  2. Always parameterize your dynamic SQL to avoid injection risks.
  3. Test execution plans to ensure added filters remain index-friendly.
  4. Avoid clutter — don’t use it unnecessarily in static sql scripts

Conclusion

The WHERE 1=1 technique might look trivial, but it’s a time-saving habit for developers dealing with conditional queries.
In the AdventureWorks2019 environment, it shines when building stored procedures or report queries where filters depend on user input.

It’s safe, clean, and efficient when used properly — just remember:
👉 Use it to simplify, not to secure your SQL.

Learn how WHERE 1=1 in SQL Server simplifies dynamic queries, improves readability, and helps while filtering data dynamically. Real examples from AdventureWorks2019 included.
Scroll to Top