Mix and match - jointures LEFT et RIGHT

Introduction à SQL Server

John MacKintosh

Instructor

Pourquoi des jointures LEFT et RIGHT

  • Pourquoi a-t-on besoin des jointures LEFT et RIGHT ?
  • Une table peut ne pas avoir de correspondance exacte dans l'autre :
    • Historique des commandes clients pour une campagne marketing
    • Liste des produits et historique des retours
    • Patients admis mais pas encore congédiés
Introduction à SQL Server

Pourquoi des jointures LEFT et RIGHT

  • Pourquoi a-t-on besoin des jointures LEFT et RIGHT ?
  • Une table peut ne pas avoir de correspondance exacte dans l'autre :
    • Historique des commandes clients pour une campagne marketing
    • Liste des produits et historique des retours
    • Patients admis mais pas encore congédiés
Introduction à SQL Server

Table Admissions

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

Table Sorties

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

INNER JOIN :

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

LEFT JOIN :

+------------+----------+------------|
| 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 du RIGHT JOIN

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

En résumé

  • INNER JOIN : retourne uniquement les lignes correspondantes
  • LEFT JOIN (ou RIGHT JOIN) : toutes les lignes de la table principale plus les correspondances de la table liée
  • NULL : affiché s'il n'y a aucune correspondance
  • LEFT JOIN et RIGHT JOIN peuvent être interchangeables
Introduction à SQL Server

Introduction à SQL Server

Passons à la pratique !

Introduction à SQL Server

Preparing Video For Download...