0% found this document useful (0 votes)
16 views10 pages

TP-2 (Corrigé)

This document contains SQL queries and solutions to various problems involving database tables. It includes queries to select, update, delete and aggregate data from tables with columns like CA, CE, CF, NOMA, DELAI, PRIXA and joins tables like ART, COND, EMB, FRS. The document also contains queries using functions, operators and clauses like GROUP BY, HAVING, UNION and views.

Uploaded by

aymenkrid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views10 pages

TP-2 (Corrigé)

This document contains SQL queries and solutions to various problems involving database tables. It includes queries to select, update, delete and aggregate data from tables with columns like CA, CE, CF, NOMA, DELAI, PRIXA and joins tables like ART, COND, EMB, FRS. The document also contains queries using functions, operators and clauses like GROUP BY, HAVING, UNION and views.

Uploaded by

aymenkrid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

TP 2

1°\
select CA,NOMA,DELAI from ART where delai between 7 and 14 order by delai ASC;

2°\
select upper(CA),upper(NOMA) from ART where (upper(CA) like 'B%' or CA like 'E%' or
CA like 'L%') order by CA Desc;

3°\
Methode 1
select CF from ART
where CA like 'A%'
and CF in ( select CF from ART where CA like 'L%' ) ;

Methode 2
Select CF from ART
Where CA like 'A%'
Interest
Select CF from ART
Where CA like 'L%'

4°\
A-
select CF , count (*) as s
from ART
where CF IS NOT NULL
group by CF
order by s ;

B-
select avg (prixa) as "pm" , avg (delai) as "dm"
from ART
where CA like 'M%' ;

C-
select count (CF) as "A non fabrique" , (count (CA)-count (CF)) as "A fabrique"
from ART ;

E-
select
case substr(ca,1,1)
when 'A' then 'audiovisuel'
when 'B' then 'bricolag'
when 'E' then 'electromenage'

1
when 'L' then 'loisir'
when 'M' then 'mobiler'
when 'T' then 'textile'
end as type_article ,
round(avg(prixa),3) as prix_moyeenne
from art
group by substr(ca,1,1) ;

5°\
A-
select
CA , sysdate+delai
from art
where CF is not null ;

B-
select CA , noma , prixa , round( prixa*0.85,3)
from art ;

6°\
A-
select Concat (min(DELAI),'Jours')
from ART;

2eme methode
select min(DELAI) || ' Jours'
from ART;

B-
select CA
FROM ART
where DELAI =(select MIN(DELAI) from ART);

7°\
A-
select CF , count (*)
from EMB
group by CF
having count (*) >=2 ;

B-
select CF , CE
from EMB
where CF in (select CF from EMB group by CF having count (CE) =1 ) ;

8°\

2
select case substr(ca,1,1)
when 'A' then 'audiovisuel'
when 'B' then 'bricolag'
when 'E' then 'electromenage'
when 'L' then 'loisir'
when 'M' then 'mobiler'
when 'T' then 'textile'
end as type_article
from art
having ((min(delai))>=7) and ((max(delai))<=14)
group by substr(ca,1,1);

2eme methode
select substr (CA,1,1)
from ART
group by substr (CA,1,1)
having min (DELAI) >=7 and max (DELAI) <=14

3eme methode
select distinct ( substr (CA,1,1) )
from ART
Where delai is not null
And substr (CA , 1,1) not in ( select substr (CA , 1,1) from ART where delai <7 or delai >
14 );

4eme methode
select substr (CA,1,1)
from ART
Where delai is not null
minus
select substr (CA , 1,1) from ART where delai not between 7 and 14 ;

9°\
A-
Select CA
From COND
Group by CA
Having count (CE)>=3;

B-
select count(distinct CA )
from COND
where CA in (select CA from COND group by CA having count(CE)>=3);

10°\

3
select CA,PRIXA,DELAI,NOMA from art where prixa > all (select prixa from ART Where
CF='F01');

11°\
select frs.cf , count(ce)
from frs left outer join emb on frs.cf = emb.cf
group by frs.cf
order by count(ce) desc ;

12°\
( methode avec in )
select ca , noma
from art
where ca in (select ca from cond where ce in ( select ce from emb where lower(
nome)='carton')) ;

( methode avec join )


select art.ca, noma
from art join cond on art.ca = cond.ca join emb on cond.ce =emb.ce where
upper(nome)='CARTON' ;

(methode avec exists )


