Correction Du TD1 SQL
Correction Du TD1 SQL
Question 1 :
create table client(
codec varchar2(5) constraint Pk_Codec primary key,
nomc varchar2(20) constraint NN_Nomc not null,
credit number constraint NN_Credit not null,
villec varchar2(25) constraint NN_Villec not null);
Question 2 :
insert into client values('CL001','Ben Salah',1000,'Tunis');
insert into client values('CL002','Ben othman',1500,'Sousse');
insert into client values('CL003','Jedidi',1200,'Tunis');
insert into client values('CL004','Issaoui',500,'Sfax');
insert into client values('CL005','Nouri',750,'Sousse');
insert into client values('CL006','Temimi',900,'Tunis');
insert into client values('CL007','Aouini',0,'Sfax');
insert into client values('CL008','Bargaoui',700,'Nabeul');
insert into client values('CL009','Amouri',500,'Tunis');
commit;
Question 3 :
update produit
set Pu = pu * 1.1
where qtestk between 100 and 300;
Question 4 :
Delete from client
where Nomc like 'A%' and credit = 0;
Question 5 :
select Ncde,Datec
from commande
where Codec ='CL001';
Question 6 :
1ère méthode :
select *
from client
where codec in (select distinct codec
from commande);
ème
2 méthode :
select distinct client.*
from client, commande
where client.codec=commande.codec;
Question 7 :
1ère méthode :
select *
from client
where codec not in (select distinct codec
from commande);
2ème méthode :
select Codec
from client
Minus
Select Codec
from commande;
Question 8 :
1ère méthode :
Select Codp, Libp, Pu
From Produit
Where Pu between 10 and 30;
2ème méthode :
Select Codp, Libp, Pu
From Produit
Where Pu >= 10 and Pu <= 30;
Question 9 :
1ère méthode :
Select Codp, Libp, QteStk
From Produit
Where QteStk > (Select QteStk
From Produit
Where Codp = 'Pr200');
2ème méthode :
Select P1.Codp, P1.Libp, P1.Pu
From Produit P1, Produit P2
Where P2.Codp='Pr200' and P1.QteStk > P2.QteStk;
Question 10 :
1ère méthode :
select Codec, Nomc
From Client
where Codec in (select distinct Codec
from Commande
where Ncde in (select Ncde
from Pc
where Codp = 'Pr200'));
2ème méthode :
select distinct Client.Codec, Nomc
From Client, Commande, Pc
where Client.Codec = Commande.Codec
and Commande.Ncde = Pc.Ncde
and Codp = 'Pr200';
Question 11 :
1ère méthode :
Select *
from Pc
Where Ncde in (Select Ncde
From Commande
Where Codec ='CL002' and Datec ='28/07/2004');
2ème méthode :
Select Pc.*
From Pc, Commande
Where Commande.Ncde = Pc.Ncde
and Codec ='CL002' and Datec ='28/07/2004';
Question 12 :
1ère méthode :
Select Codp, Pu
From Produit
Where Codp in (Select Distinct Codp
From PC
Where Ncde in (Select Ncde
From Commande
Where Datec >'14-Aug-2004'));
2ème méthode :
Select Distinct Produit.Codp, PU
From Produit, PC, Commande
where Produit.Codp = PC.Codp
And PC.Ncde = Commande.Ncde
And Datec > '14-Aug-2004';
Question 13 :
1ère méthode :
Select Distinct Ncde
From PC
Where Codp in (Select Codp
From Produit
Where Pu >= 200);
2ème méthode :
Select Distinct Ncde
From Produit, PC
Where PC.Codp = Produit.Codp
And Pu >=200;
Question 14 :
1ère méthode :
Select Client.Codec, Nomc
From Client
Where Codec in (Select Distinct Codec
From Commande
Where Ncde in (Select Distinct Ncde
From PC
Where Codp in (Select Codp
From Produit
Where Pu >= 200)));
2ème méthode :
Select distinct Client.Codec, Nomc
Question 15 :
1ère méthode :
Select Codp, Libp, Pu
From Produit
Where Pu > (Select Min(Pu)
From Produit
Where QteStk > 200);
2ème méthode :
Select Codp, Libp, Pu
From Produit
Where Pu >Any (Select Pu
From Produit
Where QteStk > 200);
Question 16 :
1ère méthode :
Select Codp, Libp, Pu
From Produit
Where Pu > (Select Max(Pu)
From Produit
Where QteStk > 200);
2ème méthode :
Select Codp, Libp, Pu
From Produit
Where Pu >All (Select Pu
From Produit
Where QteStk > 200);
Question 17 :
Si on raisonne par rapport à la quantité :
Select Ncde,Sum(QteCom)
From Pc
Group by Ncde;
Question 18 :
Select Ncde, count(*)
From Pc
Group By Ncde;
Question 19 :
Select Codp, Sum(QteCom)
From Pc
Group By Codp;
Question 20 :
Select Codep, Sum(QteCom)
From Pc
Group by Codep
Having Sum(QteCom) >25;
Question 21 :
Select Codp, Libp, Pu
From Produit
where Pu = (Select Min(Pu)
From Produit);