Mais joins

Introdução ao Oracle SQL

Sara Billen

Curriculum Manager

Outros tipos de join

  • CROSS JOIN
  • Self JOIN
Introdução ao Oracle SQL

Produto cartesiano (também chamado de cross product)

Diagrama de produto cartesiano

Introdução ao Oracle SQL

Cross product em tabelas

$$

Cross product em tabelas

Introdução ao Oracle SQL

Exemplo de produto cartesiano

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              |
Introdução ao Oracle SQL

Exemplo de produto cartesiano

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                 |

Introdução ao Oracle SQL

Self join

  • Fazer join de uma tabela com ela mesma

Caso de uso:

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         |

Introdução ao Oracle SQL

Self join

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   |
| ...      | ...       |
Introdução ao Oracle SQL

Vamos praticar!

Introdução ao Oracle SQL

Preparing Video For Download...