Comparison and logical operators: build precise conditions

A condition is an expression whose result decides whether a row satisfies a rule. It can be syntactically valid and still select different rows from those we intended. Comparison operators evaluate values; AND, OR, and NOT combine or negate those evaluations.

Clarity matters especially when a condition mixes several business rules.

Main operators

Equality and difference

= checks whether two expressions are equal. <> checks whether they are different; SQL Server also supports !=, although <> is the standard SQL form.

Expresion = Valor
Expresion <> Valor

Ordering and boundaries

> and < compare whether one value is greater than or less than another. >= and <= also include the boundary value.

Expresion > Limite
Expresion >= Limite
Expresion < Limite
Expresion <= Limite

AND: require every condition

AND returns true only when every connected condition is true.

Condicion1 AND Condicion2

OR: accept either condition

OR returns true when at least one condition is true. When it is combined with AND, parentheses show which conditions form one rule.

Condicion1 OR Condicion2

NOT: negate a condition

NOT reverses a true or false condition. When NULL is involved, the result can also be unknown and requires examining the complete expression.

NOT (Condicion)

Example: combine requirements

We want active products with stock and price of up to 800 pesos:

SELECT
    p.IdProducto,
    p.Nombre,
    p.Precio,
    p.Existencia
FROM dbo.Producto AS p
WHERE p.Activo = 1
  AND p.Existencia > 0
  AND p.Precio <= 800
ORDER BY p.Precio;

Each row must satisfy all three expressions because they are joined by AND.

Group conditions with parentheses

AND is evaluated before OR. This priority is called operator precedence. If we want active products that belong to one of two categories, we make the grouping explicit:

SELECT
    p.IdProducto,
    p.Nombre,
    p.IdCategoria,
    p.Activo
FROM dbo.Producto AS p
WHERE p.Activo = 1
  AND
  (
      p.IdCategoria = 1
      OR p.IdCategoria = 2
  );

Without the parentheses, the second category could be accepted even if the product was not active. It is preferable to visually express the intent rather than relying solely on implied precedence.

Use NOT

SELECT
    p.IdPedido,
    p.Estado
FROM dbo.Pedido AS p
WHERE NOT (p.Estado = 'Cancelado');

In this case, p.Estado <> 'Cancelado' would be more direct. NOT is useful for negating compound conditions, EXISTS, IN, BETWEEN, or LIKE, as long as the final form remains easy to read.

Comparisons with NULL

A comparison like Correo = NULL does not return true. To check absence we use:

WHERE Correo IS NULL;

And to demand an existing value:

WHERE Correo IS NOT NULL;

NULL represents an absent or unknown value. Logic involving nulls therefore admits an unknown result in addition to true and false. That third possibility can change the effect of a negation or compound expression.

What should be checked

We check exact boundaries: > does not include the compared value, while >= does. We also check types and implicit conversions—those SQL Server applies without the query requesting them explicitly—especially between numbers, dates, and text.

When a condition combines AND and OR, the parentheses should make the rule obvious before you even run the query. A small case table can also confirm which combinations we expect to accept.