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
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.
SELECT produit, categorie, prix
FROM produits
WHERE categorie = 'IT'
AND prix <= 100;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.
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.
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.
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;Check your understanding
Display products priced at least 40 TND, from cheapest to most expensive.
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.