Usando joins

Introdução ao Oracle SQL

Sara Billen

Curriculum Manager

Conjunto de dados Chinook

Diagrama ER completo do conjunto de dados Chinook

Introdução ao Oracle SQL

Combinando dados de mais de uma tabela

Álbuns do Pearl Jam

Introdução ao Oracle SQL

Joins em SQL

Tipos de joins:

  • Inner Join
  • Outer Joins
  • Cross Joins
  • Self Joins
Introdução ao Oracle SQL

Inner join

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

Detalhando um inner join

  1. Selecione as colunas de saída e inclua os nomes das tabelas
  2. No FROM, liste as tabelas separadas por INNER JOIN
  3. Use ON para definir a coluna de junção
SELECT Album.Title, Artist.Name
FROM Album INNER JOIN Artist
ON Album.ArtistId = Artist.ArtistId

Adicione outras cláusulas necessárias, como WHERE, ORDER BY!

Introdução ao Oracle SQL

Inner join

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

USING em vez de ON

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

mesmo que

SELECT Album.Title, Artist.Name
FROM Album INNER JOIN Artist
USING (ArtistId)
  • As colunas precisam ter nomes idênticos nas duas tabelas
  • Coloque o nome da coluna entre parênteses
Introdução ao Oracle SQL

Aliases de tabela

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

Com aliases:

SELECT DISTINCT c.FirstName, c.LastName, e.FirstName, e.LastName
FROM Customer c INNER JOIN Employee e
ON c.SupportRepID = e.EmployeeID
Introdução ao Oracle SQL

Juntando mais de duas tabelas

Tabelas de faixa, álbum e artista

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

Juntando mais de duas tabelas

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

Vamos praticar!

Introdução ao Oracle SQL

Preparing Video For Download...