Données · SQL
Pourquoi un WHERE annule-t-il un LEFT JOIN ?
Quatre commandes de 40, 60, 20 et 50 € : combien passent montant > 30 ?
Et si le montant de 20 € devient NULL ?
D'un filtre sur des montants aux deux versions d'une même requête, cinq points de passage :
Quand SQL applique-t-il le WHERE d'une jointure ?
Le WHERE passe après la jointure, sur chaque ligne de son résultat. Une commande de 12 € sort de la jointure, puis WHERE montant > 30 écarte sa ligne.
Prenons trois clients et leurs commandes.
SELECT nom, montant FROM clients LEFT JOIN commandes ON commandes.client_id = clients.id WHERE commandes.montant > 30
| id | nom |
|---|---|
| 1 | Léa |
| 2 | Malik |
| 3 | Inès |
| id | client_id | montant |
|---|---|---|
| 101 | 1 | 40 € |
| 102 | 1 | 70 € |
| 103 | 2 | 25 € |
| 104 | 2 | 60 € |
| 105 | 3 | 50 € |
| nom | montant |
|---|---|
| Léa | 40 € |
| Léa | 70 € |
| Malik | 25 € |
| Malik | 60 € |
| Inès | 50 € |
| nom | montant |
|---|---|
| Léa | 40 € |
| Léa | 70 € |
| Malik | 60 € |
| Inès | 50 € |
Quelles lignes passent
montant > 30? On les touche dans la jointure.On lit le montant de chaque ligne et on le compare à 30.
SELECT nom, montant FROM clients LEFT JOIN commandes ON commandes.client_id = clients.id WHERE commandes.montant > 40
clients id nom 1 Léa 2 Malik 3 Inès commandes id client_id montant 101 1 35 € 102 1 65 € 103 2 20 € 104 2 45 € 105 3 70 € SELECT nom, montant FROM clients LEFT JOIN commandes ON commandes.client_id = clients.id WHERE commandes.montant > 30
clients id nom 1 Léa 2 Malik 3 Inès 4 Hugo commandes id client_id montant 106 4 15 € 101 1 25 € 103 2 35 € 105 4 40 € 102 2 50 € 104 3 75 €
Pourquoi un WHERE fait-il disparaître la ligne NULL d'un LEFT JOIN ?
Le client dont l'id vaut 7 n'a aucune commande : sa ligne porte un montant NULL. NULL > 0 n'est pas vrai, donc WHERE montant > 0 écarte sa ligne.
Prenons trois clients, dont un sans commande.
SELECT nom, montant FROM clients LEFT JOIN commandes ON commandes.client_id = clients.id WHERE commandes.montant > 30
| id | nom |
|---|---|
| 1 | Léa |
| 2 | Malik |
| 3 | Inès |
| id | client_id | montant |
|---|---|---|
| 101 | 1 | 40 € |
| 102 | 3 | 60 € |
| nom | montant |
|---|---|
| Léa | 40 € |
| Malik | NULL |
| Inès | 60 € |
| nom | montant |
|---|---|
| Léa | 40 € |
| Inès | 60 € |
Quelles lignes passent
montant > 30?On compare chaque montant à 30. Sur la ligne de Malik, on compare NULL à 30.
Sans
WHERE, quelle jointure donne ces mêmes lignes ?
SELECT nom, montant FROM clients LEFT JOIN commandes ON commandes.client_id = clients.id WHERE commandes.montant > 40
clients id nom 1 Léa 2 Malik 3 Inès 4 Hugo commandes id client_id montant 101 1 45 € 102 3 20 € 103 3 65 € 104 4 35 € SELECT nom, montant FROM clients LEFT JOIN commandes ON commandes.client_id = clients.id WHERE commandes.montant > 40
clients id nom 1 Léa 2 Malik 3 Inès 4 Hugo 5 Jade commandes id client_id montant 104 4 15 € 106 5 25 € 102 2 30 € 101 1 50 € 105 5 60 € 103 2 70 €
Comment filtrer un LEFT JOIN sans perdre de clients ?
Écrite dans ON, la condition choisit les commandes à relier, puis LEFT JOIN garde chaque client. Le client dont l'id vaut 7 n'a qu'une commande de 12 € : AND montant > 30 la refuse, et il sort quand même, avec un montant NULL.
Prenons trois clients, avec la condition dans ON.
SELECT nom, montant FROM clients LEFT JOIN commandes ON commandes.client_id = clients.id AND commandes.montant > 30
| id | nom |
|---|---|
| 1 | Léa |
| 2 | Malik |
| 3 | Inès |
| id | client_id | montant |
|---|---|---|
| 101 | 1 | 40 € |
| 102 | 1 | 60 € |
| 103 | 1 | 25 € |
| 104 | 3 | 20 € |
| nom | montant |
|---|---|
| Léa | 40 € |
| Léa | 60 € |
| Malik | NULL |
| Inès | NULL |
Que contient la colonne
montantsur la ligne d'Inès ?On regarde d'abord quelles commandes d'Inès passent
montant > 30.
SELECT nom, montant FROM clients LEFT JOIN commandes ON commandes.client_id = clients.id AND commandes.montant > 40
clients id nom 1 Léa 2 Malik 3 Inès 4 Hugo commandes id client_id montant 101 1 50 € 102 1 65 € 103 2 15 € 104 2 45 € 105 3 20 € 106 4 35 € SELECT nom, montant FROM clients LEFT JOIN commandes ON commandes.client_id = clients.id AND commandes.montant > 50
clients id nom 1 Léa 2 Malik 3 Inès 4 Hugo 5 Jade commandes id client_id montant 104 3 15 € 101 1 25 € 106 5 30 € 105 4 45 € 102 3 55 € 103 3 70 €
Quelle différence entre une condition dans ON et dans WHERE ?
Sur 7 clients, la condition écrite dans ON donne au moins 7 lignes, une par client au moins. Écrite dans WHERE, elle ne garde que les lignes où elle est vraie, et peut n'en laisser aucune.
Prenons quatre clients et les deux versions de la requête.
SELECT nom, montant FROM clients LEFT JOIN commandes ON commandes.client_id = clients.id WHERE commandes.montant > 30
SELECT nom, montant FROM clients LEFT JOIN commandes ON commandes.client_id = clients.id AND commandes.montant > 30
| id | nom |
|---|---|
| 1 | Léa |
| 2 | Malik |
| 3 | Inès |
| 4 | Hugo |
| id | client_id | montant |
|---|---|---|
| 101 | 1 | 70 € |
| 102 | 1 | 45 € |
| 103 | 1 | 20 € |
| 104 | 2 | 35 € |
| 105 | 4 | 15 € |
| nom | montant |
|---|---|
| Léa | 70 € |
| Léa | 45 € |
| Malik | 35 € |
| nom | montant |
|---|---|
| Léa | 70 € |
| Léa | 45 € |
| Malik | 35 € |
| Inès | NULL |
| Hugo | NULL |
SELECT nom, montant FROM clients LEFT JOIN commandes ON commandes.client_id = clients.id WHERE commandes.montant > 30
SELECT nom, montant FROM clients LEFT JOIN commandes ON commandes.client_id = clients.id AND commandes.montant > 30
| id | nom |
|---|---|
| 1 | Léa |
| 2 | Malik |
| 3 | Inès |
| 4 | Hugo |
| id | client_id | montant |
|---|---|---|
| 101 | 1 | 25 € |
| 102 | 1 | 55 € |
| 103 | 3 | 40 € |
| 104 | 3 | 65 € |
| 105 | 3 | 50 € |
| 106 | 4 | 20 € |
Comment vérifier vite un LEFT JOIN filtré ?
On regarde d'abord où la condition est écrite. Dans WHERE, on compte les commandes qui la passent. Dans ON, chaque client sort une fois par commande qui la passe, et une fois s'il n'en a aucune.
SELECT nom, montant FROM clients LEFT JOIN commandes ON commandes.client_id = clients.id WHERE commandes.montant > 40
clients id nom 1 Léa 2 Malik 3 Inès commandes id client_id montant 101 1 35 € 102 3 60 € 103 3 15 € SELECT nom, montant FROM clients LEFT JOIN commandes ON commandes.client_id = clients.id AND commandes.montant > 40
clients id nom 1 Léa 2 Malik 3 Inès 4 Hugo commandes id client_id montant 101 1 45 € 102 1 20 € 103 3 30 € 104 4 55 € SELECT nom, montant FROM clients LEFT JOIN commandes ON commandes.client_id = clients.id WHERE commandes.montant > 40
clients id nom 1 Léa 2 Malik 3 Inès 4 Hugo commandes id client_id montant 101 1 20 € 105 4 30 € 103 2 35 € 104 4 65 € 102 1 75 € SELECT nom, montant FROM clients LEFT JOIN commandes ON commandes.client_id = clients.id AND commandes.montant > 40
clients id nom 1 Léa 2 Malik 3 Inès 4 Hugo 5 Jade commandes id client_id montant 104 3 15 € 103 3 25 € 107 5 35 € 105 4 45 € 102 1 50 € 101 1 65 € 106 5 80 €