---
title: "SQL : Comprendre les rêquetes LEFT JOIN via un exemple"
url: https://www.cyber-neurones.org/2024/04/sql-comprendre-les-requetes-left-join-via-un-exemple/
date: 2024-04-20
modified: 2024-04-21
author: "Frederic"
description: "Voici un petit exemple, on va faire une base de donnée sous Postgresql : $ createdb CLASSE $ psql -d CLASSE psql (14.11 (Ubuntu 14.11-0ubuntu0.22.04.1), server 10.15 (Ubuntu 10.15-0ubuntu0.18.04.1)) Type..."
categories:
  - "Linux"
tags:
  - "SQL"
word_count: 446
---

# SQL : Comprendre les rêquetes LEFT JOIN via un exemple

Voici un petit exemple, on va faire une base de donnée sous **Postgresql** :

$ createdb CLASSE
$ psql -d CLASSE
psql (14.11 (Ubuntu 14.11-0ubuntu0.22.04.1), server 10.15 (Ubuntu 10.15-0ubuntu0.18.04.1))
Type "help" for help.
CLASSE=> create table classe (Prenom varchar(50) primary key, classe int);
CREATE TABLE
CLASSE=> create table math (Prenom varchar(50) primary key, note float);
CREATE TABLE
CLASSE=> create table anglais (Prenom varchar(50) primary key, note float);
CREATE TABLE
CLASSE=> create table espagnol (Prenom varchar(50) primary key, note float);
CREATE TABLE
CLASSE=> insert into classe (Prenom, classe) values ('Riri',1);
INSERT 0 1
CLASSE=> insert into classe (Prenom, classe) values ('Fifi',1);
INSERT 0 1
CLASSE=> insert into classe (Prenom, classe) values ('Loulou',1);
INSERT 0 1
CLASSE=> insert into classe (Prenom, classe) values ('Paul',2);
INSERT 0 1
CLASSE=> insert into classe (Prenom, classe) values ('Pierre',2);
INSERT 0 1
CLASSE=> insert into classe (Prenom, classe) values ('Jacques',2);
INSERT 0 1
CLASSE=> insert into math (Prenom, note) values ('Riri',10);
INSERT 0 1
CLASSE=> insert into math (Prenom, note) values ('Fifi',11);
INSERT 0 1
CLASSE=> insert into math (Prenom, note) values ('Loulou',12);
INSERT 0 1
CLASSE=> insert into math (Prenom, note) values ('Paul',13);
INSERT 0 1
CLASSE=> insert into math (Prenom, note) values ('Pierre',14);
INSERT 0 1
CLASSE=> insert into math (Prenom, note) values ('Jacques',15);
INSERT 0 1
CLASSE=> insert into anglais (Prenom, note) values ('Riri',11);
INSERT 0 1
CLASSE=> insert into anglais (Prenom, note) values ('Fifi',12);
INSERT 0 1
CLASSE=> insert into espagnol (Prenom, note) values ('Loulou',13);
INSERT 0 1
CLASSE=> insert into espagnol (Prenom, note) values ('Paul',14);
INSERT 0 1
CLASSE=> insert into espagnol (Prenom, note) values ('Pierre',15);
INSERT 0 1
CLASSE=> insert into anglais (Prenom, note) values ('Jacques',16);
INSERT 0 1

On a donc 2 classes de 3 élèves, et dans les classes tous le monde à math mais les langues sont mélangés.

Sans jointure on n'a rien :

CLASSE=> select c.Prenom,m.note as Math,a.note as Anglais, e.note as Espagnol
from classe c, math m, anglais a, espagnol e
where c.Prenom = a.Prenom and c.Prenom = m.Prenom and c.Prenom = e.Prenom and c.classe = 1;

prenom | math | anglais | espagnol
--------+------+---------+----------
(0 rows)

Ce qui est normal car personne ne fait Anglais & Espagnol.

Avec une jointure je vais ajouter les données **si elles sont disponibles** :

CLASSE=> select c.Prenom,m.note as Math,a.note as Anglais, e.note as Espagnol
from classe c
**LEFT JOIN** math m ON c.Prenom = m.Prenom
**LEFT JOIN** anglais a ON c.Prenom = a.Prenom
**LEFT JOIN **espagnol e ON c.Prenom = e.Prenom
where c.classe = 1;
prenom | math | anglais | espagnol
--------+------+---------+----------
Loulou | 12 | | 13
Riri | 10 | 11 |
Fifi | 11 | 12 |
(3 rows)

 