No tienes acceso a esta clase

¬°Contin√ļa aprendiendo! √önete y comienza a potenciar tu carrera

Curso Pr√°ctico de SQL

Curso Pr√°ctico de SQL

Israel V√°zquez Morales

Israel V√°zquez Morales

El primero

10/29
Recursos

Aportes 497

Preguntas 34

Ordenar por:

¬ŅQuieres ver m√°s aportes, preguntas y respuestas de la comunidad?

Estas fueron todas las que se me ocurrieron. Y eso que antes le tenía un mieeedo a SQL. Ahora lo veo tan simple y hermoso

SELECT *
FROM platzi.alumnos AS alumnos
FETCH FIRST 5 ROWS ONLY;

SELECT *
FROM platzi.alumnos
LIMIT 5;

SELECT *
FROM (
	SELECT ROW_NUMBER () OVER() AS row_id, *
	FROM platzi.alumnos
 )AS alumnos_with_row_nums
 WHERE row_id < 6
;

SELECT *
FROM (
	SELECT ROW_NUMBER () OVER() AS row_id, *
	FROM platzi.alumnos
 )AS alumnos_with_row_nums
 WHERE row_id <=5
;

SELECT *
FROM (
	SELECT ROW_NUMBER () OVER() AS row_id, *
	FROM platzi.alumnos
 )AS alumnos_with_row_nums
 WHERE row_id BETWEEN 1 and 5
;

Me parece que complic√≥ DEMASIADO las cosas. Por qu√© incluir Window Functions en las primeras consultas del curso? No veo la necesidad de complicar tanto ese √ļltimo query para el tema que se est√° explicando.

El punto bueno de esto es que lo investigué por otro lado y ya lo tengo claro. Yo lo veo como un reto, pero para alguien con poco conocimiento de SQL se puede enredar y desmotivar un montón.

Resolución del RETO
Primera forma:

Segunda forma:

Tercera forma:

SELECT *
FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY;

SELECT *
FROM platzi.alumnos
LIMIT 5;

SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id,*
	FROM platzi.alumnos
	-- row funcion 
) AS alumnos_with_row_num
WHERE row_id <= 5;

