WHERE: decide which rows meet the condition

Selecting the correct columns is not always enough. We may only need active clients, confirmed orders or movements that occurred within a period.

WHERE allows you to express that condition before returning the result.

The idea

Each row is evaluated against a logical condition: an expression whose result determines whether the row satisfies the rule. If the condition is true, the row continues in the result. If it is false or unknown, it is left out.

The third, unknown possibility appears especially when working with NULL. That’s why a null value is not compared using = NULL.

Syntax

SELECT
    Columna1,
    Columna2
FROM dbo.Tabla
WHERE Condicion;

Conditions can use comparisons such as =, <>, >, >=, <, and <=. They can also be combined using AND, OR and NOT.

Example: compare and combine conditions

To obtain active products whose price is at least 300 pesos:

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

The two conditions must be met because they are linked by AND.

ProductId Name Price
1 USB-C adapter 349.00
4 Compact keyboard 629.90
3 Monitor stand 799.00

Frequent variations

Search within a list

SELECT
    p.IdPedido,
    p.IdCliente,
    p.Estado
FROM dbo.Pedido AS p
WHERE p.Estado IN ('Confirmado', 'Enviado');

IN avoids repeating the same column in multiple comparisons joined with OR.

Search for a pattern

SELECT
    c.IdCliente,
    c.Nombre
FROM dbo.Cliente AS c
WHERE c.Nombre LIKE N'A%';

The symbol % represents any number of characters. In this case, the name must begin with A.

Find null values

SELECT
    c.IdCliente,
    c.Nombre
FROM dbo.Cliente AS c
WHERE c.Correo IS NULL;

To request otherwise, use IS NOT NULL.

Filter a period that includes time

DATEADD shifts a date by a specified number of units. In DATEADD(DAY, 1, @FechaFinal), DAY indicates the unit, 1 the amount being added, and @FechaFinal the date from which the calculation begins.

DECLARE @FechaInicial DATE = '2026-03-01';
DECLARE @FechaFinal   DATE = '2026-03-04';

SELECT
    p.IdPedido,
    p.FechaPedido,
    p.Estado
FROM dbo.Pedido AS p
WHERE p.FechaPedido >= @FechaInicial
  AND p.FechaPedido < DATEADD(DAY, 1, @FechaFinal);

The upper limit is exclusive. This way we include all the hours of March 4 without depending on whether the column stores seconds, milliseconds or a greater precision.

What should be checked

When a condition mixes AND and OR, the parentheses make the intent explicit:

WHERE p.Activo = 1
  AND (p.Precio < 300 OR p.Existencia = 0);

Without the parentheses, SQL applies its operator precedence, a predefined order that determines which operations are evaluated first. The query can be valid and still return a different set than expected.

It is also important to review the data type. An implicit conversion occurs when SQL Server automatically changes a value to another type so it can perform the comparison. Comparing a numeric or date column with text can trigger that conversion, produce an error, or prevent the plan from using an efficient access method.