Jointures LEFT et RIGHT

Introduction à SQL Server

John MacKintosh

Instructor

Justification des jointures LEFT et RIGHT

  • Pourquoi avons-nous besoin des jointures LEFT et RIGHT ?
  • Il est possible qu'une table ne corresponde pas exactement à une autre :
    • Historique des commandes des clients pour la campagne marketing
    • Liste des produits et historique des retours
    • Patients admis mais pas encore sortis
Introduction à SQL Server

Justification des jointures LEFT et RIGHT

  • Pourquoi avons-nous besoin des jointures LEFT et RIGHT ?
  • Il est possible qu'une table ne corresponde pas exactement à une autre :
    • Historique des commandes des clients pour la campagne marketing
    • Liste des produits et historique des retours
    • Patients admis mais pas encore sortis
Introduction à SQL Server

Table des admissions

+------------+----------+
| Patient_ID | Admitted | 
|------------+----------|
| 1          | 1        |
| 2          | 1        |
| 3          | 1        |
| 4          | 1        |
| 5          | 1        |
+------------+----------+

Table des sorties

+------------+------------+
| Patient_ID | Discharged | 
|------------+------------|
| 1          | 1          |
| 3          | 1          |
| 4          | 1          |
+------------+------------+

JOINTURE INTERNE :

+------------+----------+------------|
| Patient_ID | Admitted | Discharged |
|------------+----------|------------|
| 1          | 1        | 1          |
| 3          | 1        | 1          |
| 4          | 1        | 1          |
+------------+----------+------------+

JOIN LEFT :

+------------+----------+------------|
| Patient_ID | Admitted | Discharged |
|------------+----------|------------|
| 1          | 1        | 1          |
| 2          | 1        | NULL       |
| 3          | 1        | 1          |
| 4          | 1        | 1          |
| 5          | 1        | NULL       |
+------------+----------+------------+
Introduction à SQL Server

SYNTAXE LEFT JOIN

SELECT 
  Admitted.Patient_ID, 
  Admitted, 
  Discharged 
FROM Admitted 
LEFT JOIN Discharged ON Discharged.Patient_ID = Admitted.Patient_ID;

Introduction à SQL Server
SELECT 
  Admitted.Patient_ID, 
  Admitted, 
  Discharged 
FROM Admitted 
LEFT JOIN Discharged ON Discharged.Patient_ID = Admitted.Patient_ID;
+------------+----------+------------|
| Patient_ID | Admitted | Discharged |
|------------+----------|------------|
| 1          | 1        | 1          |
| 2          | 1        | NULL       |
| 3          | 1        | 1          |
| 4          | 1        | 1          |
| 5          | 1        | NULL       |
+------------+----------+------------+
Introduction à SQL Server

RIGHT JOIN

SELECT 
  Admitted.Patient_ID, 
  Admitted, 
  Discharged 
FROM Discharged 
RIGHT JOIN Admitted ON Admitted.Patient_ID = Discharged.Patient_ID;

Introduction à SQL Server

Résultats de la JOIN RIGHT

SELECT 
  Admitted.Patient_ID, 
  Admitted, 
  Discharged 
FROM Discharged 
RIGHT JOIN Admitted ON Admitted.Patient_ID = Discharged.Patient_ID;
+------------+----------+------------|
| Patient_ID | Admitted | Discharged |
|------------+----------|------------|
| 1          | 1        | 1          |
| 2          | 1        | NULL       |
| 3          | 1        | 1          |
| 4          | 1        | 1          |
| 5          | 1        | NULL       |
+------------+----------+------------+
Introduction à SQL Server

Récapitulatif

  • INNER JOIN : Renvoie uniquement les lignes correspondantes
  • LEFT JOIN (ou RIGHT JOIN) : Toutes les lignes de la table principale ainsi que les correspondances de la table de jointure
  • NULL : Affiché si aucune correspondance n'est trouvée
  • LEFT JOIN et RIGHT JOIN peuvent être interchangeables
Introduction à SQL Server

Introduction à SQL Server

Passons à la pratique !

Introduction à SQL Server

Preparing Video For Download...