Para todas aquellas personas que están usando mysql el * debe estar después de select así:
SELECT *
FROM (
SELECT * , row_number() over() as row_id
FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id = 4
;

imporante separar con la coma para definir la función row_id y finalmente buscarla la 4 columna

Sugerencia: para agregar comentarios utilizar guion guion
-- Comentarios

Ahora_si_se_viene_lo_chido.jpg

Extrayendo los primeros 5 registros de la tabla

Usando FETCH

SELECT * FROM platzi.alumnos FETCH FIRST 5 ROWS ONLY;

Usando LIMIT

SELECT * FROM platzi.alumnos LIMIT 5;

Window Function

SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_id
FETCH FIRST 5 ROWS ONLY

Una p√°gina para practicar SQL de manera did√°ctica es: https://mystery.knightlab.com

limit = top en SQL server

Buenas noches, este es mi aporte para solucionar el ejercicio.

SELECT * FROM alumnos WHERE id >= 1 and id < 6;

SELECT * FROM alumnos WHERE id between 1 and 5;

SELECT TOP (5) * FROM alumnos ORDER BY id;

SELECT * FROM alumnos ORDER BY id OFFSET 0 ROWS  FETCH NEXT 5 ROWS ONLY;```

Para conocer las window functions.

Query en MySql

SELECT *
FROM (
	SELECT *, ROW_NUMBER() OVER() AS row_id
    FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id = 1
;

Las formas que use como respuesta al reto

--formas de llamar datos, en este caso los 5 primeros de la tabla

SELECT *
FROM platzi.alumnos
LIMIT 1;

-- aca se utiliza la forma de windows function 
SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id BETWEEN 1 AND 5;
;


SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
LIMIT 5;


SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
FETCH FIRST 5 ROWS ONLY;


SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id <6;


SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id <=5;


SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id IN (1,2,3,4,5);
Reto completado: ![](https://static.platzi.com/media/user_upload/Screenshot%202024-04-17%2012.08.53-eacf1d7f-2c43-412b-bc9e-1a215aba4c34.jpg)

Aggregate functions vs Window functions

reto

SELECT *
FROM platzi.alumnos
FETCH FIRST 20 ROWS ONLY;

SELECT *
FROM platzi.alumnos
LIMIT 30;

SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS lista_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE lista_id > 99;

Estas son las 12 formas distintas que pude encontrar para darle solución al problema del final de la clase:

SELECT *
FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY;

SELECT *
FROM platzi.alumnos
LIMIT 5;

SELECT *
FROM platzi.alumnos
WHERE id <= 5;

SELECT *
FROM platzi.alumnos
WHERE id < 6;

SELECT *
FROM platzi.alumnos
WHERE id IN (1,2,3,4,5);

SELECT *
FROM platzi.alumnos
WHERE id BETWEEN 1 AND 5;

SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id <= 5;

SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id < 6;

SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id IN (1,2,3,4,5);

SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
FETCH FIRST 5 ROWS ONLY;

SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id BETWEEN 1 AND 5;

SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
LIMIT 5;

la clase empieza en el minuto 4:40

1)
SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id BETWEEN 1 AND 5;

2)
SELECT *
FROM platzi.alumnos
LIMIT 5;

3)
SELECT *
FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY;

En mi concepto entre menos código se escriba para encontrar lo que buscamos es mucho mejor. De todas las maneras la mas facil para usarla en muchas situaciones es el BETWEEN.

Resolución del primer reto

SELECT *
FROM platzi.alumnos		
WHERE id < 6;
--------------------------
SELECT *
FROM platzi.alumnos		
LIMIT 5;
--------------------------
SELECT *
FROM platzi.alumnos		
FETCH FIRST 5 ROW ONLY;
--------------------------
SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id BETWEEN 1 and 5;

Primer reto.

SELECT *
FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY;

SELECT *
FROM platzi.alumnos
LIMIT 5;

SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
)AS alumnos_whith_row_num
WHERE row_id < 6;

SELECT *
FROM platzi.alumnos
WHERE id <= 5;

SELECT *
FROM platzi.alumnos
WHERE id BETWEEN 1 AND 5;

#RETO DE LA CLASE: Los primeeros 5 registros usando todos los meetodos
1.
SELECT *
FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY;

SELECT *
FROM platzi. alumnos
LIMIT 5

SELECT *
FROM (
SELECT ROW_NUMBER() OVER() AS row_id, *
FROM platzi.alumnos
) AS alumnos_rows
WHERE row_id<=5
;

Mis respuestas al RETO

Primeros 5 registros de la tabla usando fetch,limit y window function:

select * from platzi.alumnos
limit 5;
-------
select * from platzi.alumnos
fetch first 5 rows only
----
select * from(
	select row_number() over() as row_id, *
	from platzi.alumnos
) as alumnos_with_row_num
where row_id in (1,2,3,4,5)

Me propuse hacer un reto algo distinto y usando las FUNCIONES VENTANAS para poder entenderlas:

  1. OVER()
  2. PARTITION BY()
    La consulta que pensé y logré ejecutar fue contar el total de estudiantes que pagan cierto precio de COLEGIATURA POR EJEMPLO si quisieramos saber cuantos ESTUDIANTES pagan 5000 en su colegiatura el resultado sería igual a 125 estudiantes en total que pagan 5000 en su colegiatura, Aqui comparto el código SQL del query anterior espero les sirva de algo salu2! :

Traer el primer registro de una tabla

  • FETCH FIRST 1 ROWS ONLY;
  • LIMIT 1;
  • Con una subtabla que utiliza una Window Function
/// RETO ///
OPCION 1: FETCH

SELECT *
FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY;
OPCION 2: LIMIT

SELECT *
FROM platzi.alumnos
LIMIT 5;
OPCION 3: WINDOW FUNCTION

SELECT * 
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
	) AS platzi_row_numbers
WHERE row_id <= 5

Me encantó y voy a seguir metiéndole a SQL.

El primer ejercicio lo hice de la forma más sencilla y veloz que se me ocurrió:

SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) 
AS alumnos_with_row_number LIMIT 10;

Opción 1

SELECT *
FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY;

Opción 2

SELECT *
FROM platzi.alumnos
LIMIT 5;

Opción 3

SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
LIMIT 5;

Se me hizo muy interesante lo de ventanas, hasta ahora no lo había entendido muy bien en la uni.

SELECT *
FROM platzi.alumnos
LIMIT 5;

SELECT *
FROM platzi.alumnos
FETCH FIRST 6 ROWS ONLY;

SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() as row_id, *
	FROM platzi.alumnos
) AS alumnos_con_numero_fila 
WHERE row_id BETWEEN 1 AND 5;
SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id BETWEEN 1 AND 5;

SELECT *
FROM platzi.alumnos
LIMIT 5;

SELECT *
FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY;```
SELECT * 
FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY;

SELECT * 
FROM platzi.alumnos
LIMIT 5;

SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id <= 5;```
select * from platzi.alumnos a 
fetch first 5 rows only

select * from platzi.alumnos a 
limit 5

select * 
from (
	select row_number() over() as row_id, *
	from platzi.alumnos a 
) as alumnos_with_row_num
where row_id < 6
SELECT *
FROM platzi.alumnos
LIMIT 5;

SELECT *
FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY;

SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id BETWEEN 1
	AND 5;```
Nunca vi donde crearon el esquma 'Platzi',
La verdad que siempre x una cosa u otra no puedo terminar los cursos. En este caso no se donde poner los archivos que supuestamente estaban en la instalación pero que entonces no los pude abrir y ahora no accedo a la base Ptatzi, so no pued seguir el curso
Opción 1 : SELECT \* FROM platzi.alumnos LIMIT 5 Opción 2 : SELECT \* FROM platzi.alumnos FETCH FIRST 5 ROWS ONLY Opción 3 : SELECT \* FROM (SELECT ROW\_NUMBER() OVER () AS row\_id, \* FROM platzi.alumnos) AS alumnos\_with\_row\_number WHERE row\_id between 1 and 5
```js --Traer el primer registro SELECT * FROM platzi.alumnos FETCH FIRST 5 ROWS ONLY SELECT * FROM platzi.alumnos LIMIT 5; --Window funtion SELECT * FROM ( SELECT ROW_NUMBER() OVER() AS row_id, * FROM platzi.alumnos )AS alumnos_whit_row_number where row_id <= 5 ```--Traer el primer registro SELECT \* FROM platzi.alumnos FETCH FIRST 5 ROWS ONLY SELECT \* FROM platzi.alumnos LIMIT 5; \--Window funtion SELECT \* FROM ( SELECT ROW\_NUMBER() OVER() AS row\_id, \* FROM platzi.alumnos )AS alumnos\_whit\_row\_number where row\_id <= 5
-- Modo uno
SELECT *
FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY;

-- Modo dos
SELECT *
FROM platzi.alumnos
LIMIT 5;

-- Modo tres
SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, * 
	FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id < 6;
Estoy utilizando SQL SERVER `SELECT TOP 5 * FROM pol.tbPolizas` `SELECT top 5 * FROM (` `SELECT ROW_NUMBER() OVER(ORDER BY NoPoliza DESC) AS row_id, * from POL.tbPolizas` `) AS poli` `WHERE row_id = 5`
Comparto 4 formas. `SELECT *` `FROM platzi.alumnos` `FETCH FIRST 5 ROWS ONLY;` `SELECT * ` `FROM platzi.alumnos` `LIMIT 5;` `SELECT * ` `FROM (` ` ``SELECT ROW_NUMBER() OVER() AS row_id, *` ` ``FROM platzi.alumnos ` `)AS`` `` alumnos_with_row_number` `WHERE row_id <= 5;` `SELECT * ` `FROM (` ` ``SELECT ROW_NUMBER() OVER() AS row_id, *` ` ``FROM platzi.alumnos ` `)AS`` `` alumnos_with_row_number` `WHERE row_id between 1 AND 5;`
\-- 1ra Forma -- SELECT \* FROM platzi.alumnos FETCH FIRST 5 ROW ONLY; \-- 2da Forma -- SELECT \* FROM platzi.alumnos LIMIT 5; \-- 3ra Forma -- SELECT \* FROM ( SELECT ROW\_NUMBER() OVER () as row\_id,\* FROM platzi.alumnos ) WHERE row\_id between 1 and 5
![](https://static.platzi.com/media/user_upload/image-787e10e7-5959-4e97-9e23-bdab86bb663b.jpg)
Mi respuesta:![](https://static.platzi.com/media/user_upload/image-60b3a0f1-e1cd-4e87-88ae-41277a3c2604.jpg)
Otra forma, solo para demostrar que hay muchas maneras, como dice el profesor. ```js SELECT * FROM ( SELECT ROW_NUMBER() OVER() AS row_id, * FROM platzi.alumnos ) AS alumnos_with_row_num WHERE row_id IN ( SELECT * FROM generate_series(1, 5) ); ```Usando IN, una subquery y la función generate\_series, que crea rangos.

Estuve volando un poco al inicio porque me distraje en el video 9 y lo volví a reproducir, luego el 10 nuevamente y pude comprender mejor los conceptos. Les dejo mis apuntes y el Reto, SQL es hermoso.

Apuntes:

/**
  * Primer row de la tabla
  */

/*Usando FETCH*/
SELECT *
FROM platzi.alumnos
FETCH FIRST 1 ROWS ONLY;

/*Usando LIMIT*/
SELECT *
FROM platzi.alumnos
LIMIT 1;

/*Con Windows Function*/
SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id = 1;

RETO:

/**RETO
	*Extraer 5 primeros registros con los 3 métodos
*/

/*Con FETCH*/
SELECT *
FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY;

/*Con LIMIT*/
SELECT *
FROM platzi.alumnos
LIMIT 5;

/*Con Windows Function*/
SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
)	AS alumnos_with_row_num
WHERE row_id <6;

/*Con OFFSET y LIMIT*/
SELECT *
FROM (
    SELECT ROW_NUMBER() OVER() AS row_id, *
    FROM platzi.alumnos
) AS alumnos_with_row_num
ORDER BY row_id
OFFSET 0
LIMIT 5;

/*Usando OFFSET y LIMIT con ordenamiento descendente*/
SELECT *
FROM (
    SELECT ROW_NUMBER() OVER() AS row_id, *
    FROM platzi.alumnos
    ORDER BY row_id DESC
) AS alumnos_with_row_num
OFFSET 995
LIMIT 5;
 

Reto terminado

-- With FETCH
SELECT * FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY;

-- With LIMIT
SELECT * FROM platzi.alumnos
LIMIT 5;

-- With window function
SELECT * 
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_number
LIMIT 5;
select \* from platzi.alumnos fetch first 5 rows only; select \* from ( select row\_number() over() as id\_columna, \* from platzi.alumnos) as alumnos\_concolumnas limit 5;
SELECT \* FROM ( SELECT ROW\_NUMBER() OVER() AS row\_id, \* FROM platzi.alumnos ) AS alumnos\_ith\_row\_num WHERE row\_id between 0 and 5;

A quienes les interese hacer inserción de datos masivos desde un archivo .sql desde terminal.

psql -U postgres -W -h localhost nombre_base < dump_base.sql
  1. -U Se refiere al Usuario, en nuestro ejemplo usamos el usuario: postgres.
  2. -W Con este par√°metro conseguiremos que nos solicite el password del usuario antes especificado.
  3. -h Con este indicamos cu√°l es el servidor PostgreSQL al que nos conectaremos para importar nuestro dump, si estamos en el mismo servidor podemos colocar localhost, si ser√° un servidor remoto colocaremos la IP.
  4. nombre_base Este es el ultimo par√°metro en nuestra linea del comando, el cual hace referencia al nombre de la base de datos a la que importaremos nuestro dump.
  5. < dump_base.sql Indicamos cual es el archivo que contiene el dump de la base y que por tanto queremos importar.

Para listar las tablas dentro del schema platzi hacemos lo siguiente:

pruebas=# \dt platzi.*

# Listar todos los schemas

pruebas=# \dt *.*

Espero les sea de utilidad.

Por medio de los cursores de sql se puede obtener otra solucion

comparto mi codigo con el cursor

--haciendo uso de un cursor
DECLARE @Contador INT = 0; -- Variable contador para controlar el n√ļmero de filas recorridas

DECLARE CursorFilas CURSOR 
FOR SELECT * FROM platzi.alumnos;

OPEN CursorFilas;

FETCH NEXT FROM CursorFilas

WHILE @@FETCH_STATUS = 0 AND @Contador < 4
BEGIN
    -- Incrementa el contador
    SET @Contador = @Contador + 1;

    -- Obtiene la siguiente fila
    FETCH NEXT FROM CursorFilas
END

CLOSE CursorFilas;
DEALLOCATE CursorFilas;

Dejo mi tarea de la clase

SELECT * 
FROM platzi.alumnos
limit 5;

SELECT *
FROM platzi.alumnos
FETCH first 5 rows only;

SELECT *
FROM (
SELECT ROW_NUMBER () OVER () AS row_id, * FROM platzi.alumnos
) as alumnos_rows
WHERE row_id <6;
```js ``` ````js SELECT * FROM platzi.alumnos FETCH FIRST 5 ROW ONLY SELECT * FROM platzi.alumnos LIMIT 5; SELECT * FROM ( SELECT ROW_NUMBER () OVER() AS row_id, * FROM platzi.alumnos )AS alumnos_with_row_nums WHERE row_id <= 5 ; ```SELECT \* FROM platzi.alumnos FETCH FIRST 5 ROW ONLY SELECT \* FROM platzi.alumnos LIMIT 5; SELECT \* FROM ( SELECT ROW\_NUMBER () OVER() AS row\_id, \* FROM platzi.alumnos )AS alumnos\_with\_row\_nums WHERE row\_id <= 5 ; ````
Primera forma: ![](https://static.platzi.com/media/user_upload/image-a425f70d-93c8-4b05-af95-740eab11119c.jpg) Segunda forma: ![](https://static.platzi.com/media/user_upload/image-c6e34d9a-1410-46a1-86a4-60dfe4bf279d.jpg) Tercera forma: ![](https://static.platzi.com/media/user_upload/image-4e290b0f-cfcd-4825-a333-8cbfd31bcafe.jpg)
Las consultas que realice para la solución del ejercicio. `SELECT * FROM platzi.alumnos` `FETCH FIRST 5 ROW ONLY;` `SELECT * FROM platzi.alumnos` `LIMIT 5;` `SELECT *` `FROM (` ` ``SELECT ROW_NUMBER() OVER() as row_id, * ` ` ``FROM platzi.alumnos` ` ``) as alumnos_with_row_num ` ` ``WHERE row_id BETWEEN 1 AND 5;` ` ` ` ``SELECT *` `FROM (` ` ``SELECT ROW_NUMBER() OVER() as row_id, * ` ` ``FROM platzi.alumnos` ` ``) as alumnos_with_row_num ` ` ``WHERE row_id < 6;` ` ` ` ``SELECT *` `FROM (` ` ``SELECT ROW_NUMBER() OVER() as row_id, * ` ` ``FROM platzi.alumnos` ` ``) as alumnos_with_row_num ` ` ``WHERE row_id = 5;`

las primeras 10 filas de la tabla alumnos, puedes usar la cl√°usula LIMIT:

SELECT *
FROM platzi.alumnos
LIMIT 10;
SELECT \* FROM (SELECT ROW\_NUMBER () OVER () AS row\_id, \* FROM platzi.alumnos) AS alunmos\_with\_row\_num WHERE row\_id <=5 ;

SELECT *
FROM(
select row_number() over() as row_id,*
from platzi.alumnos
) as alumnos_with_row_num
where row_id in (1,2,3,4,5)

SELECT *
FROM(
select row_number() over() as row_id,*
from platzi.alumnos
) as alumnos_with_row_num
where row_id<=5

Yo hice estos:

select * from platzi.alumnos

fetch first 5 row only ;

select * from platzi.alumnos
as nice where id < 6 ;

SELECT *
FROM (
	SELECT ROW_NUMBER () OVER() AS row_id, *
	FROM platzi.alumnos
 )AS alumnos_with_row_nums
 WHERE row_id BETWEEN 1 and 5
;
Estoy haciendo este curso con MariaDB, la sintaxis cambia un poco, pero esta es la forma en la me funciono el ejercicio: ```css select * from ( select row_number() over() as row_id, a.* from platzi.alumnos as a join ( select row_number() over() as dummy_row_id from platzi.alumnos ) as dummy on 1 = 1 ) as result where row_id = 10; ```
Agregue uno utilizando el id, usando HAVING, GROUP BY y ORDER BY. `SELECT * FROM platzi.alumnos LIMIT 5;` `SELECT * FROM platzi.alumnos FETCH NEXT 5 ROWS ONLY;` `SELECT * FROM (SELECT ROW_NUMBER() OVER() AS NUM, * FROM platzi.alumnos) AS tab WHERE NUM >= 1 AND NUM <= 5;` `SELECT id, nombre, apellido, email FROM platzi.alumnos GROUP BY nombre, apellido, email, id ` `HAVING id > 0 AND id < 6 ORDER BY id;`

Este, en mi opini√≥n, puede ser √ļtil en otras ocasiones, por ejemplo, si tenemos una lista espec√≠fica que queramos buscar

SELECT *
FROM(
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
	) AS alumnos_with_rows_num
WHERE row_id IN (1,2,3,4,5)
;
```js ```
\--5 PRIMEROS SELECT\* FROM PLATZI.alumnos FETCH FIRST 5 ROWS ONLY; SELECT\* FROM PLATZI.alumnos LIMIT 5 SELECT\* FROM( SELECT ROW\_NUMBER() OVER() AS row\_id,\* FROM PLATZI.alumnos ) alumnos\_with\_row\_id WHERE ROW\_ID <=5

Solución al reto

Mi solucion. *SELECT \* FROM platzi.alumnos FETCH FIRST 5 ROWS ONLY;* *SELECT \* FROM platzi.alumnos LIMIT 5;* *SELECT \* FROM (SELECT ROW\_NUMBER() OVER() AS row\_id, \* FROM platzi.alumnos) AS alumnos\_with\_row\_num WHERE row\_id < 6;*
Que buen ejercicio y eso es solo el primero, muchas gracias

Es mi segunda vez tomando este curso. Ahora estoy más que decidida a no dejar esto y que se convierta en mi profesión!

select \* from platzi.alumnos fetch first 5 rows only; select \* from platzi.alumnos limit 5; select \* from ( select row\_number() over() as row\_id, \* from platzi.alumnos ) as alumnos\_with\_row\_num where row\_id between 1 and 5;

// Primera forma con fetch
SELECT *
FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY ;

//Segunda forma con limit
SELECT *
FROM platzi.alumnos
LIMIT 5 ;

//Tercera forma con subquery
SELECT *
FROM (
SELECT ROW_NUMBER() OVER() AS row_id, *
FROM platzi.alumnos
) AS alumnos_with_rows_num
WHERE row_id <= 5;

Hola comparto mis consultas.
Primer ejercicio

Segundo ejercicio

Tercer ejercicio

Cuarto ejercicio

Una opción más:

Resolucion del reto:

Mis aportes:
windows funtion

SELECT *
FROM (
SELECT ROW_NUMBER() OVER() AS row_id, *
FROM platzi.alumnos
)AS alumnos_witch_row_num
WHERE row_id <= 5
;

Limit
SELECT *
FROM platzi.alumnos
LIMIT 5
;

fetch
SELECT *
FROM platzi.alumnos
FETCH FIRST 5 rows only
;

SELECT *
FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY;
SELECT *
FROM (
  SELECT ROW_NUMBER() OVER() AS row_id, *
  FROM platzi.alumnos
) AS alumnos_with_row_num
LIMIT 5;
SELECT *
FROM platzi.alumnos
LIMIT 5; 

-- Muchas formas de traernos los 5 primeros registro de la tabla alumnos

SELECT * --1
FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY;


SELECT * --2
FROM platzi.alumnos
LIMIT 5;


SELECT * --3
FROM platzi.alumnos
WHERE id <= 5;


SELECT * --4
FROM platzi.alumnos
WHERE id <= (
    SELECT id
    FROM platzi.alumnos
    WHERE id = 5
);


SELECT * --5
FROM platzi.alumnos
WHERE id BETWEEN 1 AND 5;


SELECT * --6
FROM platzi.alumnos
WHERE id IN (1,2,3,4,5);


-- Usando Window Functions
SELECT * --7
FROM (
    SELECT ROW_NUMBER() OVER() AS row_id, *
    FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id <= 5
;

select * from platzi.alumnos
limit 5;

select * from platzi.alumnos
fetch first 5 rows only;

select * from ( select row_number () over() as row_id, * from platzi.alumnos) as alumnos_with_rows_nums
where row_id <=5;

select * from ( select row_number () over() as row_id, * from platzi.alumnos) as alumnos_with_rows_nums
where row_id between 1 and 5;

Este es el tercer reto, para traer los primeros 5 registros de la tabla

SELECT *
FROM platzi.alumnos
LIMIT 5;

SELECT *
FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY;

SELECT *
FROM(
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumos_with_row_num
LIMIT 5;

Empleando una CTE (Expresi√≥n de Tabla Com√ļn) con ROW_NUMBER():

WITH numbered_alumnos AS (
    SELECT ROW_NUMBER() OVER() AS row_id, *
    FROM platzi.alumnos
)

SELECT *
FROM numbered_alumnos
WHERE row_id <= 5;```

Esta es mi idea de como realizar el Reto

-- Con FETCH
SELECT *
FROM platzi.alumnos
FETCH FIRST 5 ROW ONLY;

-- Con LIMIT
SELECT *
FROM platzi.alumnos
LIMIT 5;

--Con Windows Function
SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id < 6;

Estas son las instruccionnes que validan los primeros 5 valores de la base de datos de Platzi en la tabla alumnos:

SELECT *
FROM platzi.alumnos
WHERE id < 6; -- Limitamos con "condicion" que el valor de columna en sus row id sea menor de 6

SELECT *
FROM platzi.alumnos
WHERE id > 0 AND id < 6; -- En este caso usamos limitadores superiores e inferiores

SELECT *
FROM platzi.alumnos
WHERE id BETWEEN 0 AND 5; -- Ac√° hacemos un intervalo con los valores maximo y minimo

SELECT *
FROM platzi.alumnos
LIMIT 5;  -- Ac√° limitamos la consulta a los primeros 5 rows

SELECT *
FROM platzi.alumnos
OFFSET 0
LIMIT 5;  -- Usando OFFSET con LIMIT mostramos los primeros 5 rows de la tabla

SELECT *
FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY; -- Usando FETCH para las primeras 5 rows

SELECT *
FROM platzi.alumnos
OFFSET 10
FETCH NEXT 15 ROWS ONLY; -- Usando FETCH NEXT con OFFSET seleccionamos el intervalo deseado

SELECT *
FROM (
	SELECT ROW_NUMBER() OVER(ORDER BY id) AS row_idj, *
	FROM platzi.alumnos
)AS first_5_alum
WHERE row_idj <6; -- Usando la window function con ORDER BY y obtenemos los primeros 5 rows

Por ahora, vamos bien ūüėé

Estaria bueno que no las haga el profe y nos deje el reto antes de arrancar el video

Solución al reto

  1. Fetch
SELECT *
FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY;
  1. Limit
SELECT *
FROM platzi.alumnos
LIMIT 5;
  1. Window function
SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id < 6;

Esta fue mi soluci√≥n al reto #1 ūüėÉ

SELECT *
FROM (
SELECT ROW_NUMBER () OVER() AS row_id,*
FROM platzi.alumnos
) AS alumnos_with_row_num
;
SELECT *
FROM platzi.alumnos
LIMIT 5;

Mis soluciones al reto:

Solución 1:

SELECT * FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY;

Solución 2:

SELECT *
FROM (
SELECT ROW_NUMBER () OVER () AS row_id, *
FROM platzi.alumnos
) AS alumnos_with_row_num
LIMIT 5;
;

Hola. Comparto de que manera realice el reto

--Ejercicio ROWS
--1
SELECT *
FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY;
--2
SELECT *
FROM platzi.alumnos
LIMIT 5;
--3
SELECT *
FROM (
	SELECT ROW_NUMBER () OVER (order by id ASC ) AS row_id, *
	FROM platzi.alumnos FETCH FIRST 5 ROWS ONLY
 ) AS alumnos_with_row_num;

No es mucho pero es trabajo honesto xd

<SELECT * 
FROM platzi.alumnos
FETCH NEXT 5 ROWS ONLY;

SELECT * 
FROM platzi.alumnos
LIMIT 5;

SELECT * 
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos) AS first_five_rows
LIMIT 5;> 

Estas fueron mis query

<SELECT *
FROM platzi.alumnos AS alumnos
FETCH FIRST 5 ROWS ONLY;

SELECT *
FROM platzi.alumnos AS alumnos
limit 5;

SELECT *
FROM (
	SELECT ROW_NUMBER() OVER () AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id <6;

SELECT *
FROM (
	SELECT ROW_NUMBER () OVER() AS row_id, *
	FROM platzi.alumnos
 )AS alumnos_with_row_nums
 WHERE row_id BETWEEN 1 and 5
;> 

mis soluciones:

SELECT *
FROM PLATZI.ALUMNOS FETCH FIRST 5 ROWS ONLY;

SELECT *
FROM PLATZI.ALUMNOS
LIMIT 5;

SELECT *
FROM
	(SELECT ROW_NUMBER() OVER() AS ROW_ID, *
		FROM PLATZI.ALUMNOS) AS ALUMNOS_WITH_ROW_NUMBER
WHERE ROW_ID <= 5;

Ejercicio con SQL Server

-- LIMIT
SELECT TOP(@Row_Number) * FROM Platzi.Alumnos ORDER BY Nombre

-- FETCH
SELECT TOP(5) * FROM Platzi.Alumnos ORDER BY Nombre 
SET ROWCOUNT @Row_Number;

-- SUBQUERY
SELECT * 
FROM	(
			SELECT ROW_NUMBER() OVER(ORDER BY Nombre) AS Row_Id, * 
			FROM Platzi.Alumnos
			
		) AS AlumnosWithRowNumber WHERE Row_Id > 0 and Row_Id <= @Row_Number

respuesta FETCH: SELECT *
FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY;

respuesta LIMIT:

SELECT *
FROM platzi.alumnos
LIMIT 5;

respuesta WINDOW F:

SELECT *
FROM (
SELECT ROW_NUMBER() OVER() AS row_id, *
FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id <= 5;

Mis primeras pruebas:

  • Desplegar el primer registro.

select *
from platzi.alumnos
limit 1;

  • La variante del profesor:

select *
from platzi.alumnos
Fetch first 1 ROWS ONLY;

  • El otro query ejecutado por el profesor.

SELECT *
FROM (
SELECT ROW_NUMBER() over() AS row_id, *
FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id = 10;

En mysql existen se me ocurrió esta formas de hacerlo:
1.

select * from alumnos limit 5;
select * from alumnos limit 5 offset 0;
  1. Esta opción es una alternativa a OFFSET en la cual el primer numero referencia la offset y el segundo a la cantidad de tuplas que recibiremos
select * from alumnos limit 0,5;

Ey Eyy, ¬ŅQui√©n registr√≥ a Leanna sin su apellido?, ¬°Despedido! >:v

¬°Qu√© nervios!, ya estoy ansioso de decirlo en mi trabajo ūüėĄ

10. El primero

SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos

)AS alumnos_with_row_num
WHERE row_id = 1

;

-- RETO

SELECT *
FROM platzi.alumnos
LIMIT 5;

SELECT *
FROM (
    SELECT ROW_NUMBER() OVER() AS row_id, *
    FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id <= 5;

SELECT *
FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY;

SELECT *
FROM platzi.alumnos
OFFSET 0
FETCH NEXT 5 ROWS ONLY;
SELECT *
FROM platzi.alumnos
FETCH FIRST 5 ROWS ONLY;

--2ND 

SELECT *
FROM platzi.alumnos
LIMIT 5;

--3RD 

SELECT *
FROM (
	SELECT ROW_NUMBER() OVER() AS row_id, *
	FROM platzi.alumnos
) AS alumnos_with_row_num
WHERE row_id between 1 and 5;

mi aporte

SELECT *
FROM platzi.alumnos
FETCH FIRST 5 ROW ONLY;

SELECT *
FROM platzi.alumnos
LIMIT 5;

SELECT *
FROM (
SELECT ROW_NUMBER() OVER () AS row_id, *
FROM platzi.alumnos)
AS alumnos_with_row_num
WHERE row_id <= 10