Leçon 1 / 9 — Chapitre 6: Regroupements avancés
Analyse exploratoire (1/2) : DISTINCT, compter, doublons
SELECT DISTINCT statut FROM clients;
→ 2 valeurs distinctes : 'actif', 'inactif'.
SELECT COUNT(*) FROM clients;
→ 6 (le nombre total de lignes, doublon inclus).
SELECT nom, email, COUNT(*)
FROM clients
GROUP BY nom, email
HAVING COUNT(*) > 1;
→ 1 groupe : Emma / emma@mail.com (lignes id 1 et id 5, strictement identiques).
À retenir :
SELECT DISTINCT colonneliste les valeurs uniques d'une colonne, sans répétition.COUNT(*)sans GROUP BY compte toutes les lignes de la table (ou du résultat filtré).- Détecter des doublons se fait en regroupant sur les colonnes concernées (
GROUP BY) et en filtrant les groupes dontCOUNT(*) > 1(HAVING).
À vous de jouer : testez SELECT DISTINCT nom FROM clients; et comptez combien de noms distincts apparaissent (Emma ne devrait compter qu'une fois, malgré le doublon).
À vous de jouer
À faireVoici une table clients :
clients
id | nom | email | date_inscription | montant | statut
1 | Emma | emma@mail.com | 2024-03-15 | 120.456 | actif
2 | LUCAS | lucas@mail.com | 2024-04-02 | 85.2 | actif
3 | Nina | (NULL) | 2024-04-02 | 200 | inactif
4 | noé | noe@mail.com | (NULL) | 60.75 | actif
5 | Emma | emma@mail.com | 2024-03-15 | 120.456 | actif
6 | Sophie | sophie@mail.com | 2024-05-20 | -150 | inactif
Combien de valeurs distinctes retourne SELECT DISTINCT statut FROM clients; ?
Testez cette requête dans un bac à sable SQL en ligne (SQLite Fiddle, W3Schools "Try SQL", ou tout autre) pour vérifier le résultat avant de répondre.
💡 Un nombre.
À vous de jouer
À faireVoici une table clients :
clients
id | nom | email | date_inscription | montant | statut
1 | Emma | emma@mail.com | 2024-03-15 | 120.456 | actif
2 | LUCAS | lucas@mail.com | 2024-04-02 | 85.2 | actif
3 | Nina | (NULL) | 2024-04-02 | 200 | inactif
4 | noé | noe@mail.com | (NULL) | 60.75 | actif
5 | Emma | emma@mail.com | 2024-03-15 | 120.456 | actif
6 | Sophie | sophie@mail.com | 2024-05-20 | -150 | inactif
Que retourne SELECT COUNT(*) FROM clients; ?
Testez cette requête dans un bac à sable SQL en ligne (SQLite Fiddle, W3Schools "Try SQL", ou tout autre) pour vérifier le résultat avant de répondre.
💡 Un nombre.
À vous de jouer
À faireVoici une table clients :
clients
id | nom | email | date_inscription | montant | statut
1 | Emma | emma@mail.com | 2024-03-15 | 120.456 | actif
2 | LUCAS | lucas@mail.com | 2024-04-02 | 85.2 | actif
3 | Nina | (NULL) | 2024-04-02 | 200 | inactif
4 | noé | noe@mail.com | (NULL) | 60.75 | actif
5 | Emma | emma@mail.com | 2024-03-15 | 120.456 | actif
6 | Sophie | sophie@mail.com | 2024-05-20 | -150 | inactif
Combien de groupes retourne SELECT nom, email, COUNT(*) FROM clients GROUP BY nom, email HAVING COUNT(*) > 1; ?
Testez cette requête dans un bac à sable SQL en ligne (SQLite Fiddle, W3Schools "Try SQL", ou tout autre) pour vérifier le résultat avant de répondre.
💡 Un nombre.