0% found this document useful (0 votes)
19 views

Correction Du TD1 SQL

This document contains the solutions to various SQL questions on creating tables, inserting data, and running queries on a database with tables for clients, products, orders, and order items. The questions cover topics like updating records, deleting records, joining tables, aggregating data, and more. The solutions provide the SQL queries needed to answer each question.

Uploaded by

ranimkhemire
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Correction Du TD1 SQL

This document contains the solutions to various SQL questions on creating tables, inserting data, and running queries on a database with tables for clients, products, orders, and order items. The questions cover topics like updating records, deleting records, joining tables, aggregating data, and more. The solutions provide the SQL queries needed to answer each question.

Uploaded by

ranimkhemire
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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);

create table produit(


codp varchar2(5) constraint Pk_Codp primary key,
libp varchar2(50) constraint NN_Libp not null,
pu number(12,3) constraint NN_Pu not null constraint Ck_Pu check(pu>0),
qtestk number constraint Ck_QteStk check(qtestk >0));

create table commande(


ncde varchar2(10) constraint Pk_Ncde primary key,
datec date constraint NN_Datec Not null,
codec varchar2(5) constraint Fk_Codec references client(codec));

create table pc(


ncde varchar2(10) constraint Fk_Ncde references commande(ncde),
codp varchar2(5) constraint Fk_Codp references produit(codp),
qtecom number constraint Ck_QteCom check(qtecom >0),
constraint Pk_Ncde_Codp primary key(ncde,codp));

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');

insert into produit values('Pr100','Souris 3 Boutons',14,580);


insert into produit values('Pr200','Souris 2 Boutons',11,200);
insert into produit values('Pr300','Ecran SVGA 17 Pouces',200,150);
insert into produit values('Pr400','Clavier Arabe-Francais',11,250);
insert into produit values('Pr500','Unite Centrale PIII',650,60);
insert into produit values('Pr600','Carte Mere Pentium 166',120,20);

insert into commande values('Com01/04','12/03/2004','CL005');


insert into commande values('Com02/04','14/03/2004','CL001');

Réalisé par : Bassem Boughzala (Iset Rades) Page :1/7


Correction du TD1 SQL

insert into commande values('Com03/04','18/04/2004','CL002');


insert into commande values('Com04/04','19/05/2004','CL001');
insert into commande values('Com05/04','01/06/2004','CL004');
insert into commande values('Com06/04','28/07/2004','CL002');
insert into commande values('Com07/04','28/07/2004','CL002');
insert into commande values('Com08/04','28/07/2004','CL002');
insert into commande values('Com09/04','15/08/2004','CL003');
insert into commande values('Com10/04','15/08/2004','CL001');
insert into commande values('Com11/04','19/09/2004','CL003');
insert into commande values('Com12/04','22/09/2004','CL004');

insert into pc values('Com01/04','Pr100',10);


insert into pc values('Com01/04','Pr200',5);
insert into pc values('Com01/04','Pr500',7);
insert into pc values('Com02/04','Pr200',2);
insert into pc values('Com02/04','Pr300',3);
insert into pc values('Com03/04','Pr100',4);
insert into pc values('Com03/04','Pr200',1);
insert into pc values('Com03/04','Pr300',2);
insert into pc values('Com03/04','Pr400',5);
insert into pc values('Com03/04','Pr500',7);
insert into pc values('Com03/04','Pr600',6);
insert into pc values('Com04/04','Pr400',9);
insert into pc values('Com05/04','Pr100',10);
insert into pc values('Com05/04','Pr600',11);
insert into pc values('Com06/04','Pr200',15);
insert into pc values('Com06/04','Pr400',20);
insert into pc values('Com06/04','Pr600',14);
insert into pc values('Com07/04','Pr100',12);
insert into pc values('Com07/04','Pr300',10);
insert into pc values('Com07/04','Pr500',5);
insert into pc values('Com08/04','Pr100',2);
insert into pc values('Com09/04','Pr200',1);
insert into pc values('Com10/04','Pr300',1);
insert into pc values('Com11/04','Pr400',2);
insert into pc values('Com12/04','Pr500',3);

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;

Réalisé par : Bassem Boughzala (Iset Rades) Page :2/7


Correction du TD1 SQL

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 :

Réalisé par : Bassem Boughzala (Iset Rades) Page :3/7


Correction du TD1 SQL

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 :

Réalisé par : Bassem Boughzala (Iset Rades) Page :4/7


Correction du TD1 SQL

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

Réalisé par : Bassem Boughzala (Iset Rades) Page :5/7


Correction du TD1 SQL

From Client, Commande, Produit, PC


Where Client.Codec = Commande.Codec
And PC.Ncde = Commande.Ncde
And PC.Codp = Produit.Codp
And Pu >=200;

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;

Si on raisonne par rapport au montant :

Réalisé par : Bassem Boughzala (Iset Rades) Page :6/7


Correction du TD1 SQL

Select Ncde, Sum(QteCom * PU) Total_Commandée


From Pc, Produit
Where Pc.codp = Produit.Codp
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);

Réalisé par : Bassem Boughzala (Iset Rades) Page :7/7

You might also like