select ca, noma
from art
where exists (select * from cond where cond.ca = art.ca and exists (select * from emb
where cond.ce = emb.ce and lower(nome)='carton'));

13°\
( methode 1 )
select cf , nomf
from frs
where cf in (select cf from frs
minus
select cf from art );

( methode 2 )
select cf , nomf
from frs
where cf NOT IN (select cf from art where cf is not NULL );

(methode 3 )
select cf , nomf
from frs
where NOT exists (select * from art where frs.cf = art.cf);

(methode 4 )

4
select frs.CF , NOMF
from frs left outer join ART on frs.CF = ART.CF
where ART.CF is null ;

14°\
A-
select CE
from cond
minus
select CE
from cond
where CA like 'E%' ;

B-
select distinct CE from COND
where CE not in (select CE from COND where CA like 'E%' );

C-
select distinct C1.CE from COND C1
where NOT Exists (select * from COND C2 where CA like 'E%' and C1.CE=C2.CE );

15°\
A-
(methode 1 )
select distinct CA , INITCAP(NOMA)
from ART
where CA in (select CA from COND where CE in (select CE from EMB where CF in
(select CF from FRS where TYPF='STA' ))) ;

(methode 2)
select distinct ART.CA , INITCAP(ART.NOMA)
from ART join COND on ART.CA = COND.CA join EMB on COND.CE = EMB.CE join
FRS on EMB.CF = FRS.CF
where typf = 'STA';

B-
Select CA from COND where CE='E04'
UNION
Select CA from COND where CE='E05';
C-
select CA from COND where CE='E03'
INTERSECT
select CA from COND where CE='E06' ;

Mathode 2
select C1.CA from COND C1 join COND C2

5
on C1.CA = C2.CA
where C1.CE = 'E03' and C2.CE = 'E06' ;

16°\
A-
update EMB set CF='F04' where CE='E01';
update ART set CF='F08' where CA='A01';

B-
Select CF from FRS where TYPF='STA' and (CF in(select CF from EMB) and CF not
in(select CF from ART)) or (CF in(select CF from ART) and CF not in(select CF from
EMB));

17°\
A-
alter table ART add NBEMB NUMBER default 0 ;

B-
update ART set NBEMB=(select count (CE) from COND where ART.CA=COND.CA);

C-
Commit ;

18°\
A-
delete from COND where CA like 'A%' and CE in (select CE from EMB where
lower(NOME)='caisse');

B-
select distinct CE , NOME
from EMB
where CE in (select CE from COND where CA like 'A%');

Methode 2
select Distinct EMB.CE , NOME
from EMB join COND
on EMB.CE = COND.CE where CA like 'A%';

C-
select distinct ART.CA, NOMA
from ART join COND on ART.CA=COND.CA
join EMB on COND.CE = EMB.CE
where upper(NOME)='CAISSE';

D-

6
Rollback;

19°\
A-
alter table COND add (PRIXNET number default 0, COUTBUT number default 0 );

B-
update COND set PRIXNET = NBART*(select PRIXA from ART where COND.CA =
ART.CA );

Methode 2
update COND set PRIXNET = (select NBART*PRIXA from ART where
COND.CA=ART.CA);

20°\
A-
Creer view :
create view V_NBCOND (code,NBEMB)
as select CA , count (CE)
from COND
group by CA ;
select * from V_NBCOND ;
Methode 1 :
select CA , NOMA
from ART
where CA in (select CA from COND Group by CA having count (CE)=(select
Max(NBEMB)from V_NBCOND ));
Methode 2 :
select CA , NOMA
from ART
where NBEMB = (select Max (NBEMB) from V_NBCOND );

B-
Creer view :
create view V_NBART (code,NBART)
as select CE , count (CA)
from COND
group by CE ;
Solution :
select CE , NOME , NBART
from EMB join V_NBART on CE=code
where NBART > (select AVG(NBART)from V_NBART );
C-
Creer view :
create view V_FRSART (code,NBART)

7
as select CF , count (CA)
from ART
group by CF ;
create view V_FRSEMB (code,NBART)
as select CF , count (CE)
from EMB
group by CF ;
Solution :
select cf,nomf, nbart ,nbemb from frs join V_frsart on cf=V_frsart.code join V_frsemb on
cf=V_frsemb.code order by cf asc; he4i ne9sa 5ater taffichi ken les frs elli commun biin
biin les 2 views w table frs bech taffichihom l koll lezem ta3mel join externe ta3 left outer

You might also like