Lesson 3 of 20

Filter with WHERE and sort with ORDER BY

Turn a full reading into an accurate response with conditions and sorting.

18 minNo account required

By the end of this lesson

  • Build a condition WHERE
  • Combine multiple criteria
  • Sort with ASC and DESC

WHERE keeps rows that meet a condition

Without filter, the query returns all rows. WHERE is written after FROM and compares a column to a value.

The texts are surrounded by apostrophes. Numbers remain without apostrophes when stored as numbers.

  • = equal
  • <> different
  • > and < strict comparisons
  • >= and <= include limit value
Products less than 50 TND
SELECT produit, prix
FROM produits
WHERE prix < 50;

Combine with AND, OR and NOT

AND requires both conditions to be true. OR accepts that only one condition is true. Parentheses make the priority explicit.

NOT reverses a test. It should be used with wording that remains easy to reread.

Two mandatory conditions
SELECT produit, categorie, prix
FROM produits
WHERE categorie = 'IT'
  AND prix <= 100;
When AND and OR appear together, use parentheses even if you know their precedence. The next reader will thank you.

ORDER BY organizes the result

ORDER BY occurs after the filter. ASC sorts in ascending order and is usually the default. DESC applies descending order.

Several columns can be specified. The second separates the identical lines according to the first.

From highest to lowest price
SELECT produit, prix
FROM produits
WHERE prix >= 10
ORDER BY prix DESC, produit ASC;

Pay attention to the NULL values

NULL means no known value. It does not compare with = NULL.

Use IS NULL to find missing values and IS NOT NULL for populated values.

Customers without a city entered
SELECT nom
FROM clients
WHERE ville IS NULL;

Make a filter explainable and reproducible

A filter translates a business rule. Write this rule as a sentence before converting it to SQL: Paid orders, created during July, whose amount is at least 100 TND. Each word influences the operator and the limits.

Dates require special attention. For a complete period, an included start point and an excluded end point avoid time problems: date >= '2026-07-01' AND date < '2026-08-01'.

A ranking must also be stable. When two rows have the same main value, add a second unique criterion to obtain the same order in each execution and in each paginated export.

Dated filter and stable order
SELECT id_commande, date_commande, montant
FROM commandes
WHERE statut = 'Paid'
  AND date_commande >= DATE '2026-07-01'
  AND date_commande < DATE '2026-08-01'
ORDER BY montant DESC, id_commande ASC;
An implicit boundary, status, or sort almost always ends up creating a discrepancy in a report.

Check your understanding

Display products priced at least 40 TND, from cheapest to most expensive.

Complete this query
SELECT produit, prix
FROM produits
WHERE -- condition
ORDER BY -- sorting;

Progress

Have you completed this lesson?

Your choice stays in this browser and also syncs with your account when you are signed in.

Next lesson