Home

WHERE clause conditions should not be contradictory

Description

    This SQL Server code rule states that conditions in the WHERE clause of a query should not be contradictory. This means that the conditions should not be mutually exclusive or logically impossible. Contradictory conditions can lead to incorrect results and should be avoided.

    For example, if a query contains the following WHERE clause:

    WHERE age > 18 AND age < 18

    This would be considered a contradictory condition as it is logically impossible for a person to be both older than 18 and younger than 18 at the same time. Therefore, this condition should be avoided.

Key Benefits

  • Accuracy: WHERE clause conditions which are not contradictory can ensure that the query results are accurate and reliable.
  • Consistency: WHERE clause conditions should not be contradictory, which helps maintain data consistency.
  • Performance: WHERE clause conditions which are not contradictory can improve the performance of the query by reducing the number of records that need to be processed.

 

Non-compliant Code Example

SELECT Product.ProductID,  
Product.Name,
Color  
FROM Production.Product  
WHERE ListPrice > 50  AND ListPrice = 200;  --Non compliant code (Where clause condition is contradictory)

Compliant Code Example

SELECT Product.ProductID,  
Product.Name,
Color  
FROM Production.Product  
WHERE ListPrice > 50;  --Compliant code (Where clause condition is not contradictory)
Visual Expert 2024
 VETSQLRULE15