Sentencias sql

Clientes que se llaman igual que actores de películas

SELECT distinct concat(c.first_name, " ", c.last_name) cliente, 
concat(a.first_name, " ", a.last_name) as actor
FROM actor a join
film_actor using (actor_id)
join film using (film_id)
join inventory using (film_id)
join rental using (inventory_id)
join customer c using (customer_id)
where a.first_name=c.first_name
order by cliente

Películas por categoría:

select name, count(title)
from category join film_category using(category_id)
join film using (film_id)
group by name

Clientes por país:

SELECT country, count(customer_id) FROM sakila.country
left join city using (country_id)
left join address using (city_id)
left join customer using (address_id)
group by country;

Clientes que se apellidan como el empleado que los atiende:

select distinct concat(c.first_name, " ", c.last_name) cliente, 
concat(s.first_name, " ", s.last_name) as empleado
from customer c join rental using (customer_id)
join staff s using (staff_id)
where c.last_name=s.last_name

Clientes que viven en el mismo código postal que los empleados:

select distinct concat(c.first_name, " ", c.last_name) cliente, 
concat(s.first_name, " ", s.last_name) as empleado
from customer c join address a1 on c.address_id=a1.address_id
join address a2 on a1.postal_code=a2.postal_code
join staff s on a2.address_id=s.address_id

Películas con importe total de alquiler menor que 2:

select title, avg(ifnull(amount,0)) total
from film left join inventory using (film_id)
left join rental using (inventory_id)
left join payment using (rental_id)
group by title
having total<2
order by 2 

Total de clientes por países

select country, count(customer_id)
from country join city using (country_id)
join address using (city_id)
join customer using (address_id)
group by country

Publicado por

Juan Pablo Fuentes

Formador de programación y bases de datos