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 SELECT y de escritura (INSERT, UPDATE y DELETE)
- Loading branch information
Showing
23 changed files
with
172 additions
and
2 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,11 @@ | ||
-- Comentario en una lína | ||
|
||
/* | ||
Este | ||
es | ||
un | ||
comentario | ||
en | ||
varias | ||
líneas | ||
*/ |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,16 @@ | ||
/* | ||
NULL | ||
Clase 3: https://twitch.tv/videos/1953432950 | ||
*/ | ||
|
||
-- Obtiene todos datos de la tabla "users" de la tabla "users" con email nulo | ||
SELECT * FROM users WHERE email IS NULL; | ||
|
||
-- Obtiene todos datos de la tabla "users" de la tabla "users" con email no nulo | ||
SELECT * FROM users WHERE email IS NOT NULL; | ||
|
||
-- Obtiene todos datos de la tabla "users" de la tabla "users" con email no nulo y edad igual a 15 | ||
SELECT * FROM users WHERE email IS NOT NULL AND age = 15; | ||
|
||
-- Obtiene el nombre, apellido y edad de la tabla "users", y si la edad es nula la muestra como 0 | ||
SELECT name, surname, IFNULL(age, 0) AS age FROM users; |
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,10 @@ | ||
/* | ||
MIN MAX | ||
Clase 3: https://twitch.tv/videos/1953432950 | ||
*/ | ||
|
||
-- Obtiene el valor menor del campo edad de la tabla "users" | ||
Select MIN(age) FROM users; | ||
|
||
-- Obtiene el valor mayor del campo edad de la tabla "users" | ||
Select MAX(age) FROM users; |
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,10 @@ | ||
/* | ||
COUNT | ||
Clase 3: https://twitch.tv/videos/1953432950 | ||
*/ | ||
|
||
-- Cuenta cuantas filas contiene la tabla "users" | ||
Select COUNT(*) FROM users; | ||
|
||
-- Cuenta cuantas filas contienen un dato no nulo en el campo edad de la tabla "users" | ||
Select COUNT(age) FROM users; |
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,7 @@ | ||
/* | ||
SUM | ||
Clase 3: https://twitch.tv/videos/1953432950 | ||
*/ | ||
|
||
-- Suma todos los valores del campo edad de la tabla "users" | ||
Select SUM(age) FROM users; |
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,7 @@ | ||
/* | ||
AVG | ||
Clase 3: https://twitch.tv/videos/1953432950 | ||
*/ | ||
|
||
-- Obitne la media de edad de la tabla "users" | ||
Select AVG(age) FROM users; |
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,7 @@ | ||
/* | ||
IN | ||
Clase 3: https://twitch.tv/videos/1953432950 | ||
*/ | ||
|
||
-- Ordena todos los datos de la tabla "users" con nombre igual a brais y sara | ||
SELECT * FROM users WHERE name IN ('brais', 'sara') |
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,7 @@ | ||
/* | ||
BETWEEN | ||
Clase 3: https://twitch.tv/videos/1953432950 | ||
*/ | ||
|
||
-- Ordena todos los datos de la tabla "users" con edad comprendida entre 20 y 30 | ||
SELECT * FROM users WHERE age BETWEEN 20 AND 30 |
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,16 @@ | ||
/* | ||
ALIAS | ||
Clase 3: https://twitch.tv/videos/1953432950 | ||
*/ | ||
|
||
-- Establece el alias 'Fecha de inicio en programación' a la columna init_date | ||
SELECT name, init_date AS 'Fecha de inicio en programación' FROM users WHERE name = 'Brais' | ||
|
||
-- Consulta igual que la anterior. Representa la posibilidad de usar comillas dobles para cadenas | ||
SELECT name, init_date AS "Fecha de inicio en programación" FROM users WHERE name = "Brais" | ||
|
||
-- Concatena en una sola columa los campos nombre y apellido | ||
SELECT CONCAT('Nombre: ', name, ', Apellidos: ', surname) FROM users | ||
|
||
-- Concatena en una sola columa los campos nombre y apellido y le establece el alias 'Nombre completo' | ||
SELECT CONCAT('Nombre: ', name, ', Apellidos: ', surname) AS 'Nombre completo' FROM users |
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,16 @@ | ||
/* | ||
GROUP BY | ||
Clase 3: https://twitch.tv/videos/1953432950 | ||
*/ | ||
|
||
-- Agrupa los resultados por edad diferente | ||
SELECT MAX(age) FROM users GROUP BY age | ||
|
||
-- Agrupa los resultados por edad diferente y cuenta cuantos registros existen de cada una | ||
SELECT COUNT(age), age FROM users GROUP BY age | ||
|
||
-- Agrupa los resultados por edad diferente, cuenta cuantos registros existen de cada una y los ordena | ||
SELECT COUNT(age), age FROM users GROUP BY age ORDER BY age ASC | ||
|
||
-- Agrupa los resultados por edad diferente mayor de 15, cuenta cuantos registros existen de cada una y los ordena | ||
SELECT COUNT(age), age FROM users WHERE age > 15 GROUP BY age ORDER BY age ASC |
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,7 @@ | ||
/* | ||
HAVING | ||
Clase 3: https://twitch.tv/videos/1953432950 | ||
*/ | ||
|
||
-- Cuenta cuantas filas contienen un dato no nulo en el campo edad de la tabla "users" mayor que 3 | ||
SELECT COUNT(age) FROM users HAVING COUNT(age) > 3 |
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,21 @@ | ||
/* | ||
CASE | ||
Clase 3: https://twitch.tv/videos/1953432950 | ||
*/ | ||
|
||
-- Obtiene todos los datos de la tabla "users" y establece condiciones de visualización de cadenas de texto según el valor de la edad | ||
SELECT *, | ||
CASE | ||
WHEN age > 18 THEN 'Es mayor de edad' | ||
WHEN age = 18 THEN 'Acaba de cumplir la mayoría de edad' | ||
ELSE 'Es menor de edad' | ||
END AS '¿Es mayor de edad?' | ||
FROM users; | ||
|
||
-- Obtiene todos los datos de la tabla "users" y establece condiciones de visualización de valores booleanos según el valor de la edad | ||
SELECT *, | ||
CASE | ||
WHEN age > 17 THEN True | ||
ELSE False | ||
END AS '¿Es mayor de edad?' | ||
FROM users; |
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,13 @@ | ||
/* | ||
INSERT INTO | ||
Clase 3: https://twitch.tv/videos/1953432950 | ||
*/ | ||
|
||
-- Inserta un registro con identificador, nombre y apellido en la tabla "users" | ||
INSERT INTO users (user_id, name, surname) VALUES (8, 'María', 'López') | ||
|
||
-- Inserta un registro con nombre y apellido en la tabla "users" | ||
INSERT INTO users (name, surname) VALUES ('Paco', 'Pérez') | ||
|
||
-- Inserta un registro con identificador no correlativo, nombre y apellido en la tabla "users" | ||
INSERT INTO users (user_id, name, surname) VALUES (11, 'El', 'Merma') |
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,13 @@ | ||
/* | ||
UPDATE | ||
Clase 3: https://twitch.tv/videos/1953432950 | ||
*/ | ||
|
||
-- Estable el valor 21 para la edad del registro de la tabla "users" con identificador igual a 11 | ||
UPDATE users SET age = '21' WHERE user_id = 11 | ||
|
||
-- Estable el valor 20 para la edad del registro de la tabla "users" con identificador igual a 11 | ||
UPDATE users SET age = '20' WHERE user_id = 11 | ||
|
||
-- Estable edad y una fecha para registro de la tabla "users" con identificador igual a 11 | ||
UPDATE users SET age = 20, init_date = '2020-10-12' WHERE user_id = 11 |
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,7 @@ | ||
/* | ||
DELETE | ||
Clase 3: https://twitch.tv/videos/1953432950 | ||
*/ | ||
|
||
-- Elimina el registro de la tabla "users" con identificador igual a 11 | ||
DELETE FROM users WHERE user_id = 11; |