Plus de jointures

Introduction à Oracle SQL

Sara Billen

Curriculum Manager

Autres types de jointures

  • CROSS JOIN
  • Auto-JOIN
Introduction à Oracle SQL

Produit cartésien (aussi appelé cross product)

Schéma du produit cartésien

Introduction à Oracle SQL

Produit cartésien sur des tables

$$

Produit cartésien sur des tables

Introduction à Oracle SQL

Exemple de produit cartésien

SELECT * 
FROM MediaType
| MediaTypeId | Name                        |
|-------------|-----------------------------|
| 1           | MPEG audio file             |
| 2           | Protected AAC audio file    |
| 3           | Protected MPEG-4 video file |
| 4           | Purchased AAC audio file    |
| 5           | AAC audio file              |
Introduction à Oracle SQL

Exemple de produit cartésien

SELECT m.MediaTypeId, m.Name, t.TrackId, t.Name, t.MediaTypeId
FROM MediaType m CROSS JOIN Track t
| MediaTypeId | Name                        | TrackId | Name       | MediaTypeId       |
|-------------|-----------------------------|---------|------------|-------------------|
| 1           | MPEG audio file             | 3021    | Forty      | 1                 |
| 2           | Protected AAC audio file    | 3021    | Forty      | 1                 |
| 3           | Protected MPEG-4 video file | 3021    | Forty      | 1                 |
| 4           | Purchased AAC audio file    | 3021    | Forty      | 1                 |
| 5           | AAC audio file              | 3021    | Forty      | 1                 |

Introduction à Oracle SQL

Autojointure

  • Joindre une table à elle-même

Cas d'usage :

SELECT * FROM Employee
| EmployeeId | LastName | Title               | ... | ReportsTo |
|------------|----------|---------------------|-----|-----------|
| 1          | Adames   | General Manager     | ... | null      |
| 2          | Edwards  | Sales Manager       | ... | 1         |
| 3          | Peacock  | Sales Support Agent | ... | 2         |
| 4          | Park     | Sales Support Agent | ... | 2         |

Introduction à Oracle SQL

Autojointure

SELECT e.LastName Employee, m.LastName ReportsTo
FROM Employee e JOIN Employee m
ON (e.ReportsTo = m.EmployeeId)
| Employee | ReportsTo |
|----------|-----------|
| Edwards  | Adams     |
| Peacock  | Edwards   |
| Mitchell | Adams     |
| Park       | Edwards   |
| ...      | ...       |
Introduction à Oracle SQL

Passons à la pratique !

Introduction à Oracle SQL

Preparing Video For Download...