Ejemplos sentencias sql

Actores con más de 10 películas

select first_name, last_name, count(film_id) peliculas from
actor join film_actor using (actor_id)
group by first_name,last_name
having peliculas>30

Países con más de 10 ciudades

select country, count(city) cities
from country join city using (country_id)
group by country
having cities>10

Los diez países que más gastan

select country, sum(amount) total, avg(amount) media
from country join city using (country_id)
join address using (city_id)
join customer using (address_id)
join payment using (customer_id)
group by country
order by total desc
limit 0,10

El actor con más películas

select first_name, last_name, count(film_id) peliculas
from actor join film_actor using(actor_id)
group by first_name,last_name
order by peliculas desc
limit 1

Ejercicios SQL

Actores con ‘ll’ en el apellido o en el nombre
Actores con id>=100 y menor igual que 200

Una de las dos anteriores o las dos
Insertar actor ‘Juan’ ‘Pérez’
Insertar actor ‘Rosa’ ‘Pi’

Actualizar el nombre del actor con id 10 a ‘PEPE’

Actualizar el nombre de los actores con el id > 200 a ‘Actor’ + id

Borrar los actores con id > 200

select * from sakila.actor
where first_name like '%ll%' or last_name like '%ll%';

select * from sakila.actor
where actor_id between 100 and 200;

select * from sakila.actor
where first_name like '%ll%' or last_name like '%ll%' or actor_id between 100 and 200;

insert into sakila.actor (first_name,last_name)
values ('Juan','Pérez');

insert into sakila.actor (first_name,last_name)
values ('Rosa','Pi');

update sakila.actor 
set first_name='PEPE'
where actor_id=10;

update sakila.actor 
set first_name=concat('Actor',actor_id)
where actor_id>200;

delete from sakila.actor
where actor_id>200;

Ejercicio ong

 


DROP TABLE IF EXISTS `poblacion`;

CREATE TABLE `poblacion` (
 `idpoblacion` int(11) NOT NULL AUTO_INCREMENT,
 `nombre` varchar(45) DEFAULT NULL,
 `habitantes` int(11) DEFAULT NULL,
 `pais` varchar(45) DEFAULT NULL,
 PRIMARY KEY (`idpoblacion`),
 KEY `ix_nombre` (`nombre`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


DROP TABLE IF EXISTS `sede`;

CREATE TABLE `sede` (
 `idsede` int(11) NOT NULL AUTO_INCREMENT,
 `ciudad` varchar(45) CHARACTER SET utf8 DEFAULT NULL,
 `pais` varchar(45) DEFAULT NULL,
 `direccion` varchar(45) DEFAULT NULL,
 `telefono` varchar(45) DEFAULT NULL,
 `director` varchar(45) DEFAULT NULL,
 PRIMARY KEY (`idsede`),
 KEY `ix_ciudad` (`ciudad`),
 KEY `ix_pais` (`pais`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;


DROP TABLE IF EXISTS `proyecto`;

CREATE TABLE `proyecto` (
 `idproyecto` int(11) NOT NULL AUTO_INCREMENT,
 `idsede` int(11) DEFAULT NULL,
 `titulo` varchar(45) DEFAULT NULL,
 `fini` date DEFAULT NULL,
 `ffin` date DEFAULT NULL,
 `presupuesto` decimal(10,2) DEFAULT NULL,
 `responsable` varchar(45) DEFAULT NULL,
 PRIMARY KEY (`idproyecto`),
 KEY `fk_sede_idx` (`idsede`),
 KEY `ix_titulo` (`titulo`),
 CONSTRAINT `fk_sede` FOREIGN KEY (`idsede`) REFERENCES `sede` (`idsede`) ON DELETE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;




DROP TABLE IF EXISTS `actuacion`;

CREATE TABLE `actuacion` (
 `idactuacion` int(11) NOT NULL AUTO_INCREMENT,
 `idproyecto` int(11) NOT NULL,
 `idpoblacion` int(11) DEFAULT NULL,
 `inversion` decimal(10,2) DEFAULT NULL,
 `descripcion` text,
 PRIMARY KEY (`idactuacion`),
 KEY `fk_proyecto` (`idproyecto`),
 KEY `fk_poblacion_idx` (`idpoblacion`),
 CONSTRAINT `fk_poblacion` FOREIGN KEY (`idpoblacion`) REFERENCES `poblacion` (`idpoblacion`),
 CONSTRAINT `fk_proyecto` FOREIGN KEY (`idproyecto`) REFERENCES `proyecto` (`idproyecto`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;