Usar joins

Introducción a Oracle SQL

Sara Billen

Curriculum Manager

Conjunto de datos Chinook

Diagrama ER del conjunto de datos completo de Chinook

Introducción a Oracle SQL

Combinar datos de varias tablas

Álbumes de Pearl Jam

Introducción a Oracle SQL

Joins en SQL

Tipos de joins:

  • Inner Join
  • Outer Joins
  • Cross Joins
  • Self Joins
Introducción a 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  |
| ...                             | ...           |
Introducción a Oracle SQL

Desglose de un inner join

  1. Elige las columnas de salida e incluye los nombres de tabla
  2. En FROM, lista las tablas separadas por INNER JOIN
  3. Usa ON para definir la columna de unión
SELECT Album.Title, Artist.Name
FROM Album INNER JOIN Artist
ON Album.ArtistId = Artist.ArtistId

Añade otras cláusulas necesarias como WHERE, ORDER BY.

Introducción a 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   |
| ...         | ...         |
Introducción a Oracle SQL

USING en lugar de ON

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

igual que

SELECT Album.Title, Artist.Name
FROM Album INNER JOIN Artist
USING (ArtistId)
  • Las columnas deben llamarse igual en ambas tablas
  • Encierra el nombre de columna entre paréntesis
Introducción a Oracle SQL

Alias de tablas

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

Con alias:

SELECT DISTINCT c.FirstName, c.LastName, e.FirstName, e.LastName
FROM Customer c INNER JOIN Employee e
ON c.SupportRepID = e.EmployeeID
Introducción a Oracle SQL

Unir más de dos tablas

Tablas de track, album y 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)
Introducción a Oracle SQL

Unir más de dos tablas

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 |
| ...                | ...                            | ...         |
Introducción a Oracle SQL

¡Vamos a practicar!

Introducción a Oracle SQL

Preparing Video For Download...