SELECT country,count(city_id) total FROM sakila.country left join city using(country_id) group by country_id having total=0 select country from country left join city using(country_id) where city_id is null select * from film left join film_actor using(film_id) where actor_id is null; select * from film_actor right join film using(film_id) where actor_id is null; select * from actor left join film_actor using(actor_id) where film_id is null SELECT * FROM customer left join rental using(customer_id) where rental_id is null
Ejercicios agrupados
Mostrar la categoría con más películas
Mostrar los cinco clientes que más han gastado
Mostrar los países que tengan menos de 10 clientes
Mostrar los actores que han trabajado en más de 20 películas
Mostrar los actores que han trabajado en 5 o más películas de acción
select name,count(film_id) total from category join film_category using (category_id) join film using (film_id) group by category_id order by total desc limit 1 select first_name, last_name, sum(amount) total from customer join payment using (customer_id) group by customer_id order by total desc limit 5 select country,count(customer_id) total from customer join address using (address_id) join city using (city_id) join country using (country_id) group by country_id having total<10 select first_name,last_name, count(film_id) total from actor join film_actor using (actor_id) join film using (film_id) group by actor_id having total>20 select first_name,last_name, count(film_id) total from actor join film_actor using (actor_id) join film using (film_id) join film_category using (film_id) join category using (category_id) where name='action' group by actor_id having total>=5
Ejemplos where + join
Buscar los países que tengan entre 10 y 20 ciudades que tengan una letra ‘a’
select country, count(city_id) total from country join city using(country_id) where city like '%a%' -- Primero filtro las ciudades con una 'a' y después las cuento group by country_id having total between 10 and 20 -- este filtro se aplica después de contar order by total desc
Ejercicios agrupados
Número de películas para cada actor
Número de clientes por tienda
Total de ventas (amount) por película
Total de ventas(amount) por categoría
SELECT first_name,last_name,count(film_id) total FROM actor join film_actor using(actor_id) group by actor_id select store.store_id, count(customer_id) total from customer join store using(store_id) group by store_id select title,sum(amount) total from film join inventory using (film_id) join rental using (inventory_id) join payment using (rental_id) group by film_id select category.name, sum(amount) total from category join film_category using (category_id) join film using (film_id) join inventory using (film_id) join rental using (inventory_id) join payment using (rental_id) group by category_id -- comentario
Ejemplo Alias
select count(title) total from film f join film_category fc on f.film_id=fc.film_id join category c on fc.category_id = c.category_id where name='Action'
Ejercicios Sakila
Películas de la categoría ‘Action’
Actores que han trabajado en películas de la categoría ‘Action’
Clientes de Argentina
Clientes que hayan alquilado películas de la categoría ‘Action’
select title from film join film_category using (film_id) join category using (category_id) where name='Action' select distinct first_name,last_name from actor join film_actor using(actor_id) join film using(film_id) join film_category using(film_id) join category using(category_id) where name='action' order by first_name, last_name select country,first_name,last_name from country join city on country.country_id=city.country_id join address on city.city_id=address.city_id join customer on address.address_id=customer.address_id where country='argentina' order by first_name, last_name select distinct first_name, last_name from customer join rental using (customer_id) join inventory using (inventory_id) join film using (film_id) join film_category using (film_id) join category using (category_id) where name='Action' select first_name, last_name from country join city using (country_id) join address using (city_id) join customer using (address_id) join rental using (customer_id) join inventory using (inventory_id) join film using (film_id) join film_category using (film_id) join category using (category_id) where name='Action' and country='argentina'
Menú hamburguesa y organizar presentaciones
Ejercicios Sakila sql
Actores que tengan una x en el nombre o en el apellido
Direcciones de california que tengan ‘274’ en el número de teléfono
Películas ‘Épicas’ (Epic) o ‘Brillantes’ (brilliant) que duren más de 180 minutos
Películas que duren entre 100 y 120 minutos o entre 50 y 70 minutos
Películas que cuesten 0.99, 2.99 y tengan un rating ‘g’ o ‘r’ y que hablen de cocodrilos (cocodrile)
Direcciones de ontario o de punjab o que su código postal acabe en 5 o que su teléfono acabe en 5
SELECT * FROM sakila.actor
where first_name like '%x%' or last_name like '%x%'
SELECT * FROM sakila.address
where district='california' and phone like '%274%'
SELECT * FROM sakila.film
where (description like '%epic%' or description like '%brilliant%')
and length>180
SELECT * FROM sakila.film
where length between 100 and 120 or length between 50 and 70
SELECT * FROM sakila.film
where description like '%crocodile%' and rental_rate in (0.99,2.99)
and rating in ('R','G')
SELECT * FROM sakila.address
where district='ontario' or district='punjab'
or postal_code like '%5' or phone like '%5'
Ejercicio tablas
Si tenemos la tabla ‘productos’ del examen con los siguientes campos:
Referencia varchar
Precio decimal
Stock decimal
Insertar los siguientes registros:
Disolvente, 1 €, 2000
Emulsionante, 3 €, 1500
Colorante rojo, 4 €, 200
Tinta verde, 10 €, 100
Después cambiar el stock del colorante rojo a 300 y de la tinta verde a 50
Eliminar el disolvente
Mostrar los registros.
CRUD en Mysql
INSERT INTO table_name ( field1, field2,...fieldN ) VALUES ( value1, value2,...valueN );
UPDATE table_name SET field1 = new-value1, field2 = new-value2 [WHERE Clause]
DELETE FROM table_name [WHERE Clause]
SELECT field1, field2,...fieldN FROM table_name1, table_name2... [WHERE Clause]
[WHERE condition1 [AND [OR]] condition2.....