forked from mouredev/hello-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Comandos JOIN
- Loading branch information
Showing
6 changed files
with
176 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
INNER JOIN (JOIN) | ||
Clase 5: https://twitch.tv/videos/1965141039 | ||
*/ | ||
|
||
-- Realiza un JOIN de manera incorrecta, ya que no existe un campo de relación | ||
SELECT * FROM users | ||
INNER JOIN dni; | ||
|
||
-- Obtiene los datos de los usuarios que tienen un dni | ||
SELECT * FROM users | ||
INNER JOIN dni | ||
ON users.user_id = dni.user_id; | ||
|
||
-- Obtiene los datos de los usuarios que tienen un dni (JOIN es lo mismo que INNER JOIN) | ||
SELECT * FROM users | ||
JOIN dni | ||
ON users.user_id = dni.user_id; | ||
|
||
-- Obtiene el nombre y el dni de los usuarios que tienen un dni y los ordena por edad | ||
SELECT name, dni_number FROM users | ||
JOIN dni | ||
ON users.user_id = dni.user_id | ||
ORDER BY age ASC; | ||
|
||
-- Obtiene los datos de los usuarios que tienen empresa | ||
SELECT * FROM users | ||
JOIN companies | ||
ON users.company_id = companies.company_id; | ||
|
||
-- Obtiene los datos de las empresas que tienen usuarios | ||
SELECT * FROM companies | ||
JOIN users | ||
ON users.company_id = companies.company_id; | ||
|
||
-- Obtiene el nombre de las empresas junto al nombre de sus usuarios | ||
SELECT companies.name, users.name FROM companies | ||
JOIN users | ||
ON companies.company_id = users.company_id; | ||
|
||
-- Obtiene los nombres de usuarios junto a los lenguajes que conocen | ||
SELECT users.name, languages.name | ||
FROM users_languages | ||
JOIN users ON users_languages.user_id=users.user_id | ||
JOIN languages ON users_languages.language_id=languages.language_id; | ||
|
||
-- Obtiene los nombres de usuarios junto a los lenguajes que conocen (utilizando otro orden de relación entre tablas) | ||
SELECT users.name, languages.name | ||
FROM users | ||
JOIN users_languages ON users.user_id=users_languages.user_id | ||
JOIN languages ON users_languages.language_id=languages.language_id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
LEFT JOIN | ||
Clase 5: https://twitch.tv/videos/1965141039 | ||
*/ | ||
|
||
-- Obtiene los datos de todos los usuarios junto a su dni (lo tenga o no) | ||
SELECT * FROM users | ||
LEFT JOIN dni | ||
ON users.user_id = dni.user_id; | ||
|
||
-- Obtiene el nombre de todos los usuarios junto a su dni (lo tenga o no) | ||
SELECT name, dni_number FROM users | ||
LEFT JOIN dni | ||
ON users.user_id = dni.user_id; | ||
|
||
-- Obtiene todos los dni junto al nombre de su usuario (lo tenga o no) | ||
SELECT name, dni_number FROM dni | ||
LEFT JOIN users | ||
ON users.user_id = dni.user_id; | ||
|
||
-- Obtiene el nombre de todos los usuarios junto a sus lenguajes (los tenga o no) | ||
SELECT users.name, languages.name | ||
FROM users | ||
LEFT JOIN users_languages ON users.user_id=users_languages.user_id | ||
LEFT JOIN languages ON users_languages.language_id=languages.language_id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
RIGHT JOIN | ||
Clase 5: https://twitch.tv/videos/1965141039 | ||
*/ | ||
|
||
-- Obtiene todos los dni junto a su usuario (lo tenga o no) | ||
SELECT * FROM users | ||
RIGHT JOIN dni | ||
ON users.user_id = dni.user_id; | ||
|
||
-- Obtiene todos los dni junto al nombre de su usuario (lo tenga o no) | ||
SELECT name, dni_number FROM users | ||
RIGHT JOIN dni | ||
ON users.user_id = dni.user_id; | ||
|
||
-- Obtiene el nombre de todos los usuarios junto a su dni (lo tenga o no) | ||
SELECT name, dni_number FROM dni | ||
RIGHT JOIN users | ||
ON users.user_id = dni.user_id; | ||
|
||
-- Obtiene el nombre de todos los lenguajes junto a sus usuarios (los tenga o no) | ||
SELECT users.name, languages.name | ||
FROM users | ||
RIGHT JOIN users_languages ON users.user_id=users_languages.user_id | ||
RIGHT JOIN languages ON users_languages.language_id=languages.language_id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
UNION (FULL JOIN) | ||
Clase 5: https://twitch.tv/videos/1965141039 | ||
*/ | ||
|
||
-- UNION elimina duplicados | ||
|
||
-- Obtiene todos los id de usuarios de las tablas dni y usuarios (exista o no relación) | ||
SELECT users.user_id AS u_user_id, dni.user_id AS d_user_id | ||
FROM users | ||
LEFT JOIN dni | ||
ON users.user_id = dni.user_id | ||
UNION | ||
SELECT users.user_id AS user_id, dni.user_id AS d_user_id | ||
FROM users | ||
RIGHT JOIN dni | ||
ON users.user_id = dni.user_id; | ||
|
||
-- Obtiene todos los datos de las tablas dni y usuarios (exista o no relación) | ||
SELECT * | ||
FROM users | ||
LEFT JOIN dni | ||
ON users.user_id = dni.user_id | ||
UNION | ||
SELECT * | ||
FROM users | ||
RIGHT JOIN dni | ||
ON users.user_id = dni.user_id; | ||
|
||
-- UNION ALL mantiene duplicados |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters