Aller au contenu principal

Exercices

Doublons d'adhérents
select a.id, a.nom, a.prenom
from adherent a
join adherent b on a.nom = b.nom
and a.prenom = b.prenom
and a.naissance = b.naissance
and a.id <> b.id;
446 | Pennino | Adrienne
1139 | Pennino | Adrienne
À SAVOIR

La jointure se fait sur d'autres critères que le couple clés primaire/étrangère

Tickets avec adhérent qui n'existe pas
select * from ticket t
where not exists (select 1 from adherent a where a.id = t.adherent_id);
109304 | 2017-10-13 14:19:19.000 | 9999 | 4
Ticket sans ligne
select t.* from ticket t
left join ligne l on l.ticket_id = t.id
where l.id is null;
Ligne non attachées à un ticket existant
select l.* from ticket t
right join ligne l on l.ticket_id = t.id
where t.id is null;

La jointure externe prend cette fois ci la droite.

ligne sans articles
select * from ligne l
where not exists (select 1 from article a where a.code = l.article_code);
589575 | 125141 | 42.23 | 1 | 42.23