Skip to content

SA0039 : The comparison expression evaluates to FALSE

Misconfigured comparison expressions that consistently evaluate to FALSE in SQL queries can uncover logical errors in a SQL query.

This problem occurs when SQL queries contain comparison expressions that are logically incorrect, causing them to always return FALSE . Such expressions can lead to inefficient queries, unnecessary resource consumption, and incorrect query results.

For example:

-- Example of a problematic query
SELECT * FROM Employees WHERE EmployeeStatus > EmployeeStatus;

In this example, the condition EmployeeStatus > EmployeeStatus will never be true, resulting in an empty result set and wasted execution resources. Misconfigured comparisons like this are common pitfalls that can occur due to logical errors or incorrect assumptions about data.

  • Queries with such conditions are inefficient because they always return the same static result.

  • Logical mistakes in query design can lead to data integrity issues and incorrect business insights.

Review and correct logically misconfigured comparison expressions in SQL queries to ensure they don’t always evaluate to FALSE, thereby improving query efficiency and accuracy.

Follow these steps to address the issue:

1.Identify the problematic comparison expression. Look for conditions in your SQL queries that always evaluate to FALSE, such as 1 = 0 or 'A' = 'B' , or A != A .

2.Determine the logical intent of the condition. Understand what the condition was meant to achieve by reviewing the business logic or data requirements.

3.Revise the condition to correctly reflect the intended logic. Ensure that the expression can evaluate to TRUE under the correct circumstances.

For example:

-- Corrected example of a query
SELECT * FROM Employees WHERE EmployeeStatus = 'Active';

The rule has a Batch scope and is applied only on the SQL script.

Rule has no parameters.

The rule does not need Analysis Context or SQL Connection.

3 minutes per issue.

Design Rules, Code Smells

There is no additional info for this rule.

-- Comparison will always evaluate to FALSE.
SELECT *
FROM sys.objects AS o
WHERE 1 != 1 OR
1 > 1 OR
0 > 1 OR
1 = 0 OR
0 != 0 OR
2 != 2 OR
2 <> 2 OR
2 < 1 OR
2 <= 1 OR
2 <> 2.0 OR
2.0 < 1.0 OR
2 <= 1 OR
1 >= 2 OR
object_id != [object_id] OR
o.object_id != o.[object_id] OR
'A' < 'A' OR
'A' != 'A' OR
'B' <> 'B' OR
'B' <> N'B' OR
'ab' != 'ab' OR
'ab' != N'ab' OR
'ab' < N'a' OR
'ab' < N'a' OR
'ab' = N'a' OR
'ab' < 'a' OR
'ab' < 'a' OR
'ab' = 'a' OR
name != name OR
name <> name OR
name < name OR
o.[name] > o.name OR
name NOT LIKE name OR
1 IN( 2, 4, 3 ) OR
'a' IN( 'b', 'c', 'd', 6 ) OR
1 NOT IN( 1, 2, 4, 3, 2 - 1 ) OR
1 IN( 2, 4, 3 ) OR
'a' NOT IN( 'b', 'c', 'd', 'a' ) OR
NOT EXISTS( SELECT 0 )
  Message Line Column
1 SA0039 : The comparison expression evaluates to FALSE. 35 16
2 SA0039 : The comparison expression evaluates to FALSE. 36 9
3 SA0039 : The comparison expression evaluates to FALSE. 37 11
4 SA0039 : The comparison expression evaluates to FALSE. 38 13
5 SA0039 : The comparison expression evaluates to FALSE. 39 9
6 SA0039 : The comparison expression evaluates to FALSE. 40 15
7 SA0039 : The comparison expression evaluates to FALSE. 41 11
8 SA0039 : The comparison expression evaluates to FALSE. 4 9
9 SA0039 : The comparison expression evaluates to FALSE. 5 9
10 SA0039 : The comparison expression evaluates to FALSE. 6 9
…
37 SA0039 : The comparison expression evaluates to FALSE. 33 12
38 SA0039 : The comparison expression evaluates to FALSE. 34 16

Analysis Rules