Beruflich Dokumente
Kultur Dokumente
Février 2015
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 1/45 Février 2015 1 / 45
Requêtes avancées
Manipulations ensemblistes
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 2/45 Février 2015 2 / 45
Requêtes avancées Opérateur IN
Utilisation de sous-requêtes : IN
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 3/45 Février 2015 3 / 45
Requêtes avancées Opérateur IN
Utilisation de sous-requêtes : IN
Exemples
Liste des joueurs cancadiens.
SELECT DISTINCT last_name , f i r s t _ n a m e
FROM p l a y e r
WHERE code IN ( S E L E C T code FROM
country_codes
WHERE c o u n t r y = ’ C a n a d a ’ ) ;
est équivalente à :
SELECT DISTINCT last_name , f i r s t _ n a m e
FROM p l a y e r p , c o u n t r y _ c o d e s cc
WHERE ( p . code = cc . code )
AND ( cc . c o u n t r y = ’ C a n a d a ’ )
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 4/45 Février 2015 4 / 45
Requêtes avancées Opérateur IN
Utilisation de sous-requêtes : IN
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 5/45 Février 2015 5 / 45
Requêtes avancées Opérateur IN
Utilisation de sous-requêtes : IN
Pratique à utiliser sous la forme NOT IN
Nota
< attributs > : peut être une liste d’attributs.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 7/45 Février 2015 7 / 45
Requêtes avancées Opérateur ALL et ANY
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 8/45 Février 2015 8 / 45
Opérateur EXISTS
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 11/45 Février 2015 11 / 45
Opérateur EXISTS
S E L E C T D I S T I N C T ON ( last_name , f i r s t _ n a m e ) *
FROM atp . p l a y e r p2
WHERE E X I S T S
( SELECT *
FROM atp . r e g i s t r a t i o n r2 JOIN atp . game g
ON ( r2 . r e g i s t r n u m = g . r e g i s t r n u m 2 )
JOIN atp . r e g i s t r a t i o n r1
ON ( r1 . r e g i s t r n u m = g . r e g i s t r n u m 1 )
W HE RE p2 . pid = r2 . pid
AND r1 . pid =1)
Remarques
pid=1 est le numéro de Rafael Nadal et dans la relation game. Dans
l’état actuel de la base, le numéro d’inscription du vainqueur est
registrnum1.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 12/45 Février 2015 12 / 45
Opérateur EXISTS Intersection, Union, Différence
Opérateurs ensemblistes
Union
< requête1 > UNI ON < requête2 >
Différence
< requête1 > E X C E P T < requête2 >
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 14/45 Février 2015 14 / 45
Opérateur EXISTS Intersection, Union, Différence
Le procédé NOT EXISTS peut s’avérer couteux si les tables ne sont pas
convenablement indexées.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 17/45 Février 2015 17 / 45
Sous requêtes : WITH
WITH w i t h _ q u e r y _ n a m e [ ( c o l u m n _ n a m e [ , ...] )
AS ( S E L E C T ... )
S E L E C T ....
FROM ...
WH ER E ...
GR OU P BY ...
H A V I N G ...
OR DE R BY ...
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 18/45 Février 2015 18 / 45
La division
Division
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 20/45 Février 2015 20 / 45
La division
Définition de la division
Arbre de requêtes
- R de schéma R(A1 , A2 , . . . , Ak ), S de schéma S(Ap+1 , . . . , Ak )
est la relation T = R ÷ S de schéma T (A1 , . . . , Ap ).
R −
× R
πA1 ,...,Ap S
R . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 21/45 Février 2015 21 / 45
La division
WITH Rp AS (
S E L E C T A1 ,... , Ap
FROM R
)
S E L E C T * FROM Rp
EXCEPT
SELECT A1 ,... , Ap
FROM (
S E L E C T * FROM Rp , S
EXCEPT
S E L E C T * FROM R) R2 ;
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 22/45 Février 2015 22 / 45
La division
Exemple
WITH R AS
( S E L E C T tid , pid
FROM atp . p a r t i c i p a t i o n s
WH E R E d a t e _ p a r t = 2 008 AND s u r f a c e = ’ Clay ’ ) ,
S AS
( S E L E C T D I S T I N C T tid
FROM R ) ,
Rp AS
( S E L E C T D I S T I N C T pid FROM R )
S E L E C T pid
FROM Rp
EXCEPT
S E L E C T pid
FROM
( S E L E C T tid , pid
FROM Rp , S
EXCEPT
SELECT *
FROM R ) U ;
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 23/45 Février 2015 23 / 45
La division
Illustration de la division
Lister les joueurs qui participé à tous les tournois sur terre battue en 2008
On construit une table intermédiaire, la table des couples pid,tid où
tid est le numéro d’un tournoi disputé en 2008 sur terre battue et pid .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 25/45 Février 2015 25 / 45
La division
Exemples
Nom, prénom des joueurs qui ont participé à tous les tournois à plus de 5 tours disputés en 2008 sur
terre battue.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 26/45 Février 2015 26 / 45
Agrégats Fonctions SUM et AVG
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 28/45 Février 2015 28 / 45
Agrégats Fonctions MAX et MIN
Usage
MAX ( < n o m a t t r i b u t >)
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 30/45 Février 2015 30 / 45
Agrégats Fonction COUNT
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 31/45 Février 2015 31 / 45
Agrégats Fonction COUNT
SELECT CO UN T (*)
FROM t o u r n a m e n t _ b i g WHE R E tid = ANY
( S E L E C T tid
FROM p l a y e d _ i n _ b i g
WH E RE r e g i s t r n u m IN
( SELECT registrnum
FROM r e g i s t r a t i o n _ b i g
WH E RE pid IN
( S E L E C T pid
FROM p l a y e r _ b i g
WHE R E l a s t _ n a m e
= ’ F e d e r e r ’ )))
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 32/45 Février 2015 32 / 45
Partition et groupe GROUP BY
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 35/45 Février 2015 35 / 45
Partition et groupe GROUP BY
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 38/45 Février 2015 38 / 45
Partition et groupe GROUP BY
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 39/45 Février 2015 39 / 45
Partition et groupe Tri des résultats
Présentation/tri de résultats
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 40/45 Février 2015 40 / 45
Partition et groupe Tri des résultats
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 42/45 Février 2015 42 / 45
Partition et groupe Tri des résultats
Une sortie
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 43/45 Février 2015 43 / 45
Partition et groupe Tri des résultats
Une réponse
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 44/45 Février 2015 44 / 45
Conclusion
Résumé
Syntaxe générale
S E L E C T < attributs >
FROM < relations >
[ WH ER E < condition > ]
[ GR OU P BY < a t t r i b u t s de p a r t i t i o n n e m e n t >
[ H A V I N G < condition >] ]
[ OR DE R BY < critere >]
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
BD4 (Licence MASS, Master ISIFAR, Paris-Diderot) SQL 45/45 Février 2015 45 / 45