Correction TP 4
Correction TP 4
/* 1 */
create table Client(
NC SMALLINT PRIMARY KEY,
Nom varchar(15),
Prenom varchar(15),
Adresse varchar(50)
);
/* 2 */
insert into Client values (1, 'ALAOUI', 'Hamid', 'Rabat');
insert into Client values (2, 'ISMAILI', 'Nada', 'Casablanca');
insert into Client values (3, 'FARHATI', 'Hiba', 'Marrakech');
insert into Client values (4, 'MORTAJI', 'Youssef', 'Tanger');
/* 3 */
insert into Location values (1, 113, '2023-04-01', 5);
insert into Location values (1, 215, '2023-05-04', 5);
insert into Location values (1, 311, '2023-03-25', 3);
insert into Location values (1, 113, '2023-04-21', 3);
insert into Location values (2, 213, '2023-02-11', 2);
insert into Location values (2, 315, '2023-03-14', 4);
insert into Location values (2, 111, '2023-01-12', 1);
insert into Location values (2, 112, '2022-01-21', 2);
insert into Location values (3, 313, '2023-02-19', 4);
insert into Location values (3, 215, '2023-01-11', 2);
insert into Location values (3, 112, '2022-04-21', 3);
insert into Location values (4, 316, '2023-05-19', 2);
insert into Location values (4, 116, '2023-03-10', 5);
insert into Location values (4, 216, '2023-04-11', 5);
insert into Location values (4, 318, '2023-05-01', 2);
insert into Location values (4, 117, '2023-03-20', 5);
insert into Location values (4, 219, '2023-04-21', 5);
/* 4 */
select * from Client;
/* 5 */
select * from Client, location where Client.NC=Location.NC and
Location.NCH = 219;
/* 6 */
select distinct Client.nom, Client.Prenom from Client, location, Chambre,
CategorieChambre where Client.NC=Location.NC and Location.NCH =
Chambre.NC and Chambre.NTC = CategorieChambre.NTC and
CategorieChambre.FL > 900;
/* 7 */
select location.NC, location.NCH, location.DL,
(location.PL*CategorieChambre.FL) as 'Frais Location Total'
from location, Chambre, CategorieChambre
where Location.NCH = Chambre.NC
and Chambre.NTC = CategorieChambre.NTC
group by NC, NCH, DL;
/* 8 */
select location.NC, location.NCH, location.DL, Client.Nom, Client.Prenom,
CategorieChambre.Nom, Location.PL, CategorieChambre.FL,
(location.PL*CategorieChambre.FL) as 'Frais Location Total' from Client,
location, Chambre, CategorieChambre
where Client.NC=Location.NC
and Location.NCH = Chambre.NC
and Chambre.NTC = CategorieChambre.NTC
group by NC, NCH, DL;
/* 9 */
select nch from Location where dl < '2023-02-01';
/* 10 */
select nch from Location where pl between 3 and 4;
/* 11 */
select nc, count(*) from location GROUP by nc;
/* 12 */
select nc, count(distinct nch) from location GROUP by nc;
/* 13 */
select nch, count(nc) from location GROUP by nch;
/* 14 */
select nc, pl, count(*)
from location
GROUP by nc, pl
having pl BETWEEN 3 and 4;
/* 15 */
select nch, count(nc) from location GROUP by nch having nch in (select
chambre.nc from chambre, categoriechambre where chambre.NTC =
categoriechambre.NTC and categoriechambre.fl < 600);
/* 16 */
select location.nc, sum(pl*fl) from location, chambre, categoriechambre
where location.NCH = chambre.NC
and chambre.NTC = categoriechambre.NTC
group by location.nc;
/* 17 */
select location.nc, sum(pl*fl) from location, chambre, categoriechambre
where location.NCH = chambre.NC
and chambre.NTC = categoriechambre.NTC
group by location.nc
having location.nc in (1, 3, 5);
/* 18 */
select location.nc, sum(pl*fl) from location, chambre, categoriechambre
where location.NCH = chambre.NC
and chambre.NTC = categoriechambre.NTC
group by location.nc
having count(location.nc) > 4;
/* 19 */
select location.nc, sum(pl*fl) from location, chambre, categoriechambre
where location.NCH = chambre.NC
and chambre.NTC = categoriechambre.NTC
group by location.nc
having sum(fl*pl) between 5000 and 15000;
/* 20 */
select nom from categoriechambre where fl > (select avg(fl) from
categoriechambre);
/* 21 */
select distinct location.nch from Location where location.dl not between
'2022-01-01' and '2022-12-31';