Mixen en matchen - LEFT & RIGHT joins

Introductie tot SQL Server

John MacKintosh

Instructor

Waarom LEFT en RIGHT joins?

  • Waarom hebben we LEFT en RIGHT joins nodig?
  • Eén tabel heeft mogelijk geen exacte match in een andere:
    • Bestelgeschiedenis klanten voor marketingcampagne
    • Productlijst en retourgeschiedenis
    • Patiënten opgenomen maar nog niet ontslagen
Introductie tot SQL Server

Waarom LEFT en RIGHT joins?

  • Waarom hebben we LEFT en RIGHT joins nodig?
  • Eén tabel heeft mogelijk geen exacte match in een andere:
    • Bestelgeschiedenis klanten voor marketingcampagne
    • Productlijst en retourgeschiedenis
    • Patiënten opgenomen maar nog niet ontslagen
Introductie tot SQL Server

Tabel Admissions

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

Tabel Discharges

+------------+------------+
| 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       |
+------------+----------+------------+
Introductie tot SQL Server

LEFT JOIN-syntaxis

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

Introductie tot 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       |
+------------+----------+------------+
Introductie tot SQL Server

RIGHT JOIN

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

Introductie tot SQL Server

RIGHT JOIN-resultaten

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       |
+------------+----------+------------+
Introductie tot SQL Server

Samenvatting

  • INNER JOIN: alleen overeenkomende rijen
  • LEFT JOIN (of RIGHT JOIN): alle rijen uit de hoofdtabel plus matches uit de jointabel
  • NULL: getoond als er geen match is
  • LEFT JOIN en RIGHT JOIN zijn uitwisselbaar
Introductie tot SQL Server

Introductie tot SQL Server

Laten we oefenen!

Introductie tot SQL Server

Preparing Video For Download...