Vous êtes sur la page 1sur 4

Institut Preparatoire aux Etudes dIngenieur de Sfax: AU 2016-2017

Corrige de la serie de BDD


Propose par: Atef MASMOUDI

import sqlite3
def BDDconnect ( bdd ):
return sqlite3 . connect ( bdd )

def BDDclose ( conn ):


conn . commit ()
conn . close ()

def SQLquery ( cmd , conn ):


c = conn . cursor ()
c . execute ( cmd )
rows = c . fetchall ()
for x in rows :
print ( x )

# Exercice 1
Ex1_1 = SELECT MIN ( ab * ac * bc ) FROM triangles WHERE ab + bc + ac >=100
Ex1_2 = """ SELECT ab , ac , bc FROM triangles WHERE ab + bc + ac >=100
AND ab * ac * bc IN ( SELECT MIN ( ab * ac * bc ) FROM triangles WHERE ab + bc + ac >=100) """
Ex1_3 = SELECT * FROM triangles WHERE ab * ab + ac * ac = bc * bc
Ex1_4 = SELECT COUNT (*) FROM triangles WHERE ab * ab + ac * ac = bc * bc
Ex1_5 = SELECT MAX ( ab + bc + ac ) FROM triangles WHERE ab * ab + ac * ac = bc * bc
Ex1_6 = SELECT * FROM triangles WHERE ab = ac AND ab = bc
Ex1_7 = SELECT * FROM triangles WHERE ( ab + bc + ac )/3.0 = 42

def TrianglesTest ():


conn = BDDconnect ( triangles . sqlite )
print ( """ la plus petite valeur des produits AB * AC * BC
pour les triangles ABC de p e rim`e tre sup e rieur ou
e gal `
a 100 """ )
SQLquery ( Ex1_1 , conn )
print ( """ les longueurs AB , AC et BC correspondants au ( x ) triangles ( s )
pour le ( s ) quel ( s ) le minimum pr e ce dent est atteint """ )
SQLquery ( Ex1_2 , conn )
print ( " tous les triangles rectangles en A " )
SQLquery ( Ex1_3 , conn )
print ( " le nombre de tels triangles " )
SQLquery ( Ex1_4 , conn )
print ( " le maximum des p e rim`e tres des triangles rectangles en A " )
SQLquery ( Ex1_5 , conn )
print ( " tous les triangles e quilat e raux " )
SQLquery ( Ex1_6 , conn )
print ( " tous les triangles tels que ( AB + AC + BC )/3= 42 " )
SQLquery ( Ex1_4 , conn )
BDDclose ( conn )

1
# Exercice 2
Ex2_1 = " " " SELECT C . nom , D . nom , R . nom , C . pop FROM communes AS C
JOIN departements AS D ON C . dep = D . id JOIN regions as R
ON R . id = D . reg " " "
Ex2_2 = " " " SELECT C . nom , D . nom , R . nom , C . pop FROM communes AS C
JOIN departements AS D ON C . dep = D . id JOIN regions as R
ON R . id = D . reg ORDER BY C . pop DESC " " "
Ex2_3 = " " " SELECT count ( C . nom ) FROM communes AS C
WHERE C . pop >= ( SELECT pop FROM communes WHERE nom = Strasbourg ) " " "
Ex2_4 = SELECT nom , pop FROM communes WHERE nom LIKE " Pa % is "
Ex2_5 = SELECT nom , pop FROM communes WHERE LENGTH ( nom ) > pop

def CommunesTest ():


