Consult customers with their confirmed orders

Situation

We need to prepare a list of confirmed orders. The customer name is in Cliente, while the date and status are in Pedido.

We want to keep only orders with status Confirmado and show the most recent ones first.

Relevant data

Client Order Date State
Ana Torres 1 2026-03-01 10:15 Confirmed
Ana Torres 2 2026-03-04 17:20 Sent
Bruno Diaz 3 2026-03-02 12:35 Pending
Elena Mora 5 2026-03-05 08:50 Confirmed

Final query

SELECT
    c.IdCliente,
    c.Nombre AS Cliente,
    pe.IdPedido,
    pe.FechaPedido,
    pe.Estado
FROM dbo.Cliente AS c
INNER JOIN dbo.Pedido AS pe
    ON pe.IdCliente = c.IdCliente
WHERE pe.Estado = 'Confirmado'
ORDER BY
    pe.FechaPedido DESC,
    pe.IdPedido DESC;

How it works

INNER JOIN keeps rows that match through IdCliente. A person with several confirmed orders appears several times because the unit of the result—what each row represents—is an order, not a customer.

WHERE is applied after matches are formed and discards the other states. The second ORDER BY criterion resolves date ties and makes the presentation deterministic: the same set keeps the same order.

Expected result

ClientId Client OrderId OrderDate State
5 Elena Mora 5 2026-03-05 08:50 Confirmed
1 Ana Torres 1 2026-03-01 10:15 Confirmed

Verified in SSMS

The result contains only the two confirmed orders and preserves the descending order defined by date and identifier.

Actual SSMS result for the confirmed-orders query

Variation: orders confirmed within a period

To search March 2026, we use an inclusive lower boundary, which admits its starting value, and the start of the next month as an exclusive upper boundary, which leaves that value out:

WHERE pe.Estado = 'Confirmado'
  AND pe.FechaPedido >= '20260301'
  AND pe.FechaPedido <  '20260401'

This form includes any time on the last day without depending on the precision of the date type.

Limits and cautions

If we only need to know which customers have at least one confirmed order, returning one row per order produces duplicates from the customer’s perspective. In that case EXISTS represents the question better than adding DISTINCT without checking why the rows are repeated.

We also check indexes and volume. An index organizes values to make rows easier to access. A foreign key requires each reference to point to a valid row, but it does not necessarily create every index this query needs.

Path index

Applied cases

10 articles in Applied cases
  1. 01Query customers with confirmed orders
  2. 02Find customers who do not have orders yet
  3. 03Show the first available phone number for each customer
  4. 04Calculate total sales by customer and filter the highest totals
  5. 05Insert an order and its line items safely
  6. 06Update category prices with prior validation
  7. 07Combine current and historical orders
  8. 08Return the latest order for each customer
  9. 09Calculate daily sales and a running total
  10. 10Build paginated search with optional filters