Utiliser les jointures

Introduction à Oracle SQL

Sara Billen

Curriculum Manager

Ensemble de données Chinook

Schéma relationnel complet de l'ensemble de données Chinook

Introduction à Oracle SQL

Combiner des données de plusieurs tables

Albums de Pearl Jam

Introduction à Oracle SQL

Jointures SQL

Types de jointures :

  • Jointure interne
  • Jointures externes
  • Jointures croisées
  • Auto-jointures
Introduction à Oracle SQL

Jointure interne

SELECT Album.Title, Artist.Name
FROM Album INNER JOIN Artist
ON  Album.ArtistId = Artist.ArtistId
| Album.Title                     | Artist.Name   |
|---------------------------------|---------------|
| Black Sabbath                   | Black Sabbath |
| Black Sabbath Vol. 4 (Remaster) | Black Sabbath |
| The Cream Of Clapton            | Eric Clapton  |
| Unplugged                       | Eric Clapton  |
| ...                             | ...           |
Introduction à Oracle SQL

Décomposition d'une jointure interne

  1. Sélectionner les colonnes à afficher et inclure les noms de table
  2. Dans FROM, lister les tables séparées par INNER JOIN
  3. Utiliser ON pour définir la colonne de jointure
SELECT Album.Title, Artist.Name
FROM Album INNER JOIN Artist
ON Album.ArtistId = Artist.ArtistId

Ajouter d'autres clauses au besoin, comme WHERE, ORDER BY !

Introduction à Oracle SQL

Jointure interne

SELECT Album.Title, Artist.Name
FROM Album INNER JOIN Artist
ON  Album.ArtistId = Artist.ArtistId
WHERE Artist.Name = 'Pearl Jam'
| Album.Title | Artist.Name |
|-------------|-------------|
| Pearl Jam   | Pearl Jam   |
| Riot Act    | Pearl Jam   |
| Ten         | Pearl Jam   |
| ...         | ...         |
Introduction à Oracle SQL

USING au lieu de ON

SELECT Album.Title, Artist.Name
FROM Album INNER JOIN Artist
ON  Album.ArtistId = Artist.ArtistId

équivaut à

SELECT Album.Title, Artist.Name
FROM Album INNER JOIN Artist
USING (ArtistId)
  • Les colonnes doivent avoir le même nom dans les deux tables
  • Mettre le nom de colonne entre parenthèses
Introduction à Oracle SQL

Alias de table

SELECT DISTINCT Customer.FirstName, Customer.LastName, 
                Employee.FirstName, Employee.LastName
FROM Customer INNER JOIN Employee
ON Customer.SupportRepID = Employee.EmployeeID

Avec alias :

SELECT DISTINCT c.FirstName, c.LastName, e.FirstName, e.LastName
FROM Customer c INNER JOIN Employee e
ON c.SupportRepID = e.EmployeeID
Introduction à Oracle SQL

Joindre plus de deux tables

Tables Track, Album et Artist

SELECT t.Name AS Track, al.Title AS Album, ar.Name AS Artist
FROM 
    Track t INNER JOIN Album al USING (AlbumId)
    INNER JOIN Artist ar USING (ArtistId)
Introduction à Oracle SQL

Joindre plus de deux tables

SELECT t.Name as Track, al.Title as Album, ar.Name as Artist
FROM 
    Track t INNER JOIN Album al USING (AlbumId)
    INNER JOIN Artist ar USING (ArtistId)
| Track              | Album                          | Artist      |
|--------------------|--------------------------------|-------------|
| The Legacy         | A Matter of Life and Death     | Iron Maiden |
| Lord of Light      | A Matter of Life and Death     | Iron Maiden |
| Out of the Shadows | A Matter of Life and Death     | Iron Maiden |
| ...                | ...                            | ...         |
Introduction à Oracle SQL

Passons à la pratique !

Introduction à Oracle SQL

Preparing Video For Download...