conn = BDDconnect ( c o m m u n e s _ d e p a r t e m e n t s _ r e g i o n s . sqlite )
print ( " " " la liste de toutes les communes avec pour
chacune son d e partement , sa r e gion et sa population " " " )
SQLquery ( Ex2_1 , conn )
print ( " " " Trier la liste pr e ce dente par ordre d e croissant
de population " " " )
SQLquery ( Ex2_2 , conn )
print ( " " " De terminer le rang de la ville de Strasbourg
dans ce classement " " " )
SQLquery ( Ex2_3 , conn )
print ( " " " Donner la liste des communes ( nom et population )
dont le nom commence par Pa et se finissant par is ( utiliser LIKE ) " " " )
SQLquery ( Ex2_4 , conn )
print ( " " " De terminer les communes ( nom et population )
qui ont strictement moins d habitants que de lettres dans leur nom " " " )
SQLquery ( Ex2_5 , conn )
BDDclose ( conn )

# Exercice 3
Ex3_1 = " SELECT * FROM prenoms WHERE prenom = Abdallah ORDER BY annee "
Ex3_2 = " " " SELECT annee , COUNT ( DISTINCT prenom ) FROM prenoms
GROUP BY annee " " "
Ex3_3 = SELECT prenom FROM prenoms GROUP BY prenom HAVING SUM ( nombre ) = 100
Ex3_4 = " " " SELECT prenom , SUM ( nombre ) AS total FROM prenoms
GROUP BY prenom ORDER BY total DESC LIMIT 1 " " "
Ex3_5 = SELECT prenom , annee , MAX ( nombre ) FROM prenoms GROUP BY annee

def PrenomsTest ():


conn = BDDconnect ( prenoms_paris . sqlite )
print ( " " " le nombre de fois le pr e nom Abdallah a -t - il e t
e donn
e
a Paris pendant les ann
` e es concern e es ?
On donnera le r e sultat tri e par ann e es croissantes " " " )
SQLquery ( Ex3_1 , conn )
print ( " " " pour chaque ann e e , le nombre de pr e noms diff e rents
qui ont e t
e enregistr es """)
SQLquery ( Ex3_2 , conn )
print ( " " " les pr e noms qui ont e te donn e s exactement 100 fois
sur la pe riode compl` e te " " " )
SQLquery ( Ex3_3 , conn )
print ( " " " le pre nom qui a e t
e le plus donn e sur l ensemble

2
de la p
e riode ( on donnera aussi le nombre de fois qu il a e t
e donn
e )""")
SQLquery ( Ex3_4 , conn )
print ( " " " Pour chaque ann
e e , donner le pr e nom , l ann
ee
et le nombre de fois que ce pr e nom a
e t
e donne """)
SQLquery ( Ex3_5 , conn )
BDDclose ( conn )

# Exercice 4
Ex4_1 = SELECT nom FROM profs
Ex4_2 = SELECT COUNT (*) FROM colles WHERE note =20
Ex4_3 = " " " SELECT note FROM colles , eleves WHERE eleve = ide
AND nom = Lions ORDER BY semaine " " "
Ex4_3p = " " " SELECT note FROM colles JOIN eleves ON eleve = ide
WHERE nom = Lions ORDER BY semaine " " "
Ex4_4 = " " " SELECT e . nom , p . nom , note , semaine FROM colles JOIN eleves AS e
ON eleve = ide JOIN profs AS p ON prof = idp WHERE note >= 19 " " "
Ex4_5 = " " " SELECT AVG ( note ) FROM colles JOIN eleves AS e ON eleve = ide
WHERE nom = Lions " " "
Ex4_6 = " " " SELECT nom FROM ( SELECT eleve FROM
( SELECT eleve , COUNT (*) AS nb FROM colles WHERE note < 10
GROUP BY eleve ) WHERE nb >= 10) JOIN eleves AS e ON eleve = ide " " "
Ex4_7 = " " " SELECT nom FROM ( SELECT eleve FROM
( SELECT eleve , COUNT (*) AS nb FROM colles WHERE note > 18
GROUP BY eleve ) WHERE nb >= 6) JOIN eleves AS e ON eleve = ide " " "
Ex4_8 = " " " SELECT nom , AVG ( note ) FROM colles JOIN eleves AS e
ON eleve = ide GROUP BY nom ORDER BY AVG ( note ) " " "
Ex4_9 = " " " SELECT nom FROM colles JOIN profs ON prof = idp
GROUP BY nom ORDER BY AVG ( note ) DESC LIMIT 1 " " "
Ex4_10 = " " " SELECT nom , AVG ( note ) AS moyenne ,
AVG ( note * note ) - AVG ( note )* AVG ( note ) AS variance FROM colles
JOIN profs ON prof = idp GROUP BY nom " " "
Ex4_11 = " " " SELECT AVG ( moyenne ) FROM ( SELECT AVG ( note ) AS moyenne
FROM colles JOIN eleves AS e ON eleve = ide GROUP BY nom ) " " "

def CollesTest ():


conn = BDDconnect ( notes_colles . sqlite )
print ( " la liste des professeurs " )
SQLquery ( Ex4_1 , conn )
print ( " le nombre de 20 qui ont e t
e attribu es ")
SQLquery ( Ex4_2 , conn )
print ( " " " les notes de Jacques - Louis Lions
( tri
e es selon les semaines croissantes ) " " " )
SQLquery ( Ex4_3 , conn )
print ( " " " les quadruplets ( e l`e ve , prof , note , semaine ) pour toutes
les colles o` u la note e tait sup e rieure ou e gale `a 19 " " " )
SQLquery ( Ex4_4 , conn )
print ( " la moyenne des notes de colle de Jacques - Louis Lions " )
SQLquery ( Ex4_5 , conn )
print ( " " " Parmi tous les e l`
e ves , d e terminer le nom de ceux ayant
eu au moins 10 notes strictement sous la moyenne " " " )
SQLquery ( Ex4_6 , conn )
print ( " " " Parmi tous les e l`
e ves , afficher ceux ayant eu au moins 6
notes strictement sup e rieures ` a 18 " " " )

3
SQLquery ( Ex4_7 , conn )
print ( " la liste des couples ( e l`
e ve , moyenne ) " )
SQLquery ( Ex4_8 , conn )
print ( " " " le nom du colleur qui tend ` a donner les meilleurs notes en moyenne
SQLquery ( Ex4_9 , conn )
print ( " pour chaque colleur la moyenne des notes donn e es et la variance " )
SQLquery ( Ex4_10 , conn )
print ( " la moyenne des moyennes des e l`
e ves " )
SQLquery ( Ex4_11 , conn )
BDDclose ( conn )

TrianglesTest ()
CommunesTest ()
PrenomsTest ()
CollesTest ()